You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.3KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. use yii\validators\Validator;
  9. /**
  10. * DynamicModel is a model class primarily used to support ad hoc data validation.
  11. *
  12. * The typical usage of DynamicModel is as follows,
  13. *
  14. * ```php
  15. * public function actionSearch($name, $email)
  16. * {
  17. * $model = DynamicModel::validateData(compact('name', 'email'), [
  18. * [['name', 'email'], 'string', 'max' => 128],
  19. * ['email', 'email'],
  20. * ]);
  21. * if ($model->hasErrors()) {
  22. * // validation fails
  23. * } else {
  24. * // validation succeeds
  25. * }
  26. * }
  27. * ```
  28. *
  29. * The above example shows how to validate `$name` and `$email` with the help of DynamicModel.
  30. * The [[validateData()]] method creates an instance of DynamicModel, defines the attributes
  31. * using the given data (`name` and `email` in this example), and then calls [[Model::validate()]].
  32. *
  33. * You can check the validation result by [[hasErrors()]], like you do with a normal model.
  34. * You may also access the dynamic attributes defined through the model instance, e.g.,
  35. * `$model->name` and `$model->email`.
  36. *
  37. * Alternatively, you may use the following more "classic" syntax to perform ad-hoc data validation:
  38. *
  39. * ```php
  40. * $model = new DynamicModel(compact('name', 'email'));
  41. * $model->addRule(['name', 'email'], 'string', ['max' => 128])
  42. * ->addRule('email', 'email')
  43. * ->validate();
  44. * ```
  45. *
  46. * DynamicModel implements the above ad-hoc data validation feature by supporting the so-called
  47. * "dynamic attributes". It basically allows an attribute to be defined dynamically through its constructor
  48. * or [[defineAttribute()]].
  49. *
  50. * @author Qiang Xue <qiang.xue@gmail.com>
  51. * @since 2.0
  52. */
  53. class DynamicModel extends Model
  54. {
  55. private $_attributes = [];
  56. /**
  57. * Constructors.
  58. * @param array $attributes the dynamic attributes (name-value pairs, or names) being defined
  59. * @param array $config the configuration array to be applied to this object.
  60. */
  61. public function __construct(array $attributes = [], $config = [])
  62. {
  63. foreach ($attributes as $name => $value) {
  64. if (is_int($name)) {
  65. $this->_attributes[$value] = null;
  66. } else {
  67. $this->_attributes[$name] = $value;
  68. }
  69. }
  70. parent::__construct($config);
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function __get($name)
  76. {
  77. if (array_key_exists($name, $this->_attributes)) {
  78. return $this->_attributes[$name];
  79. } else {
  80. return parent::__get($name);
  81. }
  82. }
  83. /**
  84. * @inheritdoc
  85. */
  86. public function __set($name, $value)
  87. {
  88. if (array_key_exists($name, $this->_attributes)) {
  89. $this->_attributes[$name] = $value;
  90. } else {
  91. parent::__set($name, $value);
  92. }
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function __isset($name)
  98. {
  99. if (array_key_exists($name, $this->_attributes)) {
  100. return isset($this->_attributes[$name]);
  101. } else {
  102. return parent::__isset($name);
  103. }
  104. }
  105. /**
  106. * @inheritdoc
  107. */
  108. public function __unset($name)
  109. {
  110. if (array_key_exists($name, $this->_attributes)) {
  111. unset($this->_attributes[$name]);
  112. } else {
  113. parent::__unset($name);
  114. }
  115. }
  116. /**
  117. * Defines an attribute.
  118. * @param string $name the attribute name
  119. * @param mixed $value the attribute value
  120. */
  121. public function defineAttribute($name, $value = null)
  122. {
  123. $this->_attributes[$name] = $value;
  124. }
  125. /**
  126. * Undefines an attribute.
  127. * @param string $name the attribute name
  128. */
  129. public function undefineAttribute($name)
  130. {
  131. unset($this->_attributes[$name]);
  132. }
  133. /**
  134. * Adds a validation rule to this model.
  135. * You can also directly manipulate [[validators]] to add or remove validation rules.
  136. * This method provides a shortcut.
  137. * @param string|array $attributes the attribute(s) to be validated by the rule
  138. * @param mixed $validator the validator for the rule.This can be a built-in validator name,
  139. * a method name of the model class, an anonymous function, or a validator class name.
  140. * @param array $options the options (name-value pairs) to be applied to the validator
  141. * @return $this the model itself
  142. */
  143. public function addRule($attributes, $validator, $options = [])
  144. {
  145. $validators = $this->getValidators();
  146. $validators->append(Validator::createValidator($validator, $this, (array) $attributes, $options));
  147. return $this;
  148. }
  149. /**
  150. * Validates the given data with the specified validation rules.
  151. * This method will create a DynamicModel instance, populate it with the data to be validated,
  152. * create the specified validation rules, and then validate the data using these rules.
  153. * @param array $data the data (name-value pairs) to be validated
  154. * @param array $rules the validation rules. Please refer to [[Model::rules()]] on the format of this parameter.
  155. * @return static the model instance that contains the data being validated
  156. * @throws InvalidConfigException if a validation rule is not specified correctly.
  157. */
  158. public static function validateData(array $data, $rules = [])
  159. {
  160. /* @var $model DynamicModel */
  161. $model = new static($data);
  162. if (!empty($rules)) {
  163. $validators = $model->getValidators();
  164. foreach ($rules as $rule) {
  165. if ($rule instanceof Validator) {
  166. $validators->append($rule);
  167. } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
  168. $validator = Validator::createValidator($rule[1], $model, (array) $rule[0], array_slice($rule, 2));
  169. $validators->append($validator);
  170. } else {
  171. throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
  172. }
  173. }
  174. }
  175. $model->validate();
  176. return $model;
  177. }
  178. /**
  179. * @inheritdoc
  180. */
  181. public function attributes()
  182. {
  183. return array_keys($this->_attributes);
  184. }
  185. }