setData($attributes['data']); } } /** * 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']); } $data = $this->serializeData($data); $this->setRawAttributes(\array_merge_recursive( $attrs, ['data' => $data] )); } /** * Get data attribute * Cast json_decode to object. * * @return object|null */ protected function getDataAttribute(): ?object { return \json_decode($this->getAttributes()['data'] ?? json_encode($this->getEmptyData())) ?? '[]'; } /** * Get data attribute * Cast json_decode to object. * * @param array $data * @return void */ protected function setDataAttribute(array $data): void { $this->setData($data); } /** * Serialize properties * * @param array $data * @return string * @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): string { foreach ($data as $o => $input) { // Always take 'en' becaue it's the default language. // For others languages, only 'value' will be set, not inputType. $inputType = $input['en']['inputType'] ?? $input['de']['inputType'] ?? $input['inputType']; switch ($inputType) { case InputType::image->value: $data[$o][App::currentLocale()]['value'] = FileStorageHelper::storeFile($this, $input['value'], true); $data[$o][App::currentLocale()]['alt'] = $input['alt']; $data[$o][App::currentLocale()]['title'] = $input['title']; unset($data[$o]['value']); unset($data[$o]['alt']); unset($data[$o]['title']); break; case InputType::boolean->value: case InputType::text->value: $data[$o][App::currentLocale()]['value'] = $input['value']; unset($data[$o]['value']); break; case InputType::number->value: case InputType::string->value: $data[$o][App::currentLocale()]['value'] = $input['value']; unset($data[$o]['value']); break; case InputType::videoVimeo->value: case InputType::url->value: }//end switch } //end foreach return json_encode($data); } }