Issues (462)

src/validators/EmbeddedUnitsDataValidator.php (33 issues)

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
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
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
Missing short description in doc comment
Loading history...
22
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   Units
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
26
class EmbeddedUnitsDataValidator extends Validator
27
{
28
    // Public Properties
29
    // =========================================================================
30
31
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
32
     * @var string the base units
33
     */
34
    public $units;
35
36
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
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
Missing short description in doc comment
Loading history...
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
Missing short description in doc comment
Loading history...
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
Parameter $model should have a doc-comment as per coding-style.
Loading history...
Parameter $attribute should have a doc-comment as per coding-style.
Loading history...
Missing short description in doc comment
Loading history...
59
     * @inheritdoc
60
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
61
    public function validateAttribute($model, $attribute)
62
    {
63
        /** @var Model $model */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
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
Missing parameter comment
Loading history...
102
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
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