1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Units plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* A plugin for handling physical quantities and the units of measure in which |
6
|
|
|
* they're represented. |
7
|
|
|
* |
8
|
|
|
* @link https://nystudio107.com/ |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\units\validators; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
use nystudio107\units\models\UnitsData; |
16
|
|
|
use yii\base\Model; |
17
|
|
|
use yii\validators\NumberValidator; |
18
|
|
|
use yii\validators\Validator; |
19
|
|
|
use function is_object; |
20
|
|
|
|
21
|
|
|
/** |
|
|
|
|
22
|
|
|
* @author nystudio107 |
|
|
|
|
23
|
|
|
* @package Units |
|
|
|
|
24
|
|
|
* @since 1.0.0 |
|
|
|
|
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
class EmbeddedUnitsDataValidator extends Validator |
27
|
|
|
{ |
28
|
|
|
// Public Properties |
29
|
|
|
// ========================================================================= |
30
|
|
|
|
31
|
|
|
/** |
|
|
|
|
32
|
|
|
* @var string the base units |
33
|
|
|
*/ |
34
|
|
|
public $units; |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
37
|
|
|
* @var bool whether the attribute value can only be an integer. Defaults |
38
|
|
|
* to false. |
39
|
|
|
*/ |
40
|
|
|
public $integerOnly = false; |
41
|
|
|
/** |
|
|
|
|
42
|
|
|
* @var int|float|null upper limit of the number. Defaults to null, meaning no |
43
|
|
|
* upper limit. |
44
|
|
|
* @see tooBig for the customized message used when the number is too big. |
45
|
|
|
*/ |
46
|
|
|
public $max = null; |
47
|
|
|
/** |
|
|
|
|
48
|
|
|
* @var int|float|null lower limit of the number. Defaults to null, meaning no |
49
|
|
|
* lower limit. |
50
|
|
|
* @see tooSmall for the customized message used when the number is too |
51
|
|
|
* small. |
52
|
|
|
*/ |
53
|
|
|
public $min = null; |
54
|
|
|
|
55
|
|
|
// Public Methods |
56
|
|
|
// ========================================================================= |
57
|
|
|
|
58
|
|
|
/** |
|
|
|
|
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
|
|
|
|
61
|
|
|
public function validateAttribute($model, $attribute) |
62
|
|
|
{ |
63
|
|
|
/** @var Model $model */ |
|
|
|
|
64
|
|
|
$value = $model->$attribute; |
65
|
|
|
|
66
|
|
|
if ($value !== null && is_object($value) && $value instanceof UnitsData) { |
67
|
|
|
// Validate the model |
68
|
|
|
$value->validate(); |
69
|
|
|
// Normalize the min/max value |
70
|
|
|
$this->normalizeMinMax($value); |
71
|
|
|
// Do a min/max validation, too |
72
|
|
|
$config = [ |
73
|
|
|
'integerOnly' => $this->integerOnly, |
74
|
|
|
'min' => $this->min, |
75
|
|
|
'max' => $this->max, |
76
|
|
|
]; |
77
|
|
|
$numberValidator = new NumberValidator($config); |
78
|
|
|
$numberValidator->validateAttribute($value, 'value'); |
79
|
|
|
// Add any errors to the parent model |
80
|
|
|
$errors = $value->getErrors(); |
81
|
|
|
foreach ($errors as $attributeError => $valueErrors) { |
82
|
|
|
/** @var array $valueErrors */ |
|
|
|
|
83
|
|
|
foreach ($valueErrors as $valueError) { |
84
|
|
|
$model->addError( |
85
|
|
|
$attribute, |
86
|
|
|
$valueError |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
|
|
$model->addError($attribute, Craft::t('units', 'Is not a Model object.')); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Protected Methods |
96
|
|
|
// ========================================================================= |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Normalize the min/max values using the base units |
100
|
|
|
* |
101
|
|
|
* @param UnitsData $unitsData |
|
|
|
|
102
|
|
|
*/ |
|
|
|
|
103
|
|
|
protected function normalizeMinMax(UnitsData $unitsData) |
104
|
|
|
{ |
105
|
|
|
$config = [ |
106
|
|
|
'unitsClass' => $unitsData->unitsClass, |
107
|
|
|
'units' => $this->units, |
108
|
|
|
]; |
109
|
|
|
// Normalize the min |
110
|
|
|
if (!empty($this->min)) { |
111
|
|
|
$config['value'] = (float)$this->min; |
112
|
|
|
$baseUnit = new UnitsData($config); |
113
|
|
|
$this->min = $baseUnit->toUnit($unitsData->units); |
114
|
|
|
} else { |
115
|
|
|
$this->min = null; |
116
|
|
|
} |
117
|
|
|
// Normalize the max |
118
|
|
|
if (!empty($this->max)) { |
119
|
|
|
$config['value'] = (float)$this->max; |
120
|
|
|
$baseUnit = new UnitsData($config); |
121
|
|
|
$this->max = $baseUnit->toUnit($unitsData->units); |
122
|
|
|
} else { |
123
|
|
|
$this->max = null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|