DynamicModel.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. * Array of the dynamic attribute labels.
  58. * Used to as form field labels and in validation errors.
  59. *
  60. * @see attributeLabels()
  61. * @see setAttributeLabels()
  62. * @see setAttributeLabel()
  63. * @since 2.0.35
  64. */
  65. private $_attributeLabels = [];
  66. /**
  67. * Constructors.
  68. * @param array $attributes the dynamic attributes (name-value pairs, or names) being defined
  69. * @param array $config the configuration array to be applied to this object.
  70. */
  71. public function __construct(array $attributes = [], $config = [])
  72. {
  73. foreach ($attributes as $name => $value) {
  74. if (is_int($name)) {
  75. $this->_attributes[$value] = null;
  76. } else {
  77. $this->_attributes[$name] = $value;
  78. }
  79. }
  80. parent::__construct($config);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function __get($name)
  86. {
  87. if ($this->hasAttribute($name)) {
  88. return $this->_attributes[$name];
  89. }
  90. return parent::__get($name);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function __set($name, $value)
  96. {
  97. if ($this->hasAttribute($name)) {
  98. $this->_attributes[$name] = $value;
  99. } else {
  100. parent::__set($name, $value);
  101. }
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function __isset($name)
  107. {
  108. if ($this->hasAttribute($name)) {
  109. return isset($this->_attributes[$name]);
  110. }
  111. return parent::__isset($name);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function __unset($name)
  117. {
  118. if ($this->hasAttribute($name)) {
  119. unset($this->_attributes[$name]);
  120. } else {
  121. parent::__unset($name);
  122. }
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
  128. {
  129. return parent::canGetProperty($name, $checkVars, $checkBehaviors) || $this->hasAttribute($name);
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
  135. {
  136. return parent::canSetProperty($name, $checkVars, $checkBehaviors) || $this->hasAttribute($name);
  137. }
  138. /**
  139. * Returns a value indicating whether the model has an attribute with the specified name.
  140. * @param string $name the name of the attribute
  141. * @return bool whether the model has an attribute with the specified name.
  142. * @since 2.0.16
  143. */
  144. public function hasAttribute($name)
  145. {
  146. return array_key_exists($name, $this->_attributes);
  147. }
  148. /**
  149. * Defines an attribute.
  150. * @param string $name the attribute name
  151. * @param mixed $value the attribute value
  152. */
  153. public function defineAttribute($name, $value = null)
  154. {
  155. $this->_attributes[$name] = $value;
  156. }
  157. /**
  158. * Undefines an attribute.
  159. * @param string $name the attribute name
  160. */
  161. public function undefineAttribute($name)
  162. {
  163. unset($this->_attributes[$name]);
  164. }
  165. /**
  166. * Adds a validation rule to this model.
  167. * You can also directly manipulate [[validators]] to add or remove validation rules.
  168. * This method provides a shortcut.
  169. * @param string|array $attributes the attribute(s) to be validated by the rule
  170. * @param string|Validator|\Closure $validator the validator. This can be either:
  171. * * a built-in validator name listed in [[builtInValidators]];
  172. * * a method name of the model class;
  173. * * an anonymous function;
  174. * * a validator class name.
  175. * * a Validator.
  176. * @param array $options the options (name-value pairs) to be applied to the validator
  177. * @return $this the model itself
  178. */
  179. public function addRule($attributes, $validator, $options = [])
  180. {
  181. $validators = $this->getValidators();
  182. if ($validator instanceof Validator) {
  183. $validator->attributes = (array)$attributes;
  184. } else {
  185. $validator = Validator::createValidator($validator, $this, (array)$attributes, $options);
  186. }
  187. $validators->append($validator);
  188. return $this;
  189. }
  190. /**
  191. * Validates the given data with the specified validation rules.
  192. * This method will create a DynamicModel instance, populate it with the data to be validated,
  193. * create the specified validation rules, and then validate the data using these rules.
  194. * @param array $data the data (name-value pairs) to be validated
  195. * @param array $rules the validation rules. Please refer to [[Model::rules()]] on the format of this parameter.
  196. * @return static the model instance that contains the data being validated
  197. * @throws InvalidConfigException if a validation rule is not specified correctly.
  198. */
  199. public static function validateData(array $data, $rules = [])
  200. {
  201. /* @var $model DynamicModel */
  202. $model = new static($data);
  203. if (!empty($rules)) {
  204. $validators = $model->getValidators();
  205. foreach ($rules as $rule) {
  206. if ($rule instanceof Validator) {
  207. $validators->append($rule);
  208. } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
  209. $validator = Validator::createValidator($rule[1], $model, (array)$rule[0], array_slice($rule, 2));
  210. $validators->append($validator);
  211. } else {
  212. throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
  213. }
  214. }
  215. }
  216. $model->validate();
  217. return $model;
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function attributes()
  223. {
  224. return array_keys($this->_attributes);
  225. }
  226. /**
  227. * Sets the attribute labels in a massive way.
  228. *
  229. * @see attributeLabels()
  230. * @see $_attributeLabels
  231. * @since 2.0.35
  232. *
  233. * @param array $labels Array of attribute labels
  234. * @return $this
  235. */
  236. public function setAttributeLabels(array $labels = [])
  237. {
  238. $this->_attributeLabels = $labels;
  239. return $this;
  240. }
  241. /**
  242. * Sets a label for an attribute.
  243. *
  244. * @see attributeLabels()
  245. * @see $_attributeLabels
  246. * @since 2.0.35
  247. *
  248. * @param string $attribute Attribute name
  249. * @param string $label Attribute label value
  250. * @return $this
  251. */
  252. public function setAttributeLabel($attribute, $label)
  253. {
  254. $this->_attributeLabels[$attribute] = $label;
  255. return $this;
  256. }
  257. /**
  258. * {@inheritDoc}
  259. */
  260. public function attributeLabels()
  261. {
  262. return array_merge(parent::attributeLabels(), $this->_attributeLabels);
  263. }
  264. }