1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\helpers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\ArrayHelper; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Converting file validation rules to text |
10
|
|
|
*/ |
11
|
|
|
class FileRulesTextHelper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array Validation rules |
15
|
|
|
*/ |
16
|
|
|
private $rules; |
17
|
|
|
/** |
18
|
|
|
* @var array Validation rules of image |
19
|
|
|
*/ |
20
|
|
|
private $imageSize; |
21
|
|
|
/** |
22
|
|
|
* @var string Delimiter for rules |
23
|
|
|
*/ |
24
|
|
|
private $delimiter = '<br>'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $rules Validation rules |
28
|
|
|
* @param string $delimiter Delimiter for rules. Default `<br>` |
29
|
|
|
*/ |
30
|
|
|
public function __construct(array $rules, $delimiter = null) |
31
|
|
|
{ |
32
|
|
|
$this->rules = $rules; |
33
|
|
|
$this->imageSize = ArrayHelper::getValue($rules, 'imageSize', []); |
|
|
|
|
34
|
|
|
|
35
|
|
|
if ($delimiter !== null) { |
36
|
|
|
$this->delimiter = $delimiter; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a description of the validation rules in as text |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function __toString() |
46
|
|
|
{ |
47
|
|
|
$text = []; |
48
|
|
|
|
49
|
|
|
$text[] = $this->imageSizeDescription(); |
50
|
|
|
$text[] = $this->extensionDescription(); |
51
|
|
|
$text[] = $this->maxSizeDescription(); |
52
|
|
|
$text[] = $this->maxFilesDescription(); |
53
|
|
|
|
54
|
|
|
return implode($this->delimiter, array_filter($text)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get description for rule the `maximum size` |
59
|
|
|
* |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
|
|
public function maxSizeDescription(): ?string |
63
|
|
|
{ |
64
|
|
|
$rules = ArrayHelper::getValue($this->rules, 'maxSize'); |
65
|
|
|
if ($rules === null) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return Yii::t('app', 'Max. file size') . ': ' . Yii::$app->formatter->asShortSize($rules) . ' '; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get description for rule the `maximum count of files` |
74
|
|
|
* |
75
|
|
|
* @return string|null |
76
|
|
|
*/ |
77
|
|
|
public function maxFilesDescription(): ?string |
78
|
|
|
{ |
79
|
|
|
$rules = ArrayHelper::getValue($this->rules, 'maxFiles'); |
80
|
|
|
if ($rules === null) { |
81
|
|
|
return null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return Yii::t('app', 'Max. file number') . ': ' . $rules . ' '; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get description for rule the `allowed extensions` |
89
|
|
|
* |
90
|
|
|
* @return string|null |
91
|
|
|
*/ |
92
|
|
|
public function extensionDescription(): ?string |
93
|
|
|
{ |
94
|
|
|
$rules = ArrayHelper::getValue($this->rules, 'extensions'); |
95
|
|
|
if ($rules === null) { |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return Yii::t('app', 'File types') . ': ' . strtoupper(implode(', ', $rules)) . ' '; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get description for rule the `image sizes` |
104
|
|
|
* |
105
|
|
|
* @return string|null |
106
|
|
|
*/ |
107
|
|
|
public function imageSizeDescription(): ?string |
108
|
|
|
{ |
109
|
|
|
$text = []; |
110
|
|
|
switch ($this->imageSize) { |
111
|
|
|
case $this->isImageStrictSize(): |
112
|
|
|
$text[] = $this->imageStrictSizeDescription(); |
113
|
|
|
break; |
114
|
|
|
case $this->isImageMinAndMaxSize(): |
115
|
|
|
$text[] = $this->imageMinSizeDescription(); |
116
|
|
|
$text[] = $this->imageMaxSizeDescription(); |
117
|
|
|
break; |
118
|
|
|
case $this->isImageMinSize(): |
119
|
|
|
$text[] = $this->imageMinSizeDescription(); |
120
|
|
|
$text = array_merge($text, $this->imageRules(['minWidth', 'minHeight'])); |
121
|
|
|
break; |
122
|
|
|
case $this->isImageMaxSize(): |
123
|
|
|
$text[] = $this->imageMaxSizeDescription(); |
124
|
|
|
$text = array_merge($text, $this->imageRules(['maxWidth', 'maxHeight'])); |
125
|
|
|
break; |
126
|
|
|
default: |
127
|
|
|
$text = $this->imageRules(); |
128
|
|
|
break; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (count($text)) { |
132
|
|
|
return implode($this->delimiter, array_filter($text)); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Whether the image has strict size |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function isImageStrictSize(): bool |
144
|
|
|
{ |
145
|
|
|
$maxWidth = ArrayHelper::getValue($this->imageSize, 'maxWidth'); |
146
|
|
|
$minWidth = ArrayHelper::getValue($this->imageSize, 'minWidth'); |
147
|
|
|
$maxHeight = ArrayHelper::getValue($this->imageSize, 'maxHeight'); |
148
|
|
|
$minHeight = ArrayHelper::getValue($this->imageSize, 'minHeight'); |
149
|
|
|
|
150
|
|
|
return count($this->imageSize) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get description for image with strict size |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function imageStrictSizeDescription(): string |
159
|
|
|
{ |
160
|
|
|
return |
161
|
|
|
Yii::t('app', 'Image size') . ': ' . |
162
|
|
|
ArrayHelper::getValue($this->imageSize, 'maxWidth') . 'x' . |
163
|
|
|
ArrayHelper::getValue($this->imageSize, 'maxHeight') . 'px'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Whether the image has max and min size |
168
|
|
|
* |
169
|
|
|
* @return bool |
170
|
|
|
*/ |
171
|
|
|
public function isImageMinAndMaxSize(): bool |
172
|
|
|
{ |
173
|
|
|
return count($this->imageSize) == 4; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Whether the image has min size |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function isImageMinSize(): bool |
182
|
|
|
{ |
183
|
|
|
$minWidth = ArrayHelper::getValue($this->imageSize, 'minWidth'); |
184
|
|
|
$minHeight = ArrayHelper::getValue($this->imageSize, 'minHeight'); |
185
|
|
|
|
186
|
|
|
return (count($this->imageSize) == 2 || count($this->imageSize) == 3) && $minWidth && $minHeight; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get description for image with min size |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function imageMinSizeDescription(): string |
195
|
|
|
{ |
196
|
|
|
return |
197
|
|
|
Yii::t('app', 'Min. size of image') . ': ' . |
198
|
|
|
ArrayHelper::getValue($this->imageSize, 'minWidth') . 'x' . |
199
|
|
|
ArrayHelper::getValue($this->imageSize, 'minHeight') . 'px'; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Whether the image has max size |
204
|
|
|
* |
205
|
|
|
* @return bool |
206
|
|
|
*/ |
207
|
|
|
public function isImageMaxSize(): bool |
208
|
|
|
{ |
209
|
|
|
$maxWidth = ArrayHelper::getValue($this->imageSize, 'maxWidth'); |
210
|
|
|
$maxHeight = ArrayHelper::getValue($this->imageSize, 'maxHeight'); |
211
|
|
|
|
212
|
|
|
return (count($this->imageSize) == 2 || count($this->imageSize) == 3) && $maxWidth && $maxHeight; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get description for image with max size |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function imageMaxSizeDescription(): string |
221
|
|
|
{ |
222
|
|
|
return |
223
|
|
|
Yii::t('app', 'Max. size of image') . ': ' . |
224
|
|
|
ArrayHelper::getValue($this->imageSize, 'maxWidth') . 'x' . |
225
|
|
|
ArrayHelper::getValue($this->imageSize, 'maxHeight') . 'px'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
private function imageRules(array $exclude = []): array |
229
|
|
|
{ |
230
|
|
|
$rules = $this->imageSize; |
231
|
|
|
foreach ($exclude as $item) { |
232
|
|
|
unset($rules[$item]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$text = []; |
236
|
|
|
foreach ($rules as $rule => $value) { |
237
|
|
|
switch ($rule) { |
238
|
|
|
case 'minWidth': |
239
|
|
|
$text[] = Yii::t('app', 'Min. width') . ' ' . $value . 'px'; |
240
|
|
|
break; |
241
|
|
|
case 'minHeight': |
242
|
|
|
$text[] = Yii::t('app', 'Min. height') . ' ' . $value . 'px'; |
243
|
|
|
break; |
244
|
|
|
case 'maxWidth': |
245
|
|
|
$text[] = Yii::t('app', 'Max. width') . ' ' . $value . 'px'; |
246
|
|
|
break; |
247
|
|
|
case 'maxHeight': |
248
|
|
|
$text[] = Yii::t('app', 'Max. height') . ' ' . $value . 'px'; |
249
|
|
|
break; |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return $text; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..