Completed
Push — develop ( 43936d...b3a70f )
by Nate
02:02
created

MinMaxValidator::validateAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/domains/license
6
 * @link       https://www.flipboxfactory.com/software/domains/
7
 */
8
9
namespace flipbox\domains\validators;
10
11
use craft\validators\ArrayValidator;
12
use yii\db\QueryInterface;
13
14
/**
15
 * @author Flipbox Factory <[email protected]>
16
 * @since 1.0.0
17
 */
18
class MinMaxValidator extends ArrayValidator
19
{
20
    /**
21
     * @inheritdoc
22
     */
23
    public function validateAttribute($model, $attribute)
24
    {
25
        $value = $model->$attribute;
26
27
        if($value instanceof QueryInterface) {
28
            return $this->validateQueryAttribute($model, $attribute, $value);
29
        }
30
31
        return parent::validateAttribute($model, $attribute);
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    protected function validateValue($value)
38
    {
39
        if(!$value instanceof QueryInterface) {
40
            return parent::validateValue($value);
41
        }
42
43
        /** @var $value QueryInterface */
44
        if (!is_string($value)) {
45
            return [$this->message, []];
46
        }
47
48
        $count = $value->count();
0 ignored issues
show
Bug introduced by
The method count cannot be called on $value (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
50
        if ($this->min !== null && $count < $this->min) {
51
            return [$this->tooFew, ['min' => $this->min]];
52
        }
53
        if ($this->max !== null && $count > $this->max) {
54
            return [$this->tooMany, ['max' => $this->max]];
55
        }
56
        if ($this->count !== null && $count !== $this->count) {
57
            return [$this->notEqual, ['count' => $this->count]];
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * @param $model
65
     * @param $attribute
66
     * @param QueryInterface $query
67
     */
68
    protected function validateQueryAttribute($model, $attribute, QueryInterface $query)
69
    {
70
        $count = $query->count();
71
72
        if ($this->min !== null && $count < $this->min) {
73
            $this->addError($model, $attribute, $this->tooFew, ['min' => $this->min]);
74
        }
75
        if ($this->max !== null && $count > $this->max) {
76
            $this->addError($model, $attribute, $this->tooMany, ['max' => $this->max]);
77
        }
78
        if ($this->count !== null && $count !== $this->count) {
79
            $this->addError($model, $attribute, $this->notEqual, ['count' => $this->count]);
80
        }
81
    }
82
}
83