getDefaultAttributes(); } if (is_string($value)) { $decoded = json_decode($value, true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { return array_merge($this->getDefaultAttributes(), $decoded); } } if (is_array($value)) { return array_merge($this->getDefaultAttributes(), $value); } return $this->getDefaultAttributes(); } /** * 转换成将要进行存储的值 * * @param Model $model * @param string $key * @param mixed $value * @param array $attributes * @return string */ public function set(Model $model, string $key, mixed $value, array $attributes): string { if (is_null($value)) { return json_encode($this->getDefaultAttributes()); } if (is_array($value)) { return json_encode(array_merge($this->getDefaultAttributes(), $value)); } if (is_string($value)) { $decoded = json_decode($value, true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { return json_encode(array_merge($this->getDefaultAttributes(), $decoded)); } } return json_encode($this->getDefaultAttributes()); } /** * 获取默认显示属性 * * @return array 默认属性数组 */ private function getDefaultAttributes(): array { return [ 'icon' => '⭐', // 默认图标 'color' => '#1890ff', // 默认颜色 'background' => '#f0f9ff', // 默认背景色 'border_color' => '#91d5ff', // 默认边框色 'text_color' => '#003a8c', // 默认文字颜色 'show_in_list' => true, // 是否在列表中显示 'show_in_detail' => true, // 是否在详情中显示 'sort_order' => 0, // 排序顺序 'description' => '', // 描述信息 ]; } }