GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 6886d6...1496c8 )
by Robert
15:10
created

Model::activeAttributes()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
ccs 12
cts 12
cp 1
cc 4
eloc 10
nc 4
nop 0
crap 4
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
8
namespace yii\base;
9
10
use Yii;
11
use ArrayAccess;
12
use ArrayObject;
13
use ArrayIterator;
14
use ReflectionClass;
15
use IteratorAggregate;
16
use yii\helpers\Inflector;
17
use yii\validators\RequiredValidator;
18
use yii\validators\Validator;
19
20
/**
21
 * Model is the base class for data models.
22
 *
23
 * Model implements the following commonly used features:
24
 *
25
 * - attribute declaration: by default, every public class member is considered as
26
 *   a model attribute
27
 * - attribute labels: each attribute may be associated with a label for display purpose
28
 * - massive attribute assignment
29
 * - scenario-based validation
30
 *
31
 * Model also raises the following events when performing data validation:
32
 *
33
 * - [[EVENT_BEFORE_VALIDATE]]: an event raised at the beginning of [[validate()]]
34
 * - [[EVENT_AFTER_VALIDATE]]: an event raised at the end of [[validate()]]
35
 *
36
 * You may directly use Model to store model data, or extend it with customization.
37
 *
38
 * @property \yii\validators\Validator[] $activeValidators The validators applicable to the current
39
 * [[scenario]]. This property is read-only.
40
 * @property array $attributes Attribute values (name => value).
41
 * @property array $errors An array of errors for all attributes. Empty array is returned if no error. The
42
 * result is a two-dimensional array. See [[getErrors()]] for detailed description. This property is read-only.
43
 * @property array $firstErrors The first errors. The array keys are the attribute names, and the array values
44
 * are the corresponding error messages. An empty array will be returned if there is no error. This property is
45
 * read-only.
46
 * @property ArrayIterator $iterator An iterator for traversing the items in the list. This property is
47
 * read-only.
48
 * @property string $scenario The scenario that this model is in. Defaults to [[SCENARIO_DEFAULT]].
49
 * @property ArrayObject|\yii\validators\Validator[] $validators All the validators declared in the model.
50
 * This property is read-only.
51
 *
52
 * @author Qiang Xue <[email protected]>
53
 * @since 2.0
54
 */
55
class Model extends Component implements IteratorAggregate, ArrayAccess, Arrayable
56
{
57
    use ArrayableTrait;
58
59
    /**
60
     * The name of the default scenario.
61
     */
62
    const SCENARIO_DEFAULT = 'default';
63
    /**
64
     * @event ModelEvent an event raised at the beginning of [[validate()]]. You may set
65
     * [[ModelEvent::isValid]] to be false to stop the validation.
66
     */
67
    const EVENT_BEFORE_VALIDATE = 'beforeValidate';
68
    /**
69
     * @event Event an event raised at the end of [[validate()]]
70
     */
71
    const EVENT_AFTER_VALIDATE = 'afterValidate';
72
73
    /**
74
     * @var array validation errors (attribute name => array of errors)
75
     */
76
    private $_errors;
77
    /**
78
     * @var ArrayObject list of validators
79
     */
80
    private $_validators;
81
    /**
82
     * @var string current scenario
83
     */
84
    private $_scenario = self::SCENARIO_DEFAULT;
85
86
87
    /**
88
     * Returns the validation rules for attributes.
89
     *
90
     * Validation rules are used by [[validate()]] to check if attribute values are valid.
91
     * Child classes may override this method to declare different validation rules.
92
     *
93
     * Each rule is an array with the following structure:
94
     *
95
     * ```php
96
     * [
97
     *     ['attribute1', 'attribute2'],
98
     *     'validator type',
99
     *     'on' => ['scenario1', 'scenario2'],
100
     *     //...other parameters...
101
     * ]
102
     * ```
103
     *
104
     * where
105
     *
106
     *  - attribute list: required, specifies the attributes array to be validated, for single attribute you can pass a string;
107
     *  - validator type: required, specifies the validator to be used. It can be a built-in validator name,
108
     *    a method name of the model class, an anonymous function, or a validator class name.
109
     *  - on: optional, specifies the [[scenario|scenarios]] array in which the validation
110
     *    rule can be applied. If this option is not set, the rule will apply to all scenarios.
111
     *  - additional name-value pairs can be specified to initialize the corresponding validator properties.
112
     *    Please refer to individual validator class API for possible properties.
113
     *
114
     * A validator can be either an object of a class extending [[Validator]], or a model class method
115
     * (called *inline validator*) that has the following signature:
116
     *
117
     * ```php
118
     * // $params refers to validation parameters given in the rule
119
     * function validatorName($attribute, $params)
120
     * ```
121
     *
122
     * In the above `$attribute` refers to the attribute currently being validated while `$params` contains an array of
123
     * validator configuration options such as `max` in case of `string` validator. The value of the attribute currently being validated
124
     * can be accessed as `$this->$attribute`. Note the `$` before `attribute`; this is taking the value of the variable
125
     * `$attribute` and using it as the name of the property to access.
126
     *
127
     * Yii also provides a set of [[Validator::builtInValidators|built-in validators]].
128
     * Each one has an alias name which can be used when specifying a validation rule.
129
     *
130
     * Below are some examples:
131
     *
132
     * ```php
133
     * [
134
     *     // built-in "required" validator
135
     *     [['username', 'password'], 'required'],
136
     *     // built-in "string" validator customized with "min" and "max" properties
137
     *     ['username', 'string', 'min' => 3, 'max' => 12],
138
     *     // built-in "compare" validator that is used in "register" scenario only
139
     *     ['password', 'compare', 'compareAttribute' => 'password2', 'on' => 'register'],
140
     *     // an inline validator defined via the "authenticate()" method in the model class
141
     *     ['password', 'authenticate', 'on' => 'login'],
142
     *     // a validator of class "DateRangeValidator"
143
     *     ['dateRange', 'DateRangeValidator'],
144
     * ];
145
     * ```
146
     *
147
     * Note, in order to inherit rules defined in the parent class, a child class needs to
148
     * merge the parent rules with child rules using functions such as `array_merge()`.
149
     *
150
     * @return array validation rules
151
     * @see scenarios()
152
     */
153 34
    public function rules()
154
    {
155 34
        return [];
156
    }
157
158
    /**
159
     * Returns a list of scenarios and the corresponding active attributes.
160
     * An active attribute is one that is subject to validation in the current scenario.
161
     * The returned array should be in the following format:
162
     *
163
     * ```php
164
     * [
165
     *     'scenario1' => ['attribute11', 'attribute12', ...],
166
     *     'scenario2' => ['attribute21', 'attribute22', ...],
167
     *     ...
168
     * ]
169
     * ```
170
     *
171
     * By default, an active attribute is considered safe and can be massively assigned.
172
     * If an attribute should NOT be massively assigned (thus considered unsafe),
173
     * please prefix the attribute with an exclamation character (e.g. `'!rank'`).
174
     *
175
     * The default implementation of this method will return all scenarios found in the [[rules()]]
176
     * declaration. A special scenario named [[SCENARIO_DEFAULT]] will contain all attributes
177
     * found in the [[rules()]]. Each scenario will be associated with the attributes that
178
     * are being validated by the validation rules that apply to the scenario.
179
     *
180
     * @return array a list of scenarios and the corresponding active attributes.
181
     */
182 34
    public function scenarios()
183
    {
184 34
        $scenarios = [self::SCENARIO_DEFAULT => []];
185 34
        foreach ($this->getValidators() as $validator) {
186 18
            foreach ($validator->on as $scenario) {
187 4
                $scenarios[$scenario] = [];
188 18
            }
189 18
            foreach ($validator->except as $scenario) {
190 1
                $scenarios[$scenario] = [];
191 18
            }
192 34
        }
193 34
        $names = array_keys($scenarios);
194
195 34
        foreach ($this->getValidators() as $validator) {
196 18
            if (empty($validator->on) && empty($validator->except)) {
197 18
                foreach ($names as $name) {
198 18
                    foreach ($validator->attributes as $attribute) {
199 18
                        $scenarios[$name][$attribute] = true;
200 18
                    }
201 18
                }
202 18
            } elseif (empty($validator->on)) {
203 1
                foreach ($names as $name) {
204 1
                    if (!in_array($name, $validator->except, true)) {
205 1
                        foreach ($validator->attributes as $attribute) {
206 1
                            $scenarios[$name][$attribute] = true;
207 1
                        }
208 1
                    }
209 1
                }
210 1
            } else {
211 4
                foreach ($validator->on as $name) {
212 4
                    foreach ($validator->attributes as $attribute) {
213 4
                        $scenarios[$name][$attribute] = true;
214 4
                    }
215 4
                }
216
            }
217 34
        }
218
219 34
        foreach ($scenarios as $scenario => $attributes) {
220 34
            if (!empty($attributes)) {
221 18
                $scenarios[$scenario] = array_keys($attributes);
222 18
            }
223 34
        }
224
225 34
        return $scenarios;
226
    }
227
228
    /**
229
     * Returns the form name that this model class should use.
230
     *
231
     * The form name is mainly used by [[\yii\widgets\ActiveForm]] to determine how to name
232
     * the input fields for the attributes in a model. If the form name is "A" and an attribute
233
     * name is "b", then the corresponding input name would be "A[b]". If the form name is
234
     * an empty string, then the input name would be "b".
235
     *
236
     * The purpose of the above naming schema is that for forms which contain multiple different models,
237
     * the attributes of each model are grouped in sub-arrays of the POST-data and it is easier to
238
     * differentiate between them.
239
     *
240
     * By default, this method returns the model class name (without the namespace part)
241
     * as the form name. You may override it when the model is used in different forms.
242
     *
243
     * @return string the form name of this model class.
244
     * @see load()
245
     */
246 34
    public function formName()
247
    {
248 34
        $reflector = new ReflectionClass($this);
249 34
        return $reflector->getShortName();
250
    }
251
252
    /**
253
     * Returns the list of attribute names.
254
     * By default, this method returns all public non-static properties of the class.
255
     * You may override this method to change the default behavior.
256
     * @return array list of attribute names.
257
     */
258 3
    public function attributes()
259
    {
260 3
        $class = new ReflectionClass($this);
261 3
        $names = [];
262 3
        foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
263 3
            if (!$property->isStatic()) {
264 3
                $names[] = $property->getName();
265 3
            }
266 3
        }
267
268 3
        return $names;
269
    }
270
271
    /**
272
     * Returns the attribute labels.
273
     *
274
     * Attribute labels are mainly used for display purpose. For example, given an attribute
275
     * `firstName`, we can declare a label `First Name` which is more user-friendly and can
276
     * be displayed to end users.
277
     *
278
     * By default an attribute label is generated using [[generateAttributeLabel()]].
279
     * This method allows you to explicitly specify attribute labels.
280
     *
281
     * Note, in order to inherit labels defined in the parent class, a child class needs to
282
     * merge the parent labels with child labels using functions such as `array_merge()`.
283
     *
284
     * @return array attribute labels (name => label)
285
     * @see generateAttributeLabel()
286
     */
287 48
    public function attributeLabels()
288
    {
289 48
        return [];
290
    }
291
292
    /**
293
     * Returns the attribute hints.
294
     *
295
     * Attribute hints are mainly used for display purpose. For example, given an attribute
296
     * `isPublic`, we can declare a hint `Whether the post should be visible for not logged in users`,
297
     * which provides user-friendly description of the attribute meaning and can be displayed to end users.
298
     *
299
     * Unlike label hint will not be generated, if its explicit declaration is omitted.
300
     *
301
     * Note, in order to inherit hints defined in the parent class, a child class needs to
302
     * merge the parent hints with child hints using functions such as `array_merge()`.
303
     *
304
     * @return array attribute hints (name => hint)
305
     * @since 2.0.4
306
     */
307 3
    public function attributeHints()
308
    {
309 3
        return [];
310
    }
311
312
    /**
313
     * Performs the data validation.
314
     *
315
     * This method executes the validation rules applicable to the current [[scenario]].
316
     * The following criteria are used to determine whether a rule is currently applicable:
317
     *
318
     * - the rule must be associated with the attributes relevant to the current scenario;
319
     * - the rules must be effective for the current scenario.
320
     *
321
     * This method will call [[beforeValidate()]] and [[afterValidate()]] before and
322
     * after the actual validation, respectively. If [[beforeValidate()]] returns false,
323
     * the validation will be cancelled and [[afterValidate()]] will not be called.
324
     *
325
     * Errors found during the validation can be retrieved via [[getErrors()]],
326
     * [[getFirstErrors()]] and [[getFirstError()]].
327
     *
328
     * @param array $attributeNames list of attribute names that should be validated.
329
     * If this parameter is empty, it means any attribute listed in the applicable
330
     * validation rules should be validated.
331
     * @param boolean $clearErrors whether to call [[clearErrors()]] before performing validation
332
     * @return boolean whether the validation is successful without any error.
333
     * @throws InvalidParamException if the current scenario is unknown.
334
     */
335 27
    public function validate($attributeNames = null, $clearErrors = true)
336
    {
337 27
        if ($clearErrors) {
338 20
            $this->clearErrors();
339 20
        }
340
341 27
        if (!$this->beforeValidate()) {
342
            return false;
343
        }
344
345 27
        $scenarios = $this->scenarios();
346 27
        $scenario = $this->getScenario();
347 27
        if (!isset($scenarios[$scenario])) {
348
            throw new InvalidParamException("Unknown scenario: $scenario");
349
        }
350
351 27
        if ($attributeNames === null) {
352 27
            $attributeNames = $this->activeAttributes();
353 27
        }
354
355 27
        foreach ($this->getActiveValidators() as $validator) {
356 12
            $validator->validateAttributes($this, $attributeNames);
357 27
        }
358 27
        $this->afterValidate();
359
360 27
        return !$this->hasErrors();
361
    }
362
363
    /**
364
     * This method is invoked before validation starts.
365
     * The default implementation raises a `beforeValidate` event.
366
     * You may override this method to do preliminary checks before validation.
367
     * Make sure the parent implementation is invoked so that the event can be raised.
368
     * @return boolean whether the validation should be executed. Defaults to true.
369
     * If false is returned, the validation will stop and the model is considered invalid.
370
     */
371 27
    public function beforeValidate()
372
    {
373 27
        $event = new ModelEvent;
374 27
        $this->trigger(self::EVENT_BEFORE_VALIDATE, $event);
375
376 27
        return $event->isValid;
377
    }
378
379
    /**
380
     * This method is invoked after validation ends.
381
     * The default implementation raises an `afterValidate` event.
382
     * You may override this method to do postprocessing after validation.
383
     * Make sure the parent implementation is invoked so that the event can be raised.
384
     */
385 27
    public function afterValidate()
386
    {
387 27
        $this->trigger(self::EVENT_AFTER_VALIDATE);
388 27
    }
389
390
    /**
391
     * Returns all the validators declared in [[rules()]].
392
     *
393
     * This method differs from [[getActiveValidators()]] in that the latter
394
     * only returns the validators applicable to the current [[scenario]].
395
     *
396
     * Because this method returns an ArrayObject object, you may
397
     * manipulate it by inserting or removing validators (useful in model behaviors).
398
     * For example,
399
     *
400
     * ```php
401
     * $model->validators[] = $newValidator;
402
     * ```
403
     *
404
     * @return ArrayObject|\yii\validators\Validator[] all the validators declared in the model.
405
     */
406 50
    public function getValidators()
407
    {
408 50
        if ($this->_validators === null) {
409 50
            $this->_validators = $this->createValidators();
410 50
        }
411 50
        return $this->_validators;
412
    }
413
414
    /**
415
     * Returns the validators applicable to the current [[scenario]].
416
     * @param string $attribute the name of the attribute whose applicable validators should be returned.
417
     * If this is null, the validators for ALL attributes in the model will be returned.
418
     * @return \yii\validators\Validator[] the validators applicable to the current [[scenario]].
419
     */
420 44
    public function getActiveValidators($attribute = null)
421
    {
422 44
        $validators = [];
423 44
        $scenario = $this->getScenario();
424 44
        foreach ($this->getValidators() as $validator) {
425 21
            if ($validator->isActive($scenario) && ($attribute === null || in_array($attribute, $validator->attributes, true))) {
426 21
                $validators[] = $validator;
427 21
            }
428 44
        }
429 44
        return $validators;
430
    }
431
432
    /**
433
     * Creates validator objects based on the validation rules specified in [[rules()]].
434
     * Unlike [[getValidators()]], each time this method is called, a new list of validators will be returned.
435
     * @return ArrayObject validators
436
     * @throws InvalidConfigException if any validation rule configuration is invalid
437
     */
438 51
    public function createValidators()
439
    {
440 51
        $validators = new ArrayObject;
441 51
        foreach ($this->rules() as $rule) {
442 18
            if ($rule instanceof Validator) {
443
                $validators->append($rule);
444 18
            } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type
445 17
                $validator = Validator::createValidator($rule[1], $this, (array) $rule[0], array_slice($rule, 2));
446 17
                $validators->append($validator);
447 17
            } else {
448 1
                throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.');
449
            }
450 50
        }
451 50
        return $validators;
452
    }
453
454
    /**
455
     * Returns a value indicating whether the attribute is required.
456
     * This is determined by checking if the attribute is associated with a
457
     * [[\yii\validators\RequiredValidator|required]] validation rule in the
458
     * current [[scenario]].
459
     *
460
     * Note that when the validator has a conditional validation applied using
461
     * [[\yii\validators\RequiredValidator::$when|$when]] this method will return
462
     * `false` regardless of the `when` condition because it may be called be
463
     * before the model is loaded with data.
464
     *
465
     * @param string $attribute attribute name
466
     * @return boolean whether the attribute is required
467
     */
468 11
    public function isAttributeRequired($attribute)
469
    {
470 11
        foreach ($this->getActiveValidators($attribute) as $validator) {
471 3
            if ($validator instanceof RequiredValidator && $validator->when === null) {
472 3
                return true;
473
            }
474 9
        }
475 9
        return false;
476
    }
477
478
    /**
479
     * Returns a value indicating whether the attribute is safe for massive assignments.
480
     * @param string $attribute attribute name
481
     * @return boolean whether the attribute is safe for massive assignments
482
     * @see safeAttributes()
483
     */
484 1
    public function isAttributeSafe($attribute)
485
    {
486 1
        return in_array($attribute, $this->safeAttributes(), true);
487
    }
488
489
    /**
490
     * Returns a value indicating whether the attribute is active in the current scenario.
491
     * @param string $attribute attribute name
492
     * @return boolean whether the attribute is active in the current scenario
493
     * @see activeAttributes()
494
     */
495 1
    public function isAttributeActive($attribute)
496
    {
497 1
        return in_array($attribute, $this->activeAttributes(), true);
498
    }
499
500
    /**
501
     * Returns the text label for the specified attribute.
502
     * @param string $attribute the attribute name
503
     * @return string the attribute label
504
     * @see generateAttributeLabel()
505
     * @see attributeLabels()
506
     */
507 13
    public function getAttributeLabel($attribute)
508
    {
509 13
        $labels = $this->attributeLabels();
510 13
        return isset($labels[$attribute]) ? $labels[$attribute] : $this->generateAttributeLabel($attribute);
511
    }
512
513
    /**
514
     * Returns the text hint for the specified attribute.
515
     * @param string $attribute the attribute name
516
     * @return string the attribute hint
517
     * @see attributeHints()
518
     * @since 2.0.4
519
     */
520 7
    public function getAttributeHint($attribute)
521
    {
522 7
        $hints = $this->attributeHints();
523 7
        return isset($hints[$attribute]) ? $hints[$attribute] : '';
524
    }
525
526
    /**
527
     * Returns a value indicating whether there is any validation error.
528
     * @param string|null $attribute attribute name. Use null to check all attributes.
529
     * @return boolean whether there is any error.
530
     */
531 238
    public function hasErrors($attribute = null)
532
    {
533 238
        return $attribute === null ? !empty($this->_errors) : isset($this->_errors[$attribute]);
534
    }
535
536
    /**
537
     * Returns the errors for all attribute or a single attribute.
538
     * @param string $attribute attribute name. Use null to retrieve errors for all attributes.
539
     * @property array An array of errors for all attributes. Empty array is returned if no error.
540
     * The result is a two-dimensional array. See [[getErrors()]] for detailed description.
541
     * @return array errors for all attributes or the specified attribute. Empty array is returned if no error.
542
     * Note that when returning errors for all attributes, the result is a two-dimensional array, like the following:
543
     *
544
     * ```php
545
     * [
546
     *     'username' => [
547
     *         'Username is required.',
548
     *         'Username must contain only word characters.',
549
     *     ],
550
     *     'email' => [
551
     *         'Email address is invalid.',
552
     *     ]
553
     * ]
554
     * ```
555
     *
556
     * @see getFirstErrors()
557
     * @see getFirstError()
558
     */
559 27
    public function getErrors($attribute = null)
560
    {
561 27
        if ($attribute === null) {
562 9
            return $this->_errors === null ? [] : $this->_errors;
563
        } else {
564 19
            return isset($this->_errors[$attribute]) ? $this->_errors[$attribute] : [];
565
        }
566
    }
567
568
    /**
569
     * Returns the first error of every attribute in the model.
570
     * @return array the first errors. The array keys are the attribute names, and the array
571
     * values are the corresponding error messages. An empty array will be returned if there is no error.
572
     * @see getErrors()
573
     * @see getFirstError()
574
     */
575 2
    public function getFirstErrors()
576
    {
577 2
        if (empty($this->_errors)) {
578 1
            return [];
579
        } else {
580 2
            $errors = [];
581 2
            foreach ($this->_errors as $name => $es) {
582 2
                if (!empty($es)) {
583 2
                    $errors[$name] = reset($es);
584 2
                }
585 2
            }
586
587 2
            return $errors;
588
        }
589
    }
590
591
    /**
592
     * Returns the first error of the specified attribute.
593
     * @param string $attribute attribute name.
594
     * @return string the error message. Null is returned if no error.
595
     * @see getErrors()
596
     * @see getFirstErrors()
597
     */
598 20
    public function getFirstError($attribute)
599
    {
600 20
        return isset($this->_errors[$attribute]) ? reset($this->_errors[$attribute]) : null;
601
    }
602
603
    /**
604
     * Adds a new error to the specified attribute.
605
     * @param string $attribute attribute name
606
     * @param string $error new error message
607
     */
608 87
    public function addError($attribute, $error = '')
609
    {
610 87
        $this->_errors[$attribute][] = $error;
611 87
    }
612
613
    /**
614
     * Adds a list of errors.
615
     * @param array $items a list of errors. The array keys must be attribute names.
616
     * The array values should be error messages. If an attribute has multiple errors,
617
     * these errors must be given in terms of an array.
618
     * You may use the result of [[getErrors()]] as the value for this parameter.
619
     * @since 2.0.2
620
     */
621 5
    public function addErrors(array $items)
622
    {
623 5
        foreach ($items as $attribute => $errors) {
624 5
            if (is_array($errors)) {
625 5
                foreach ($errors as $error) {
626 5
                    $this->addError($attribute, $error);
627 5
                }
628 5
            } else {
629 1
                $this->addError($attribute, $errors);
630
            }
631 5
        }
632 5
    }
633
634
    /**
635
     * Removes errors for all attributes or a single attribute.
636
     * @param string $attribute attribute name. Use null to remove errors for all attribute.
637
     */
638 44
    public function clearErrors($attribute = null)
639
    {
640 44
        if ($attribute === null) {
641 39
            $this->_errors = [];
642 39
        } else {
643 9
            unset($this->_errors[$attribute]);
644
        }
645 44
    }
646
647
    /**
648
     * Generates a user friendly attribute label based on the give attribute name.
649
     * This is done by replacing underscores, dashes and dots with blanks and
650
     * changing the first letter of each word to upper case.
651
     * For example, 'department_name' or 'DepartmentName' will generate 'Department Name'.
652
     * @param string $name the column name
653
     * @return string the attribute label
654
     */
655 55
    public function generateAttributeLabel($name)
656
    {
657 55
        return Inflector::camel2words($name, true);
658
    }
659
660
    /**
661
     * Returns attribute values.
662
     * @param array $names list of attributes whose value needs to be returned.
663
     * Defaults to null, meaning all attributes listed in [[attributes()]] will be returned.
664
     * If it is an array, only the attributes in the array will be returned.
665
     * @param array $except list of attributes whose value should NOT be returned.
666
     * @return array attribute values (name => value).
667
     */
668 2
    public function getAttributes($names = null, $except = [])
669
    {
670 2
        $values = [];
671 2
        if ($names === null) {
672 2
            $names = $this->attributes();
673 2
        }
674 2
        foreach ($names as $name) {
675 2
            $values[$name] = $this->$name;
676 2
        }
677 2
        foreach ($except as $name) {
678 1
            unset($values[$name]);
679 2
        }
680
681 2
        return $values;
682
    }
683
684
    /**
685
     * Sets the attribute values in a massive way.
686
     * @param array $values attribute values (name => value) to be assigned to the model.
687
     * @param boolean $safeOnly whether the assignments should only be done to the safe attributes.
688
     * A safe attribute is one that is associated with a validation rule in the current [[scenario]].
689
     * @see safeAttributes()
690
     * @see attributes()
691
     */
692 10
    public function setAttributes($values, $safeOnly = true)
693
    {
694 10
        if (is_array($values)) {
695 10
            $attributes = array_flip($safeOnly ? $this->safeAttributes() : $this->attributes());
696 10
            foreach ($values as $name => $value) {
697 10
                if (isset($attributes[$name])) {
698 10
                    $this->$name = $value;
699 10
                } elseif ($safeOnly) {
700 2
                    $this->onUnsafeAttribute($name, $value);
701 2
                }
702 10
            }
703 10
        }
704 10
    }
705
706
    /**
707
     * This method is invoked when an unsafe attribute is being massively assigned.
708
     * The default implementation will log a warning message if YII_DEBUG is on.
709
     * It does nothing otherwise.
710
     * @param string $name the unsafe attribute name
711
     * @param mixed $value the attribute value
712
     */
713 2
    public function onUnsafeAttribute($name, $value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
714
    {
715 2
        if (YII_DEBUG) {
716 2
            Yii::trace("Failed to set unsafe attribute '$name' in '" . get_class($this) . "'.", __METHOD__);
717 2
        }
718 2
    }
719
720
    /**
721
     * Returns the scenario that this model is used in.
722
     *
723
     * Scenario affects how validation is performed and which attributes can
724
     * be massively assigned.
725
     *
726
     * @return string the scenario that this model is in. Defaults to [[SCENARIO_DEFAULT]].
727
     */
728 115
    public function getScenario()
729
    {
730 115
        return $this->_scenario;
731
    }
732
733
    /**
734
     * Sets the scenario for the model.
735
     * Note that this method does not check if the scenario exists or not.
736
     * The method [[validate()]] will perform this check.
737
     * @param string $value the scenario that this model is in.
738
     */
739 7
    public function setScenario($value)
740
    {
741 7
        $this->_scenario = $value;
742 7
    }
743
744
    /**
745
     * Returns the attribute names that are safe to be massively assigned in the current scenario.
746
     * @return string[] safe attribute names
747
     */
748 5
    public function safeAttributes()
749
    {
750 5
        $scenario = $this->getScenario();
751 5
        $scenarios = $this->scenarios();
752 5
        if (!isset($scenarios[$scenario])) {
753 3
            return [];
754
        }
755 5
        $attributes = [];
756 5
        foreach ($scenarios[$scenario] as $attribute) {
757 5
            if ($attribute[0] !== '!' && !in_array('!' . $attribute, $scenarios[$scenario])) {
758 5
                $attributes[] = $attribute;
759 5
            }
760 5
        }
761
762 5
        return $attributes;
763
    }
764
765
    /**
766
     * Returns the attribute names that are subject to validation in the current scenario.
767
     * @return string[] safe attribute names
768
     */
769 34
    public function activeAttributes()
770
    {
771 34
        $scenario = $this->getScenario();
772 34
        $scenarios = $this->scenarios();
773 34
        if (!isset($scenarios[$scenario])) {
774 2
            return [];
775
        }
776 34
        $attributes = $scenarios[$scenario];
777 34
        foreach ($attributes as $i => $attribute) {
778 18
            if ($attribute[0] === '!') {
779 2
                $attributes[$i] = substr($attribute, 1);
780 2
            }
781 34
        }
782
783 34
        return $attributes;
784
    }
785
786
    /**
787
     * Populates the model with input data.
788
     *
789
     * This method provides a convenient shortcut for:
790
     *
791
     * ```php
792
     * if (isset($_POST['FormName'])) {
793
     *     $model->attributes = $_POST['FormName'];
794
     *     if ($model->save()) {
795
     *         // handle success
796
     *     }
797
     * }
798
     * ```
799
     *
800
     * which, with `load()` can be written as:
801
     *
802
     * ```php
803
     * if ($model->load($_POST) && $model->save()) {
804
     *     // handle success
805
     * }
806
     * ```
807
     *
808
     * `load()` gets the `'FormName'` from the model's [[formName()]] method (which you may override), unless the
809
     * `$formName` parameter is given. If the form name is empty, `load()` populates the model with the whole of `$data`,
810
     * instead of `$data['FormName']`.
811
     *
812
     * Note, that the data being populated is subject to the safety check by [[setAttributes()]].
813
     *
814
     * @param array $data the data array to load, typically `$_POST` or `$_GET`.
815
     * @param string $formName the form name to use to load the data into the model.
816
     * If not set, [[formName()]] is used.
817
     * @return boolean whether `load()` found the expected form in `$data`.
818
     */
819 1
    public function load($data, $formName = null)
820
    {
821 1
        $scope = $formName === null ? $this->formName() : $formName;
822 1
        if ($scope === '' && !empty($data)) {
823 1
            $this->setAttributes($data);
824
825 1
            return true;
826 1
        } elseif (isset($data[$scope])) {
827 1
            $this->setAttributes($data[$scope]);
828
829 1
            return true;
830
        } else {
831 1
            return false;
832
        }
833
    }
834
835
    /**
836
     * Populates a set of models with the data from end user.
837
     * This method is mainly used to collect tabular data input.
838
     * The data to be loaded for each model is `$data[formName][index]`, where `formName`
839
     * refers to the value of [[formName()]], and `index` the index of the model in the `$models` array.
840
     * If [[formName()]] is empty, `$data[index]` will be used to populate each model.
841
     * The data being populated to each model is subject to the safety check by [[setAttributes()]].
842
     * @param array $models the models to be populated. Note that all models should have the same class.
843
     * @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array
844
     * supplied by end user.
845
     * @param string $formName the form name to be used for loading the data into the models.
846
     * If not set, it will use the [[formName()]] value of the first model in `$models`.
847
     * This parameter is available since version 2.0.1.
848
     * @return boolean whether at least one of the models is successfully populated.
849
     */
850
    public static function loadMultiple($models, $data, $formName = null)
851
    {
852
        if ($formName === null) {
853
            /* @var $first Model */
854
            $first = reset($models);
855
            if ($first === false) {
856
                return false;
857
            }
858
            $formName = $first->formName();
859
        }
860
861
        $success = false;
862
        foreach ($models as $i => $model) {
863
            /* @var $model Model */
864
            if ($formName == '') {
865
                if (!empty($data[$i])) {
866
                    $model->load($data[$i], '');
867
                    $success = true;
868
                }
869
            } elseif (!empty($data[$formName][$i])) {
870
                $model->load($data[$formName][$i], '');
871
                $success = true;
872
            }
873
        }
874
875
        return $success;
876
    }
877
878
    /**
879
     * Validates multiple models.
880
     * This method will validate every model. The models being validated may
881
     * be of the same or different types.
882
     * @param array $models the models to be validated
883
     * @param array $attributeNames list of attribute names that should be validated.
884
     * If this parameter is empty, it means any attribute listed in the applicable
885
     * validation rules should be validated.
886
     * @return boolean whether all models are valid. False will be returned if one
887
     * or multiple models have validation error.
888
     */
889
    public static function validateMultiple($models, $attributeNames = null)
890
    {
891
        $valid = true;
892
        /* @var $model Model */
893
        foreach ($models as $model) {
894
            $valid = $model->validate($attributeNames) && $valid;
895
        }
896
897
        return $valid;
898
    }
899
900
    /**
901
     * Returns the list of fields that should be returned by default by [[toArray()]] when no specific fields are specified.
902
     *
903
     * A field is a named element in the returned array by [[toArray()]].
904
     *
905
     * This method should return an array of field names or field definitions.
906
     * If the former, the field name will be treated as an object property name whose value will be used
907
     * as the field value. If the latter, the array key should be the field name while the array value should be
908
     * the corresponding field definition which can be either an object property name or a PHP callable
909
     * returning the corresponding field value. The signature of the callable should be:
910
     *
911
     * ```php
912
     * function ($model, $field) {
913
     *     // return field value
914
     * }
915
     * ```
916
     *
917
     * For example, the following code declares four fields:
918
     *
919
     * - `email`: the field name is the same as the property name `email`;
920
     * - `firstName` and `lastName`: the field names are `firstName` and `lastName`, and their
921
     *   values are obtained from the `first_name` and `last_name` properties;
922
     * - `fullName`: the field name is `fullName`. Its value is obtained by concatenating `first_name`
923
     *   and `last_name`.
924
     *
925
     * ```php
926
     * return [
927
     *     'email',
928
     *     'firstName' => 'first_name',
929
     *     'lastName' => 'last_name',
930
     *     'fullName' => function ($model) {
931
     *         return $model->first_name . ' ' . $model->last_name;
932
     *     },
933
     * ];
934
     * ```
935
     *
936
     * In this method, you may also want to return different lists of fields based on some context
937
     * information. For example, depending on [[scenario]] or the privilege of the current application user,
938
     * you may return different sets of visible fields or filter out some fields.
939
     *
940
     * The default implementation of this method returns [[attributes()]] indexed by the same attribute names.
941
     *
942
     * @return array the list of field names or field definitions.
943
     * @see toArray()
944
     */
945
    public function fields()
946
    {
947
        $fields = $this->attributes();
948
949
        return array_combine($fields, $fields);
950
    }
951
952
    /**
953
     * Returns an iterator for traversing the attributes in the model.
954
     * This method is required by the interface [[\IteratorAggregate]].
955
     * @return ArrayIterator an iterator for traversing the items in the list.
956
     */
957 1
    public function getIterator()
958
    {
959 1
        $attributes = $this->getAttributes();
960 1
        return new ArrayIterator($attributes);
961
    }
962
963
    /**
964
     * Returns whether there is an element at the specified offset.
965
     * This method is required by the SPL interface [[\ArrayAccess]].
966
     * It is implicitly called when you use something like `isset($model[$offset])`.
967
     * @param mixed $offset the offset to check on.
968
     * @return boolean whether or not an offset exists.
969
     */
970 1
    public function offsetExists($offset)
971
    {
972 1
        return isset($this->$offset);
973
    }
974
975
    /**
976
     * Returns the element at the specified offset.
977
     * This method is required by the SPL interface [[\ArrayAccess]].
978
     * It is implicitly called when you use something like `$value = $model[$offset];`.
979
     * @param mixed $offset the offset to retrieve element.
980
     * @return mixed the element at the offset, null if no element is found at the offset
981
     */
982 129
    public function offsetGet($offset)
983
    {
984 129
        return $this->$offset;
985
    }
986
987
    /**
988
     * Sets the element at the specified offset.
989
     * This method is required by the SPL interface [[\ArrayAccess]].
990
     * It is implicitly called when you use something like `$model[$offset] = $item;`.
991
     * @param integer $offset the offset to set element
992
     * @param mixed $item the element value
993
     */
994 1
    public function offsetSet($offset, $item)
995
    {
996 1
        $this->$offset = $item;
997 1
    }
998
999
    /**
1000
     * Sets the element value at the specified offset to null.
1001
     * This method is required by the SPL interface [[\ArrayAccess]].
1002
     * It is implicitly called when you use something like `unset($model[$offset])`.
1003
     * @param mixed $offset the offset to unset element
1004
     */
1005 1
    public function offsetUnset($offset)
1006
    {
1007 1
        $this->$offset = null;
1008 1
    }
1009
}
1010