ldc-volena/app/Traits/Models/MiniCrudModel.php

201 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2023-11-13 11:56:06 +01:00
<?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<string, mixed> $attributes
* @return $this
*
2023-11-13 11:56:06 +01:00
* @throws \Illuminate\Database\Eloquent\MassAssignmentException If fill prop is not allowed.
* @phpcs:disable Squiz.Commenting.FunctionComment.EmptyThrows
*/
public function fill(array $attributes)
{
// phpcs:enable
parent::fill($attributes);
if (isset($attributes['data'])) {
$this->setData($attributes['data']);
}
return $this;
2023-11-13 11:56:06 +01:00
}
/**
2024-03-20 17:39:24 +01:00
* Apply function on data typed text while on save event.
2023-11-13 11:56:06 +01:00
*
* @return void
*/
2024-03-20 17:39:24 +01:00
public function minicrudCreating(): void
2023-11-13 11:56:06 +01:00
{
/** @var array<int, array<string, mixed>> */
2023-11-13 11:56:06 +01:00
$data = $this->data;
foreach ($data as &$input) {
switch ($input['inputType']) {
case InputType::text->value:
$input['value'] = clean($input['value']);
$input['value'] = FileStorageHelper::storeModelFilesFromHtml($this, $input['value']);
2023-11-13 11:56:06 +01:00
break;
} //end switch
} //end foreach
$this->data = $data;
}
/**
2024-03-20 17:39:24 +01:00
* Apply function on data typed text while on update event.
2023-11-13 11:56:06 +01:00
*
* @return void
*/
2024-03-20 17:39:24 +01:00
public function minicrudUpdating(): void
2023-11-13 11:56:06 +01:00
{
$this->minicrudCreating();
}
/**
2024-03-20 17:39:24 +01:00
* Apply function on data typed text while on updated event.
2023-11-13 11:56:06 +01:00
*
* @return void
*/
2024-03-20 17:39:24 +01:00
public function minicrudUpdated(): void
2023-11-13 11:56:06 +01:00
{
/** @var array<int, array<string, mixed>> $data */
$data = $this->data;
foreach ($data as $index => $input) {
2023-11-13 11:56:06 +01:00
switch ($input['inputType']) {
case InputType::text->value:
$original = ToolboxHelper::getOriginal($this, 'data');
FileStorageHelper::removeOldModelFilesFromHtml(
$input['value'],
$original[$index]['value'] ?? ''
);
break;
case InputType::image->value:
$original = ToolboxHelper::getOriginal($this, 'data');
FileStorageHelper::removeOldFiles(
[$original[$index]['value']],
[$input['value']]
2023-11-13 11:56:06 +01:00
);
break;
} //end switch
} //end foreach
}
/**
2024-03-20 17:39:24 +01:00
* Apply function on data typed text while on deleted event.
2023-11-13 11:56:06 +01:00
*
* @return void
*/
2024-03-20 17:39:24 +01:00
public function minicrudDeleted(): void
2023-11-13 11:56:06 +01:00
{
/** @var array<int, array<string, mixed>> $data */
$data = $this->data;
foreach ($data as $index => $input) {
2023-11-13 11:56:06 +01:00
switch ($input['inputType']) {
case InputType::text->value:
FileStorageHelper::removeModelFilesOfHtml($input['value']);
break;
case InputType::image->value:
$original = ToolboxHelper::getOriginal($this, 'data');
FileStorageHelper::removeOldFiles(
[$original[$index]['value']],
[]
);
2023-11-13 11:56:06 +01:00
break;
} //end switch
} //end foreach
}
/**
2024-03-20 17:39:24 +01:00
* Set a specific prop.
2023-11-13 11:56:06 +01:00
*
* @param array<string, mixed> $data
2023-11-13 11:56:06 +01:00
* @return void
*/
private function setData(array $data): void
{
$attrs = $this->attributes;
if (isset($attrs['data'])) {
unset($attrs['data']);
}
$this->setAttribute('data', $data);
}
/**
2024-03-20 17:39:24 +01:00
* Set a specific prop.
2023-11-13 11:56:06 +01:00
*
* @param array<int|string, mixed> $value
2023-11-13 11:56:06 +01:00
* @return void
*/
protected function setDataAttribute(array $value): void
{
$this->attributes['data'] = $this->castAttributeAsJson(
'data',
$this->serializeData($value)
);
}
/**
2024-03-20 17:39:24 +01:00
* Set a specific prop.
2023-11-13 11:56:06 +01:00
*
* @param string|null $data
* @return array<int|string, mixed>
2023-11-13 11:56:06 +01:00
*/
protected function getDataAttribute(string|null $data): array
{
return $this->fromJson($data ?? $this->attributes['data'] ?? '[]');
}
/**
2024-03-20 17:39:24 +01:00
* Serialize properties.
2023-11-13 11:56:06 +01:00
*
* @param array<int|string, array<string, mixed>> $data
* @return array<int|string, array<string, mixed>>
2023-11-13 11:56:06 +01:00
* @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']) {
case InputType::image->value:
$data[$o]['value'] = \preg_replace(
'/^\/|^/',
'',
FileStorageHelper::storeFile($this, $input['value'], true)
);
break;
case InputType::selectEnum->value:
case InputType::boolean->value:
case InputType::password->value:
case InputType::email->value:
case InputType::number->value:
case InputType::videoVimeo->value:
case InputType::videoYoutube->value:
case InputType::video->value:
break;
case InputType::string->value:
case InputType::text->value:
case InputType::url->value:
if (!is_null($input['value'])) {
ToolboxHelper::assertNoInternalBoUrls('content', $input['value']);
}
2023-11-13 11:56:06 +01:00
break;
default:
throw new \ErrorException(sprintf(
'Unknown input Type %s',
$input['inputType']
));
} //end switch
} //end foreach
return $data;
}
}