ldc-nature-and-respect/app/Traits/Models/MiniCrudModel.php

181 lines
5.2 KiB
PHP
Raw Permalink Normal View History

2023-03-24 15:39:11 +00:00
<?php
namespace App\Traits\Models;
use App\Enums\InputType;
use App\Lib\Helpers\FileStorageHelper;
2023-05-09 09:14:21 +02:00
use App\Lib\Helpers\ToolboxHelper;
2023-03-24 15:39:11 +00:00
trait MiniCrudModel
{
/**
* Fill the model with an array of attributes.
*
2023-05-09 09:14:21 +02:00
* @param array $attributes
2024-01-29 16:02:03 +01:00
* @phpstan-ignore-next-line
2023-03-24 15:39:11 +00:00
* @return void
* @throws \Illuminate\Database\Eloquent\MassAssignmentException If fill prop is not allowed.
2023-05-09 09:14:21 +02:00
* @phpcs:disable Squiz.Commenting.FunctionComment.EmptyThrows
2023-03-24 15:39:11 +00:00
*/
public function fill(array $attributes)
{
2023-05-09 09:14:21 +02:00
// phpcs:enable
2023-03-24 15:39:11 +00:00
parent::fill($attributes);
if (isset($attributes['data'])) {
$this->setData($attributes['data']);
}
}
/**
2023-05-09 09:14:21 +02:00
* Apply function on data typed text while on save event
2023-03-24 15:39:11 +00:00
*
* @return void
*/
2023-05-09 09:14:21 +02:00
public function minicrudCreating()
2023-03-24 15:39:11 +00:00
{
2023-05-09 09:14:21 +02:00
/** @var array<string,array> */
$data = $this->data;
foreach ($data as &$input) {
2023-05-09 13:33:26 +02:00
switch ($input['inputType']) {
2023-05-09 09:14:21 +02:00
case InputType::text->value:
$input['text'] = clean($input['text']);
$input['text'] = FileStorageHelper::storeModelFilesFromHtml($this, $input['text']);
break;
} //end switch
} //end foreach
$this->data = $data;
2023-03-24 15:39:11 +00:00
}
/**
2023-05-09 09:14:21 +02:00
* Apply function on data typed text while on update event
2023-03-24 15:39:11 +00:00
*
2023-05-09 09:14:21 +02:00
* @return void
2023-03-24 15:39:11 +00:00
*/
2023-05-09 09:14:21 +02:00
public function minicrudUpdating()
2023-03-24 15:39:11 +00:00
{
2023-05-09 09:14:21 +02:00
$this->minicrudCreating();
2023-03-24 15:39:11 +00:00
}
/**
2023-05-09 09:14:21 +02:00
* Apply function on data typed text while on updated event
*
* @return void
*/
public function minicrudUpdated()
{
foreach ($this->data as $index => $input) {
2023-05-09 13:33:26 +02:00
switch ($input['inputType']) {
2023-05-09 09:14:21 +02:00
case InputType::text->value:
2023-05-09 19:24:09 +02:00
$original = ToolboxHelper::getOriginal($this, 'data');
2023-05-09 09:14:21 +02:00
FileStorageHelper::removeOldModelFilesFromHtml(
$input['text'],
2023-05-09 19:24:09 +02:00
$original[$index]['text'] ?? ''
2023-05-09 09:14:21 +02:00
);
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) {
2023-05-09 13:33:26 +02:00
switch ($input['inputType']) {
2023-05-09 09:14:21 +02:00
case InputType::text->value:
FileStorageHelper::removeModelFilesOfHtml($input['text']);
break;
} //end switch
} //end foreach
}
/**
* Set a specific prop
2023-03-24 15:39:11 +00:00
*
* @param array $data
* @return void
*/
2023-05-09 09:14:21 +02:00
private function setData(array $data): void
2023-03-24 15:39:11 +00:00
{
2023-05-09 09:14:21 +02:00
$attrs = $this->attributes;
if (isset($attrs['data'])) {
unset($attrs['data']);
}
$this->setAttribute('data', $data);
2023-03-24 15:39:11 +00:00
}
2023-05-09 13:33:26 +02:00
/**
* Set a specific prop
*
* @param array $value
* @return void
*/
protected function setDataAttribute(array $value): void
{
$this->attributes['data'] = $this->castAttributeAsJson(
'data',
$this->serializeData($value)
);
}
/**
* Set a specific prop
*
* @param string|null $data
* @return array
*/
protected function getDataAttribute(string|null $data): array
{
return $this->fromJson($data ?? $this->attributes['data'] ?? '[]');
}
2023-03-24 15:39:11 +00:00
/**
* Serialize properties
*
* @param array $data
2023-05-09 09:14:21 +02:00
* @return array
2023-03-24 15:39:11 +00: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
*/
2023-05-09 09:14:21 +02:00
private function serializeData(array $data): array
2023-03-24 15:39:11 +00:00
{
foreach ($data as $o => $input) {
2023-05-09 13:33:26 +02:00
switch ($input['inputType']) {
2023-03-24 15:39:11 +00:00
case InputType::image->value:
2023-05-09 09:14:21 +02:00
$data[$o]['value'] = \preg_replace(
'/^\/|^/',
'',
FileStorageHelper::storeFile($this, $input['value'], true)
2023-05-09 09:14:21 +02:00
);
2023-03-24 15:39:11 +00:00
break;
2023-05-09 13:33:26 +02:00
case InputType::selectEnum->value:
2023-03-24 15:39:11 +00:00
case InputType::boolean->value:
2023-05-09 09:14:21 +02:00
case InputType::password->value:
case InputType::email->value:
2023-03-24 15:39:11 +00:00
case InputType::number->value:
case InputType::videoVimeo->value:
case InputType::videoYoutube->value:
case InputType::video->value:
2023-05-09 09:14:21 +02:00
break;
case InputType::string->value:
case InputType::text->value:
2023-03-24 15:39:11 +00:00
case InputType::url->value:
2023-06-13 13:10:57 +02:00
ToolboxHelper::assertNoInternalBoUrls('content', $input['value'] ?? '');
2023-05-09 09:14:21 +02:00
break;
default:
throw new \ErrorException(sprintf(
'Unknown input Type %s',
2023-05-09 13:33:26 +02:00
$input['inputType']
2023-05-09 09:14:21 +02:00
));
} //end switch
2023-03-24 15:39:11 +00:00
} //end foreach
2023-05-09 09:14:21 +02:00
return $data;
2023-03-24 15:39:11 +00:00
}
}