Completed
Push — 2.1 ( 75349f...bf116e )
by Alexander
29:27
created

RangeValidator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 78
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 13 5
B validateValue() 0 17 8
A validateAttribute() 0 7 2
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
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\ArrayHelper;
13
14
/**
15
 * RangeValidator validates that the attribute value is among a list of values.
16
 *
17
 * The range can be specified via the [[range]] property.
18
 * If the [[not]] property is set true, the validator will ensure the attribute value
19
 * is NOT among the specified range.
20
 *
21
 * @author Qiang Xue <[email protected]>
22
 * @since 2.0
23
 */
24
class RangeValidator extends Validator
25
{
26
    /**
27
     * @var array|\Traversable|\Closure a list of valid values that the attribute value should be among or an anonymous function that returns
28
     * such a list. The signature of the anonymous function should be as follows,
29
     *
30
     * ```php
31
     * function($model, $attribute) {
32
     *     // compute range
33
     *     return $range;
34
     * }
35
     * ```
36
     */
37
    public $range;
38
    /**
39
     * @var bool whether the comparison is strict (both type and value must be the same)
40
     */
41
    public $strict = false;
42
    /**
43
     * @var bool whether to invert the validation logic. Defaults to false. If set to true,
44
     * the attribute value should NOT be among the list of values defined via [[range]].
45
     */
46
    public $not = false;
47
    /**
48
     * @var bool whether to allow array type attribute.
49
     */
50
    public $allowArray = false;
51
52
53
    /**
54
     * @inheritdoc
55
     */
56 10
    public function init()
57
    {
58 10
        parent::init();
59 10
        if (!is_array($this->range)
60 10
            && !($this->range instanceof \Closure)
61 10
            && !($this->range instanceof \Traversable)
62
        ) {
63 1
            throw new InvalidConfigException('The "range" property must be set.');
64
        }
65 9
        if ($this->message === null) {
66 9
            $this->message = Yii::t('yii', '{attribute} is invalid.');
67
        }
68 9
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73 8
    protected function validateValue($value)
74
    {
75 8
        $in = false;
76
77 8
        if ($this->allowArray
78 8
            && ($value instanceof \Traversable || is_array($value))
79 8
            && ArrayHelper::isSubset($value, $this->range, $this->strict)
80
        ) {
81 3
            $in = true;
82
        }
83
84 8
        if (!$in && ArrayHelper::isIn($value, $this->range, $this->strict)) {
85 5
            $in = true;
86
        }
87
88 8
        return $this->not !== $in ? null : [$this->message, []];
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    public function validateAttribute($model, $attribute)
95
    {
96 1
        if ($this->range instanceof \Closure) {
97
            $this->range = call_user_func($this->range, $model, $attribute);
98
        }
99 1
        parent::validateAttribute($model, $attribute);
100 1
    }
101
}
102