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

DefaultValueValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 32
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A validateAttribute() 0 7 3
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