ldc-nature-eleveurs/app/Traits/Models/MiniCrudModel.php

168 lines
4.6 KiB
PHP
Raw Permalink Normal View History

2022-08-09 12:29:21 +00:00
<?php
namespace App\Traits\Models;
use App\Enums\InputType;
2024-01-31 16:42:01 +01:00
use App\Lib\Helpers\FileStorageHelper;
use App\Lib\Helpers\ToolboxHelper;
2022-08-09 12:29:21 +00:00
trait MiniCrudModel
{
/**
* Fill the model with an array of attributes.
*
* @param array $attributes
* @return void
2024-02-01 10:19:11 +01:00
* @phpstan-ignore-next-line
2022-08-09 12:29:21 +00:00
*/
2024-02-01 10:19:11 +01:00
public function fill(array $attributes): void
2022-08-09 12:29:21 +00:00
{
parent::fill($attributes);
if (isset($attributes['data'])) {
$this->setData($attributes['data']);
}
}
2024-01-31 16:42:01 +01:00
/**
* Apply function on data typed text while on save event
*
* @return void
*/
2024-02-01 10:19:11 +01:00
public function minicrudCreating(): void
2024-01-31 16:42:01 +01:00
{
/** @var array<string,array> */
$data = $this->data;
foreach ($data as &$input) {
switch ($input['inputType']) {
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
*/
2024-02-01 10:19:11 +01:00
public function minicrudUpdating(): void
2024-01-31 16:42:01 +01:00
{
$this->minicrudCreating();
}
/**
* Apply function on data typed text while on updated event
*
* @return void
*/
2024-02-01 10:19:11 +01:00
public function minicrudUpdated(): void
2024-01-31 16:42:01 +01:00
{
foreach ($this->data as $index => $input) {
switch ($input['inputType']) {
case InputType::text->value:
$original = ToolboxHelper::getOriginal($this, 'data');
FileStorageHelper::removeOldModelFilesFromHtml(
$input['text'],
$original[$index]['text'] ?? ''
);
break;
} //end switch
} //end foreach
}
/**
* Apply function on data typed text while on deleted event
*
* @return void
*/
2024-02-01 10:19:11 +01:00
public function minicrudDeleted(): void
2024-01-31 16:42:01 +01:00
{
/** @var array */
foreach ($this->data as $input) {
switch ($input['inputType']) {
case InputType::text->value:
FileStorageHelper::removeModelFilesOfHtml($input['text']);
break;
} //end switch
} //end foreach
}
2022-08-09 12:29:21 +00:00
/**
* 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']);
}
2024-01-31 16:42:01 +01:00
$this->setAttribute('data', $data);
2022-08-09 12:29:21 +00:00
}
/**
2024-01-31 16:42:01 +01:00
* Set a specific prop
2022-08-09 12:29:21 +00:00
*
2024-01-31 16:42:01 +01:00
* @param array $value
* @return void
2022-08-09 12:29:21 +00:00
*/
2024-01-31 16:42:01 +01:00
protected function setDataAttribute(array $value): void
2022-08-09 12:29:21 +00:00
{
2024-01-31 16:42:01 +01:00
$this->attributes['data'] = $this->castAttributeAsJson(
'data',
$this->serializeData($value)
);
2022-08-09 12:29:21 +00:00
}
/**
2024-01-31 16:42:01 +01:00
* Set a specific prop
2022-08-09 12:29:21 +00:00
*
2024-01-31 16:42:01 +01:00
* @param string|null $data
* @return array
2022-08-09 12:29:21 +00:00
*/
2024-01-31 16:42:01 +01:00
protected function getDataAttribute(string|null $data): array
2022-08-09 12:29:21 +00:00
{
2024-01-31 16:42:01 +01:00
return $this->fromJson($data ?? $this->attributes['data'] ?? '[]');
2022-08-09 12:29:21 +00:00
}
/**
* Serialize properties
*
* @param array $data
2024-01-31 16:42:01 +01:00
* @return array
2022-08-09 12:29:21 +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
*/
2024-01-31 16:42:01 +01:00
private function serializeData(array $data): array
2022-08-09 12:29:21 +00:00
{
foreach ($data as $o => $input) {
switch ($input['inputType']->value ?? $input['inputType']['value']) {
2024-01-31 16:42:01 +01:00
case InputType::image->value:
2024-01-25 10:38:35 +01:00
$data[$o]['value'] = \preg_replace(
'/^\/|^/',
'',
FileStorageHelper::storeFile($this, $input['value'], true)
);
2022-08-09 12:29:21 +00:00
break;
2024-01-31 16:42:01 +01:00
case InputType::string->value:
break;
2024-01-31 16:42:01 +01:00
case InputType::text->value:
2022-08-09 12:29:21 +00:00
break;
default:
throw new \ErrorException(sprintf(
'Unknown input Type %s',
$input['inputType']->label ?? $input['inputType']['label']
2022-08-09 12:29:21 +00:00
));
2024-01-31 16:42:01 +01:00
} //end switch
2022-08-09 12:29:21 +00:00
} //end foreach
2024-01-31 16:42:01 +01:00
return $data;
2022-08-09 12:29:21 +00:00
}
}