|
- <?php
-
-
- namespace yii\base;
-
- use Yii;
- use ArrayAccess;
- use ArrayObject;
- use ArrayIterator;
- use ReflectionClass;
- use IteratorAggregate;
- use yii\helpers\Inflector;
- use yii\validators\RequiredValidator;
- use yii\validators\Validator;
-
-
- class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayable
- {
- use ArrayableTrait;
-
-
-
- const SCENARIO_DEFAULT = 'default';
-
-
- const EVENT_BEFORE_VALIDATE = 'beforeValidate';
-
-
- const EVENT_AFTER_VALIDATE = 'afterValidate';
-
-
-
- private $_errors;
-
-
- private $_validators;
-
-
- private $_scenario = self::SCENARIO_DEFAULT;
-
-
-
-
- public function rules()
- {
- return [];
- }
-
-
-
- public function scenarios()
- {
- $scenarios = [self::SCENARIO_DEFAULT => []];
- foreach ($this->getValidators() as $validator) {
- foreach ($validator->on as $scenario) {
- $scenarios[$scenario] = [];
- }
- foreach ($validator->except as $scenario) {
- $scenarios[$scenario] = [];
- }
- }
- $names = array_keys($scenarios);
-
- foreach ($this->getValidators() as $validator) {
- if (empty($validator->on) && empty($validator->except)) {
- foreach ($names as $name) {
- foreach ($validator->attributes as $attribute) {
- $scenarios[$name][$attribute] = true;
- }
- }
- } elseif (empty($validator->on)) {
- foreach ($names as $name) {
- if (!in_array($name, $validator->except, true)) {
- foreach ($validator->attributes as $attribute) {
- $scenarios[$name][$attribute] = true;
- }
- }
- }
- } else {
- foreach ($validator->on as $name) {
- foreach ($validator->attributes as $attribute) {
- $scenarios[$name][$attribute] = true;
- }
- }
- }
- }
-
- foreach ($scenarios as $scenario => $attributes) {
- if (!empty($attributes)) {
- $scenarios[$scenario] = array_keys($attributes);
- }
- }
-
- return $scenarios;
- }
-
-
-
- public function formName()
- {
- $reflector = new ReflectionClass($this);
- return $reflector->getShortName();
- }
-
-
-
- public function attributes()
- {
- $class = new ReflectionClass($this);
- $names = [];
- foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
- if (!$property->isStatic()) {
- $names[] = $property->getName();
- }
- }
-
- return $names;
- }
-
-
-
- public function attributeLabels()
- {
- return [];
- }
-
-
-
- public function attributeHints()
- {
- return [];
- }
-
-
-
- public function validate($attributeNames = null, $clearErrors = true)
- {
- if ($clearErrors) {
- $this->clearErrors();
- }
-
- if (!$this->beforeValidate()) {
- return false;
- }
-
- $scenarios = $this->scenarios();
- $scenario = $this->getScenario();
- if (!isset($scenarios[$scenario])) {
- throw new InvalidParamException("Unknown scenario: $scenario");
- }
-
- if ($attributeNames === null) {
- $attributeNames = $this->activeAttributes();
- }
-
- foreach ($this->getActiveValidators() as $validator) {
- $validator->validateAttributes($this, $attributeNames);
- }
- $this->afterValidate();
-
- return !$this->hasErrors();
- }
-
-
-
- public function beforeValidate()
- {
- $event = new ModelEvent;
- $this->trigger(self::EVENT_BEFORE_VALIDATE, $event);
-
- return $event->isValid;
- }
-
-
-
- public function afterValidate()
- {
- $this->trigger(self::EVENT_AFTER_VALIDATE);
- }
-
-
-
- public function getValidators()
- {
- if ($this->_validators === null) {
- $this->_validators = $this->createValidators();
- }
- return $this->_validators;
- }
-
-
-
- public function getActiveValidators($attribute = null)
- {
- $validators = [];
- $scenario = $this->getScenario();
- foreach ($this->getValidators() as $validator) {
- if ($validator->isActive($scenario) && ($attribute === null || in_array($attribute, $validator->attributes, true))) {
- $validators[] = $validator;
- }
- }
- return $validators;
- }
-
-
-
- public function createValidators()
- {
- $validators = new ArrayObject;
- foreach ($this->rules() as $rule) {
- if ($rule instanceof Validator) {
- $validators->append($rule);
- } elseif (is_array($rule) && isset($rule[0], $rule[1])) {
- $validator = Validator::createValidator($rule[1], $this, (array) $rule[0], array_slice($rule, 2));
- $validators->append($validator);
- } else {
- throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
- }
- }
- return $validators;
- }
-
-
-
- public function isAttributeRequired($attribute)
- {
- foreach ($this->getActiveValidators($attribute) as $validator) {
- if ($validator instanceof RequiredValidator && $validator->when === null) {
- return true;
- }
- }
- return false;
- }
-
-
-
- public function isAttributeSafe($attribute)
- {
- return in_array($attribute, $this->safeAttributes(), true);
- }
-
-
-
- public function isAttributeActive($attribute)
- {
- return in_array($attribute, $this->activeAttributes(), true);
- }
-
-
-
- public function getAttributeLabel($attribute)
- {
- $labels = $this->attributeLabels();
- return isset($labels[$attribute]) ? $labels[$attribute] : $this->generateAttributeLabel($attribute);
- }
-
-
-
- public function getAttributeHint($attribute)
- {
- $hints = $this->attributeHints();
- return isset($hints[$attribute]) ? $hints[$attribute] : '';
- }
-
-
-
- public function hasErrors($attribute = null)
- {
- return $attribute === null ? !empty($this->_errors) : isset($this->_errors[$attribute]);
- }
-
-
-
- public function getErrors($attribute = null)
- {
- if ($attribute === null) {
- return $this->_errors === null ? [] : $this->_errors;
- } else {
- return isset($this->_errors[$attribute]) ? $this->_errors[$attribute] : [];
- }
- }
-
-
-
- public function getFirstErrors()
- {
- if (empty($this->_errors)) {
- return [];
- } else {
- $errors = [];
- foreach ($this->_errors as $name => $es) {
- if (!empty($es)) {
- $errors[$name] = reset($es);
- }
- }
-
- return $errors;
- }
- }
-
-
-
- public function getFirstError($attribute)
- {
- return isset($this->_errors[$attribute]) ? reset($this->_errors[$attribute]) : null;
- }
-
-
-
- public function addError($attribute, $error = '')
- {
- $this->_errors[$attribute][] = $error;
- }
-
-
-
- public function addErrors(array $items)
- {
- foreach ($items as $attribute => $errors) {
- if (is_array($errors)) {
- foreach ($errors as $error) {
- $this->addError($attribute, $error);
- }
- } else {
- $this->addError($attribute, $errors);
- }
- }
- }
-
-
-
- public function clearErrors($attribute = null)
- {
- if ($attribute === null) {
- $this->_errors = [];
- } else {
- unset($this->_errors[$attribute]);
- }
- }
-
-
-
- public function generateAttributeLabel($name)
- {
- return Inflector::camel2words($name, true);
- }
-
-
-
- public function getAttributes($names = null, $except = [])
- {
- $values = [];
- if ($names === null) {
- $names = $this->attributes();
- }
- foreach ($names as $name) {
- $values[$name] = $this->$name;
- }
- foreach ($except as $name) {
- unset($values[$name]);
- }
-
- return $values;
- }
-
-
-
- public function setAttributes($values, $safeOnly = true)
- {
- if (is_array($values)) {
- $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());
- foreach ($values as $name => $value) {
- if (isset($attributes[$name])) {
- $this->$name = $value;
- } elseif ($safeOnly) {
- $this->onUnsafeAttribute($name, $value);
- }
- }
- }
- }
-
-
-
- public function onUnsafeAttribute($name, $value)
- {
- if (YII_DEBUG) {
- Yii::trace("Failed to set unsafe attribute '$name' in '" . get_class($this) . "'.", __METHOD__);
- }
- }
-
-
-
- public function getScenario()
- {
- return $this->_scenario;
- }
-
-
-
- public function setScenario($value)
- {
- $this->_scenario = $value;
- }
-
-
-
- public function safeAttributes()
- {
- $scenario = $this->getScenario();
- $scenarios = $this->scenarios();
- if (!isset($scenarios[$scenario])) {
- return [];
- }
- $attributes = [];
- foreach ($scenarios[$scenario] as $attribute) {
- if ($attribute[0] !== '!' && !in_array('!' . $attribute, $scenarios[$scenario])) {
- $attributes[] = $attribute;
- }
- }
-
- return $attributes;
- }
-
-
-
- public function activeAttributes()
- {
- $scenario = $this->getScenario();
- $scenarios = $this->scenarios();
- if (!isset($scenarios[$scenario])) {
- return [];
- }
- $attributes = $scenarios[$scenario];
- foreach ($attributes as $i => $attribute) {
- if ($attribute[0] === '!') {
- $attributes[$i] = substr($attribute, 1);
- }
- }
-
- return $attributes;
- }
-
-
-
- public function load($data, $formName = null)
- {
- $scope = $formName === null ? $this->formName() : $formName;
- if ($scope === '' && !empty($data)) {
- $this->setAttributes($data);
-
- return true;
- } elseif (isset($data[$scope])) {
- $this->setAttributes($data[$scope]);
-
- return true;
- } else {
- return false;
- }
- }
-
-
-
- public static function loadMultiple($models, $data, $formName = null)
- {
- if ($formName === null) {
-
- $first = reset($models);
- if ($first === false) {
- return false;
- }
- $formName = $first->formName();
- }
-
- $success = false;
- foreach ($models as $i => $model) {
-
- if ($formName == '') {
- if (!empty($data[$i])) {
- $model->load($data[$i], '');
- $success = true;
- }
- } elseif (!empty($data[$formName][$i])) {
- $model->load($data[$formName][$i], '');
- $success = true;
- }
- }
-
- return $success;
- }
-
-
-
- public static function validateMultiple($models, $attributeNames = null)
- {
- $valid = true;
-
- foreach ($models as $model) {
- $valid = $model->validate($attributeNames) && $valid;
- }
-
- return $valid;
- }
-
-
-
- public function fields()
- {
- $fields = $this->attributes();
-
- return array_combine($fields, $fields);
- }
-
-
-
- public function getIterator()
- {
- $attributes = $this->getAttributes();
- return new ArrayIterator($attributes);
- }
-
-
-
- public function offsetExists($offset)
- {
- return isset($this->$offset);
- }
-
-
-
- public function offsetGet($offset)
- {
- return $this->$offset;
- }
-
-
-
- public function offsetSet($offset, $item)
- {
- $this->$offset = $item;
- }
-
-
-
- public function offsetUnset($offset)
- {
- $this->$offset = null;
- }
- }
|