| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace App\Module\Point\Casts;
- use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
- use Illuminate\Database\Eloquent\Model;
- /**
- * 积分类型显示属性Cast类
- *
- * 用于处理积分类型表中的显示属性字段,将JSON数据转换为数组
- */
- class PointCurrencyDisplayAttributesCast implements CastsAttributes
- {
- /**
- * 将取出的数据进行转换
- *
- * @param Model $model
- * @param string $key
- * @param mixed $value
- * @param array $attributes
- * @return array
- */
- public function get(Model $model, string $key, mixed $value, array $attributes): array
- {
- if (is_null($value)) {
- return $this->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' => '#52c41a', // 默认颜色
- 'background' => '#f6ffed', // 默认背景色
- 'border_color' => '#b7eb8f', // 默认边框色
- 'text_color' => '#135200', // 默认文字颜色
- 'unit' => '积分', // 单位
- 'show_in_header' => true, // 是否在头部显示
- 'show_in_sidebar' => true, // 是否在侧边栏显示
- 'enable_transfer' => true, // 是否允许转账
- 'enable_exchange' => true, // 是否允许兑换
- 'min_transfer' => 1, // 最小转账数量
- 'max_transfer' => 999999, // 最大转账数量
- 'sort_order' => 0, // 排序顺序
- 'description' => '', // 描述信息
- ];
- }
- }
|