EmbeddedUnitsDataValidator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 9
eloc 37
c 5
b 1
f 0
dl 0
loc 98
rs 10

2 Methods

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