Completed
Push — master ( c064eb...85769a )
by Igor
03:38
created

FileRulesDescription::imageSizeDescription()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 37
ccs 27
cts 27
cp 1
rs 8.439
cc 6
eloc 29
nc 6
nop 0
crap 6
1
<?php
2
3
namespace app\modules\admin\helpers;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
8
/**
9
 * Formatting of validation rules
10
 */
11
class FileRulesDescription
12
{
13
    /**
14
     * @var array
15
     */
16
    private $rules = [];
17
18
    /**
19
     * @param array $rules Validation rules
20
     */
21 13
    public function __construct(array $rules)
22
    {
23 13
        $this->rules = $rules;
24 13
    }
25
26 13
    private function maxSizeDescription(): string
27
    {
28 13
        $rules = ArrayHelper::getValue($this->rules, 'maxSize');
29
30 13
        if ($rules === null) {
31 8
            return '';
32
        }
33 5
        $rules = Yii::$app->formatter->asShortSize($rules);
34 5
        return Yii::t('app.msg', 'Max. file size') . ': ' . $rules . ' ';
35
    }
36
37 13
    private function maxFilesDescription(): string
38
    {
39 13
        $rules = ArrayHelper::getValue($this->rules, 'maxFiles');
40
41 13
        if ($rules === null) {
42 12
            return '';
43
        }
44 3
        return Yii::t('app.msg', 'Max. file number') . ': ' . $rules . ' ';
45
    }
46
47 13
    private function extensionDescription(): string
48
    {
49 13
        $rules = ArrayHelper::getValue($this->rules, 'extensions');
50
51 13
        if ($rules === null) {
52 8
            return '';
53
        }
54 5
        $rules = strtoupper(implode(', ', $rules));
55 5
        return Yii::t('app.msg', 'File types') . ': ' . $rules . ' ';
56
    }
57
58 13
    private function imageSizeDescription(): string
59
    {
60 13
        $rules = ArrayHelper::getValue($this->rules, 'imageSize');
61
62 13
        if ($rules === null) {
63 3
            return '';
64
        }
65
66 12
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
67 12
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
68 12
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
69 12
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
70
71 12
        $text = [];
72
        switch ($rules) {
73 12
            case $this->isImageWithStrictSize($rules):
74 1
                $text[] = Yii::t('app.msg', 'Image size') . ': ' . $maxWidth . 'x' . $maxHeight . 'px';
75 1
                break;
76 11
            case $this->isImageWithMinAndMaxSize($rules):
77 1
                $text[] = Yii::t('app.msg', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px';
78 1
                $text[] = Yii::t('app.msg', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px';
79 1
                break;
80 10
            case $this->isImageWithMinSize($rules):
81 5
                $text[] = Yii::t('app.msg', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px';
82 5
                $text[] = $this->prepareImageSizeDescription($rules, ['minWidth', 'minHeight']);
83 5
                break;
84 5
            case $this->isImageWithMaxSize($rules):
85 1
                $text[] = Yii::t('app.msg', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px';
86 1
                $text[] = $this->prepareImageSizeDescription($rules, ['maxWidth', 'maxHeight']);
87 1
                break;
88
            default:
89 4
                $text[] = $this->prepareImageSizeDescription($rules);
90 4
                break;
91
        }
92
93 12
        return implode('<br>', array_filter($text));
94
    }
95
96 12
    private function isImageWithStrictSize(array $rules): bool
97
    {
98 12
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
99 12
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
100 12
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
101 12
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
102
103 12
        return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight);
104
    }
105
106 11
    private function isImageWithMinAndMaxSize(array $rules): bool
107
    {
108 11
        return count($rules) == 4;
109
    }
110
111 10
    private function isImageWithMinSize(array $rules): bool
112
    {
113 10
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
114 10
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
115
116 10
        return (count($rules) == 2 || count($rules) == 3) && $minWidth && $minHeight;
117
    }
118
119 5
    private function isImageWithMaxSize(array $rules): bool
120
    {
121 5
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
122 5
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
123
124 5
        return (count($rules) == 2 || count($rules) == 3) && $maxWidth && $maxHeight;
125
    }
126
127 10
    private function prepareImageSizeDescription(array $rules, array $exclude = []): string
128
    {
129 10
        foreach ($exclude as $item) {
130 6
            unset($rules[$item]);
131
        }
132
133 10
        $text = [];
134 10
        foreach ($rules as $rule => $value) {
135
            switch ($rule) {
136 4
                case 'minWidth':
137 1
                    $text[] = Yii::t('app.msg', 'Min. width') . ' ' . $value . 'px';
138 1
                    break;
139 3
                case 'minHeight':
140 1
                    $text[] = Yii::t('app.msg', 'Min. height') . ' ' . $value . 'px';
141 1
                    break;
142 2
                case 'maxWidth':
143 1
                    $text[] = Yii::t('app.msg', 'Max. width') . ' ' . $value . 'px';
144 1
                    break;
145 1
                case 'maxHeight':
146 1
                    $text[] = Yii::t('app.msg', 'Max. height') . ' ' . $value . 'px';
147 1
                    break;
148
            }
149
        }
150
151 10
        return implode('<br>', array_filter($text));
152
    }
153
154
    /**
155
     * Get a description of the validation rules in as text
156
     *
157
     * @return string
158
     */
159 13
    public function toText(): string
160
    {
161 13
        $text = [];
162
163 13
        $text[] = $this->imageSizeDescription();
164 13
        $text[] = $this->extensionDescription();
165 13
        $text[] = $this->maxSizeDescription();
166 13
        $text[] = $this->maxFilesDescription();
167
168 13
        return implode('<br>', array_filter($text));
169
    }
170
}
171