Completed
Push — master ( 11971b...eeafb3 )
by Nate
01:16
created

ModelValidator::validateValue()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 16
cp 0
rs 9.0111
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 42
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-ember/
7
 */
8
9
namespace flipbox\craft\ember\validators;
10
11
use craft\helpers\ArrayHelper;
12
use yii\base\Model;
13
use yii\validators\Validator;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 2.0.0
18
 */
19
class ModelValidator extends Validator
20
{
21
    /**
22
     * @var array list of attribute names that should be validated.
23
     * If this parameter is empty, it means any attribute listed in the applicable
24
     * validation rules should be validated.
25
     */
26
    public $modelAttributeNames = null;
27
28
    /**
29
     * @var bool whether to call [[clearErrors()]] before performing validation
30
     */
31
    public $clearErrors = true;
32
33
    /**
34
     * @var string the scenario used to validate the model.
35
     * Defaults to null, meaning no limit.
36
     * @see tooSmall for the customized message for a file that is too small.
37
     */
38
    public $scenario;
39
40
    /**
41
     * Validates a value.
42
     * A validator class can implement this method to support data validation out of the context of a data model.
43
     * @param mixed $value the data value to be validated.
44
     * @return array|null the error message and the parameters to be inserted into the error message.
45
     * Null should be returned if the data is valid.
46
     */
47
    protected function validateValue($value)
48
    {
49
        if (empty($value)) {
50
            return null;
51
        }
52
53
        if ($value instanceof Model) {
54
            $value = [$value];
55
        }
56
57
        if (!is_array($value) ||
58
            ArrayHelper::isAssociative($value) ||
59
            false === $this->validateModels($value)
60
        ) {
61
            return [$this->message, []];
62
        }
63
64
        return null;
65
    }
66
67
    /**
68
     * @param array $models
69
     * @return bool
70
     */
71
    protected function validateModels(array $models): bool
72
    {
73
        $isValid = true;
74
75
        foreach ($models as $model) {
76
            if (false === $this->validateModel($model)) {
77
                $isValid = false;
78
            }
79
        }
80
81
        return $isValid;
82
    }
83
84
    /**
85
     * @param Model $model
86
     * @return bool
87
     */
88
    protected function validateModel(Model $model): bool
89
    {
90
        // Current scenario
91
        $defaultScenarios = $model->getScenario();
92
93
        // Change to validation scenario
94
        if ($this->scenario) {
95
            $model->setScenario($this->scenario);
96
        }
97
98
        // Validate
99
        $isValid = true;
100
        if (!$model->validate($this->modelAttributeNames, $this->clearErrors)) {
101
            $isValid = false;
102
        }
103
104
        // Revert back to prior scenario
105
        if ($this->scenario) {
106
            $model->setScenario($defaultScenarios);
107
        }
108
109
        return $isValid;
110
    }
111
}
112