Passed
Push — scrutinizer-migrate-to-new-eng... ( 58afd6 )
by Alexander
18:11
created

DefaultValueValidator::validateAttribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
c 0
b 0
f 0
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\validators;
9
10
/**
11
 * DefaultValueValidator sets the attribute to be the specified default value.
12
 *
13
 * DefaultValueValidator is not really a validator. It is provided mainly to allow
14
 * specifying attribute default values when they are empty.
15
 *
16
 * @author Qiang Xue <[email protected]>
17
 * @since 2.0
18
 */
19
class DefaultValueValidator extends Validator
20
{
21
    /**
22
     * @var mixed the default value or an anonymous function that returns the default value which will
23
     * be assigned to the attributes being validated if they are empty. The signature of the anonymous function
24
     * should be as follows,
25
     *
26
     * ```php
27
     * function($model, $attribute) {
28
     *     // compute value
29
     *     return $value;
30
     * }
31
     * ```
32
     */
33
    public $value;
34
    /**
35
     * @var bool this property is overwritten to be false so that this validator will
36
     * be applied when the value being validated is empty.
37
     */
38
    public $skipOnEmpty = false;
39
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function validateAttribute($model, $attribute)
45
    {
46 2
        if ($this->isEmpty($model->$attribute)) {
47 2
            if ($this->value instanceof \Closure) {
48
                $model->$attribute = call_user_func($this->value, $model, $attribute);
49
            } else {
50 2
                $model->$attribute = $this->value;
51
            }
52
        }
53 2
    }
54
}
55