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\ember\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 $this->validateQueryValue($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return parent::validateValue($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param QueryInterface $query |
48
|
|
|
* @return array|null the error message and the parameters to be inserted into the error message. |
49
|
|
|
* Null should be returned if the data is valid. |
50
|
|
|
*/ |
51
|
|
|
protected function validateQueryValue(QueryInterface $query) |
52
|
|
|
{ |
53
|
|
|
/** @var QueryInterface $value */ |
54
|
|
|
$count = $query->count(); |
55
|
|
|
|
56
|
|
|
if ($this->min !== null && $count < $this->min) { |
57
|
|
|
return [$this->tooFew, ['min' => $this->min]]; |
58
|
|
|
} |
59
|
|
|
if ($this->max !== null && $count > $this->max) { |
60
|
|
|
return [$this->tooMany, ['max' => $this->max]]; |
61
|
|
|
} |
62
|
|
|
if ($this->count !== null && $count !== $this->count) { |
63
|
|
|
return [$this->notEqual, ['count' => $this->count]]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $model |
71
|
|
|
* @param $attribute |
72
|
|
|
* @param QueryInterface $query |
73
|
|
|
*/ |
74
|
|
|
protected function validateQueryAttribute($model, $attribute, QueryInterface $query) |
75
|
|
|
{ |
76
|
|
|
$count = $query->count(); |
77
|
|
|
|
78
|
|
|
if ($this->min !== null && $count < $this->min) { |
79
|
|
|
$this->addError($model, $attribute, $this->tooFew, ['min' => $this->min]); |
80
|
|
|
} |
81
|
|
|
if ($this->max !== null && $count > $this->max) { |
82
|
|
|
$this->addError($model, $attribute, $this->tooMany, ['max' => $this->max]); |
83
|
|
|
} |
84
|
|
|
if ($this->count !== null && $count !== $this->count) { |
85
|
|
|
$this->addError($model, $attribute, $this->notEqual, ['count' => $this->count]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|