/** * 修改属性值并把被修改的属性标记为被修改过的状态 * * @param string $key * @param mixed $value * @param array $attribute * @return void */ protected final function change($key, $value, array $attribute = null) { $attribute = $attribute ?: static::getMapper()->getAttribute($key); $type = Type::factory($attribute['type']); if (array_key_exists($key, $this->values)) { if ($value === $this->values[$key]) { return; } elseif ($type->isNull($value) && $type->isNull($this->values[$key])) { return; } } elseif ($type->isNull($value) && $attribute['allow_null']) { return; } $this->values[$key] = $value; $this->dirty[$key] = true; }
protected function getType($name) { return DataMapper\Type::getInstance()->get($name); }
/** * 格式化属性值 * 可以通过重载此方法实现自定义格式化逻辑 * * @param string $key 属性名 * @param mixed $value 属性值 * @param array $attribute 属性定义信息 * @return mixed 格式化过后的值 */ protected function normalize($key, $value, array $attribute) { return Type::factory($attribute['type'])->normalize($value, $attribute); }
/** * 格式化从Data class获得的配置信息 * * @param array $options * @return array */ protected function normalizeOptions(array $options) { $options = array_merge(['service' => null, 'collection' => null, 'attributes' => [], 'readonly' => false, 'strict' => false], $options); $primary_key = []; foreach ($options['attributes'] as $key => $attribute) { $attribute = Type::normalizeAttribute($attribute); if ($attribute['strict'] === null) { $attribute['strict'] = $options['strict']; } if ($attribute['primary_key'] && !$attribute['deprecated']) { $primary_key[] = $key; } $options['attributes'][$key] = $attribute; } if (!$primary_key) { throw new \RuntimeException('Mapper: undefined primary key'); } $options['primary_key'] = $primary_key; return $options; }