nystudio107 /
craft-units
| 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/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 9 | * @copyright Copyright (c) nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 10 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 22 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 23 | * @package Units |
||
|
0 ignored issues
–
show
|
|||
| 24 | * @since 1.0.0 |
||
|
0 ignored issues
–
show
|
|||
| 25 | */ |
||
|
0 ignored issues
–
show
|
|||
| 26 | class EmbeddedUnitsDataValidator extends Validator |
||
| 27 | { |
||
| 28 | // Public Properties |
||
| 29 | // ========================================================================= |
||
| 30 | |||
| 31 | /** |
||
|
0 ignored issues
–
show
|
|||
| 32 | * @var string the base units |
||
| 33 | */ |
||
| 34 | public $units; |
||
| 35 | |||
| 36 | /** |
||
|
0 ignored issues
–
show
|
|||
| 37 | * @var bool whether the attribute value can only be an integer. Defaults |
||
| 38 | * to false. |
||
| 39 | */ |
||
| 40 | public $integerOnly = false; |
||
| 41 | /** |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 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 | /** |
||
|
0 ignored issues
–
show
|
|||
| 59 | * @inheritdoc |
||
| 60 | */ |
||
|
0 ignored issues
–
show
|
|||
| 61 | public function validateAttribute($model, $attribute) |
||
| 62 | { |
||
| 63 | /** @var Model $model */ |
||
|
0 ignored issues
–
show
|
|||
| 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 */ |
||
|
0 ignored issues
–
show
|
|||
| 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 |
||
|
0 ignored issues
–
show
|
|||
| 102 | */ |
||
|
0 ignored issues
–
show
|
|||
| 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 |