Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 63 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| MiniCrudModel | |
0.00% |
0 / 63 |
|
0.00% |
0 / 9 |
992 | |
0.00% |
0 / 1 |
| fill | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| minicrudCreating | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| minicrudUpdating | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| minicrudUpdated | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| minicrudDeleted | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| setData | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| setDataAttribute | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getDataAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| serializeData | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
240 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Traits\Models; |
| 4 | |
| 5 | use App\Enums\InputType; |
| 6 | use App\Lib\Helpers\FileStorageHelper; |
| 7 | use App\Lib\Helpers\ToolboxHelper; |
| 8 | |
| 9 | trait MiniCrudModel |
| 10 | { |
| 11 | /** |
| 12 | * Fill the model with an array of attributes. |
| 13 | * |
| 14 | * @param array $attributes |
| 15 | * @return void |
| 16 | * @throws \Illuminate\Database\Eloquent\MassAssignmentException If fill prop is not allowed. |
| 17 | * @phpcs:disable Squiz.Commenting.FunctionComment.EmptyThrows |
| 18 | */ |
| 19 | public function fill(array $attributes) |
| 20 | { |
| 21 | // phpcs:enable |
| 22 | parent::fill($attributes); |
| 23 | if (isset($attributes['data'])) { |
| 24 | $this->setData($attributes['data']); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Apply function on data typed text while on save event |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function minicrudCreating() |
| 34 | { |
| 35 | /** @var array<string,array> */ |
| 36 | $data = $this->data; |
| 37 | foreach ($data as &$input) { |
| 38 | switch ($input['inputType']) { |
| 39 | case InputType::text->value: |
| 40 | $input['text'] = clean($input['text']); |
| 41 | $input['text'] = FileStorageHelper::storeModelFilesFromHtml($this, $input['text']); |
| 42 | break; |
| 43 | } //end switch |
| 44 | } //end foreach |
| 45 | $this->data = $data; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Apply function on data typed text while on update event |
| 50 | * |
| 51 | * @return void |
| 52 | */ |
| 53 | public function minicrudUpdating() |
| 54 | { |
| 55 | $this->minicrudCreating(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Apply function on data typed text while on updated event |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public function minicrudUpdated() |
| 64 | { |
| 65 | /** @var array */ |
| 66 | foreach ($this->data as $index => $input) { |
| 67 | switch ($input['inputType']) { |
| 68 | case InputType::text->value: |
| 69 | $original = ToolboxHelper::getOriginal($this, 'data'); |
| 70 | FileStorageHelper::removeOldModelFilesFromHtml( |
| 71 | $input['text'], |
| 72 | $original[$index]['text'] ?? '' |
| 73 | ); |
| 74 | break; |
| 75 | } //end switch |
| 76 | } //end foreach |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Apply function on data typed text while on deleted event |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | public function minicrudDeleted() |
| 85 | { |
| 86 | /** @var array */ |
| 87 | foreach ($this->data as $input) { |
| 88 | switch ($input['inputType']) { |
| 89 | case InputType::text->value: |
| 90 | FileStorageHelper::removeModelFilesOfHtml($input['text']); |
| 91 | break; |
| 92 | } //end switch |
| 93 | } //end foreach |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Set a specific prop |
| 98 | * |
| 99 | * @param array $data |
| 100 | * @return void |
| 101 | */ |
| 102 | private function setData(array $data): void |
| 103 | { |
| 104 | $attrs = $this->attributes; |
| 105 | if (isset($attrs['data'])) { |
| 106 | unset($attrs['data']); |
| 107 | } |
| 108 | $this->setAttribute('data', $data); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Set a specific prop |
| 113 | * |
| 114 | * @param array $value |
| 115 | * @return void |
| 116 | */ |
| 117 | protected function setDataAttribute(array $value): void |
| 118 | { |
| 119 | $this->attributes['data'] = $this->castAttributeAsJson( |
| 120 | 'data', |
| 121 | $this->serializeData($value) |
| 122 | ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Set a specific prop |
| 127 | * |
| 128 | * @param string|null $data |
| 129 | * @return array |
| 130 | */ |
| 131 | protected function getDataAttribute(string|null $data): array |
| 132 | { |
| 133 | return $this->fromJson($data ?? $this->attributes['data'] ?? '[]'); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Serialize properties |
| 138 | * |
| 139 | * @param array $data |
| 140 | * @return array |
| 141 | * @throws \ErrorException If input type is no handled. |
| 142 | * @throws \Illuminate\Validation\ValidationException If url is a bo url. |
| 143 | * @phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber |
| 144 | * @phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh |
| 145 | */ |
| 146 | private function serializeData(array $data): array |
| 147 | { |
| 148 | foreach ($data as $o => $input) { |
| 149 | switch ($input['inputType']) { |
| 150 | case InputType::image->value: |
| 151 | $data[$o]['value'] = \preg_replace( |
| 152 | '/^\/|^/', |
| 153 | '', |
| 154 | FileStorageHelper::storeFile($this, $input['value'], true) |
| 155 | ); |
| 156 | break; |
| 157 | case InputType::selectEnum->value: |
| 158 | case InputType::boolean->value: |
| 159 | case InputType::password->value: |
| 160 | case InputType::email->value: |
| 161 | case InputType::number->value: |
| 162 | case InputType::videoVimeo->value: |
| 163 | case InputType::videoYoutube->value: |
| 164 | case InputType::video->value: |
| 165 | break; |
| 166 | case InputType::string->value: |
| 167 | case InputType::text->value: |
| 168 | case InputType::url->value: |
| 169 | ToolboxHelper::assertNoInternalBoUrls('content', $input['value']); |
| 170 | break; |
| 171 | default: |
| 172 | throw new \ErrorException(sprintf( |
| 173 | 'Unknown input Type %s', |
| 174 | $input['inputType'] |
| 175 | )); |
| 176 | } //end switch |
| 177 | } //end foreach |
| 178 | return $data; |
| 179 | } |
| 180 | } |