143 lines
4.5 KiB
PHP
143 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Traits\Models;
|
|
|
|
use App\Enums\InputType;
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use App\Lib\Helpers\ToolboxHelper;
|
|
|
|
trait MiniCrudModel
|
|
{
|
|
/**
|
|
* Fill the model with an array of attributes.
|
|
*
|
|
* @param array $attributes
|
|
* @return void
|
|
* @throws \Illuminate\Database\Eloquent\MassAssignmentException If fill prop is not allowed.
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.EmptyThrows
|
|
*/
|
|
public function fill(array $attributes)
|
|
{
|
|
parent::fill($attributes);
|
|
if (isset($attributes['data'])) {
|
|
$this->setData($attributes['data']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Apply function on data typed text while on save event
|
|
* @return void
|
|
*/
|
|
public function minicrudCreating()
|
|
{
|
|
/** @var array<string,array> */
|
|
$data = $this->data;
|
|
foreach ($data as &$input) {
|
|
switch ($input['inputType']->value ?? $input['inputType']['value']) {
|
|
case InputType::text()->value:
|
|
$input['text'] = clean($input['text']);
|
|
$input['text'] = FileStorageHelper::storeModelFilesFromHtml($this, $input['text']);
|
|
break;
|
|
} //end switch
|
|
} //end foreach
|
|
$this->data = $data;
|
|
}
|
|
|
|
/**
|
|
* Apply function on data typed text while on update event
|
|
* @return void
|
|
*/
|
|
public function minicrudUpdating()
|
|
{
|
|
$this->minicrudCreating();
|
|
}
|
|
|
|
/**
|
|
* Apply function on data typed text while on updated event
|
|
* @return void
|
|
*/
|
|
public function minicrudUpdated()
|
|
{
|
|
/** @var array */
|
|
foreach ($this->data as $index => $input) {
|
|
switch ($input['inputType']->value ?? $input['inputType']['value']) {
|
|
case InputType::text()->value:
|
|
FileStorageHelper::removeOldModelFilesFromHtml(
|
|
$input['text'],
|
|
$this->getOriginal('data')[$index]['text'] ?? ''
|
|
);
|
|
break;
|
|
} //end switch
|
|
} //end foreach
|
|
}
|
|
|
|
/**
|
|
* Apply function on data typed text while on deleted event
|
|
* @return void
|
|
*/
|
|
public function minicrudDeleted()
|
|
{
|
|
/** @var array */
|
|
foreach ($this->data as $input) {
|
|
switch ($input['inputType']->value ?? $input['inputType']['value']) {
|
|
case InputType::text()->value:
|
|
FileStorageHelper::removeModelFilesOfHtml($input['text']);
|
|
break;
|
|
} //end switch
|
|
} //end foreach
|
|
}
|
|
|
|
/**
|
|
* Set a specific prop
|
|
*
|
|
* @param array $data
|
|
* @return void
|
|
*/
|
|
private function setData(array $data): void
|
|
{
|
|
$attrs = $this->attributes;
|
|
if (isset($attrs['data'])) {
|
|
unset($attrs['data']);
|
|
}
|
|
$data = $this->serializeData($data);
|
|
$this->setAttribute('data', $data);
|
|
}
|
|
|
|
/**
|
|
* Serialize properties
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
* @throws \ErrorException If input type is no handled.
|
|
* @throws \Illuminate\Validation\ValidationException If url is a bo url.
|
|
* @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
|
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
|
|
*/
|
|
private function serializeData(array $data): array
|
|
{
|
|
foreach ($data as $o => $input) {
|
|
switch ($input['inputType']->value ?? $input['inputType']['value']) {
|
|
case InputType::image()->value:
|
|
$data[$o]['value'] = FileStorageHelper::storeFile($this, $input['value']);
|
|
break;
|
|
case InputType::boolean()->value:
|
|
case InputType::text()->value:
|
|
case InputType::password()->value:
|
|
case InputType::email()->value:
|
|
case InputType::number()->value:
|
|
case InputType::string()->value:
|
|
case InputType::videoVimeo()->value:
|
|
break;
|
|
case InputType::url()->value:
|
|
ToolboxHelper::assertNoInternalBoUrls('content', $input['value']);
|
|
break;
|
|
default:
|
|
throw new \ErrorException(sprintf(
|
|
'Unknown input Type %s',
|
|
$input['inputType']->label ?? $input['inputType']['label']
|
|
));
|
|
} //end switch
|
|
} //end foreach
|
|
return $data;
|
|
}
|
|
}
|