setData($attributes['data']); } return $this; } /** * Apply function on data typed text while on save event. */ public function minicrudCreating(): void { /** @var 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. */ public function minicrudUpdating(): void { $this->minicrudCreating(); } /** * Apply function on data typed text while on updated event. */ public function minicrudUpdated(): void { /** @var array $input */ 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. */ public function minicrudDeleted(): void { /** @var array */ foreach ($this->data as $input) { switch ($input['inputType']) { case InputType::text->value: FileStorageHelper::removeModelFilesOfHtml($input['text']); break; } // end switch } // end foreach } /** * Set a specific prop. */ private function setData(array $data): void { $attrs = $this->attributes; if (isset($attrs['data'])) { unset($attrs['data']); } $this->setAttribute('data', $data); } /** * Set a specific prop. */ protected function setDataAttribute(array $value): void { $this->attributes['data'] = $this->castAttributeAsJson( 'data', $this->serializeData($value) ); } /** * Set a specific prop. */ protected function getDataAttribute(?string $data): array { return $this->fromJson($data ?? $this->attributes['data'] ?? '[]'); } /** * Serialize properties. * * @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) { if (! isset($input['inputType'])) { continue; } 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: case InputType::string->value: case InputType::text->value: case InputType::url->value: ToolboxHelper::assertNoInternalBoUrls('content', $input['value']); break; case InputType::slider->value: if (! is_array($data[$o]['value'])) { break; } foreach ($data[$o]['value'] as $index => $item) { if (isset($item['picture']) && $item['picture'] instanceof UploadedFile) { $data[$o]['value'][$index]['picture'] = \preg_replace( '/^\/|^/', '', FileStorageHelper::storeFile($this, $item['picture'], true) ); } } break; case InputType::selectImageVideo->value: if (isset($data[$o]['value']['picture']) && $data[$o]['value']['picture']['path'] instanceof UploadedFile) { $data[$o]['value']['picture']['path'] = \preg_replace( '/^\/|^/', '', FileStorageHelper::storeFile($this, $data[$o]['value']['picture']['path'], true) ); } break; default: throw new \ErrorException(sprintf( 'Unknown input Type %s', $input['inputType'] )); } // end switch } // end foreach return $data; } /** * Get fillable properties and their values from the model. */ public function getFillableFields(self $model): array { $datas = []; foreach ($model->toArray() as $property => $value) { if (in_array($property, $model->getFillable())) { $datas = array_merge($datas, [$property => $value]); } } return $datas; } }