'请提交配置项'], ['items', 'validateItems'], ]; } public function attributeLabels() { return [ 'items' => '配置项', ]; } public function validateItems($attribute) { $items = $this->$attribute; if (is_string($items)) { $items = json_decode($items, true); } if (!is_array($items)) { $this->addError($attribute, '配置项格式错误'); return; } $defaultKeys = HomePageConfigClass::DEFAULT_KEYS; if (count($items) !== count($defaultKeys)) { $this->addError($attribute, '配置项数量不正确'); return; } $result = []; $used = []; foreach ($items as $item) { if (!is_array($item)) { $this->addError($attribute, '配置项格式错误'); return; } $key = isset($item['key']) ? strval($item['key']) : ''; if ($key === '' || !in_array($key, $defaultKeys, true)) { $this->addError($attribute, '存在无效配置项'); return; } if (isset($used[$key])) { $this->addError($attribute, '配置项重复'); return; } $result[] = [ 'key' => $key, 'enabled' => !empty($item['enabled']) ? 1 : 0, ]; $used[$key] = true; } if (count($used) !== count($defaultKeys)) { $this->addError($attribute, '配置项不完整'); return; } $this->$attribute = $result; } /** * @return array */ public function getItems() { return is_array($this->items) ? $this->items : []; } }