|
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
|
10 |
|
private static function maxSizeDescription(string $value = null): string |
|
14
|
|
|
{ |
|
15
|
10 |
|
if ($value === null) { |
|
16
|
8 |
|
return ''; |
|
17
|
|
|
} |
|
18
|
2 |
|
$value = Yii::$app->formatter->asShortSize($value); |
|
19
|
2 |
|
return Yii::t('app.validators', 'Max. file size') . ': ' . $value . ' '; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
10 |
|
private static function maxFilesDescription(string $value = null): string |
|
23
|
|
|
{ |
|
24
|
10 |
|
if ($value === null) { |
|
25
|
10 |
|
return ''; |
|
26
|
|
|
} |
|
27
|
|
|
return Yii::t('app.validators', 'Max. file number') . ': ' . $value . ' '; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
10 |
|
private static function extensionDescription(array $value = null): string |
|
31
|
|
|
{ |
|
32
|
10 |
|
if ($value === null) { |
|
33
|
8 |
|
return ''; |
|
34
|
|
|
} |
|
35
|
2 |
|
$value = strtoupper(implode(', ', $value)); |
|
36
|
2 |
|
return Yii::t('app.validators', 'File types') . ': ' . $value . ' '; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
10 |
|
private static function imageSizeDescription(array $rules = null): string |
|
40
|
|
|
{ |
|
41
|
10 |
|
if ($rules === null) { |
|
42
|
1 |
|
return ''; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
9 |
|
$maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
|
46
|
9 |
|
$minWidth = ArrayHelper::getValue($rules, 'minWidth'); |
|
47
|
9 |
|
$maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
|
48
|
9 |
|
$minHeight = ArrayHelper::getValue($rules, 'minHeight'); |
|
49
|
|
|
|
|
50
|
9 |
|
$text = []; |
|
51
|
|
|
switch ($rules) { |
|
52
|
9 |
|
case self::isImageWithStrictSize($rules): |
|
53
|
1 |
|
$text[] = Yii::t('app.validators', 'Image size') . ': ' . $maxWidth . 'x' . $maxHeight . 'px'; |
|
54
|
1 |
|
break; |
|
55
|
8 |
|
case self::isImageWithMinAndMaxSize($rules): |
|
56
|
1 |
|
$text[] = Yii::t('app.validators', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px'; |
|
57
|
1 |
|
$text[] = Yii::t('app.validators', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px'; |
|
58
|
1 |
|
break; |
|
59
|
7 |
|
case self::isImageWithMinSize($rules): |
|
60
|
2 |
|
$text[] = Yii::t('app.validators', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px'; |
|
61
|
2 |
|
$text[] = self::prepareImageSizeDescription($rules, ['minWidth', 'minHeight']); |
|
62
|
2 |
|
break; |
|
63
|
5 |
|
case self::isImageWithMaxSize($rules): |
|
64
|
1 |
|
$text[] = Yii::t('app.validators', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px'; |
|
65
|
1 |
|
$text[] = self::prepareImageSizeDescription($rules, ['maxWidth', 'maxHeight']); |
|
66
|
1 |
|
break; |
|
67
|
|
|
default: |
|
68
|
4 |
|
$text[] = self::prepareImageSizeDescription($rules); |
|
69
|
4 |
|
break; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
9 |
|
return implode('<br>', array_filter($text)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
9 |
|
private static function isImageWithStrictSize(array $rules): bool |
|
76
|
|
|
{ |
|
77
|
9 |
|
$maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
|
78
|
9 |
|
$minWidth = ArrayHelper::getValue($rules, 'minWidth'); |
|
79
|
9 |
|
$maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
|
80
|
9 |
|
$minHeight = ArrayHelper::getValue($rules, 'minHeight'); |
|
81
|
|
|
|
|
82
|
9 |
|
return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
8 |
|
private static function isImageWithMinAndMaxSize(array $rules): bool |
|
86
|
|
|
{ |
|
87
|
8 |
|
return count($rules) == 4; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
private static function isImageWithMinSize(array $rules): bool |
|
91
|
|
|
{ |
|
92
|
7 |
|
$minWidth = ArrayHelper::getValue($rules, 'minWidth'); |
|
93
|
7 |
|
$minHeight = ArrayHelper::getValue($rules, 'minHeight'); |
|
94
|
|
|
|
|
95
|
7 |
|
return (count($rules) == 2 || count($rules) == 3) && $minWidth && $minHeight; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
private static function isImageWithMaxSize(array $rules): bool |
|
99
|
|
|
{ |
|
100
|
5 |
|
$maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
|
101
|
5 |
|
$maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
|
102
|
|
|
|
|
103
|
5 |
|
return (count($rules) == 2 || count($rules) == 3) && $maxWidth && $maxHeight; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
7 |
|
private static function prepareImageSizeDescription(array $rules, array $exclude = []): string |
|
107
|
|
|
{ |
|
108
|
7 |
|
foreach ($exclude as $item) { |
|
109
|
3 |
|
unset($rules[$item]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
7 |
|
$text = []; |
|
113
|
7 |
|
foreach ($rules as $rule => $value) { |
|
114
|
|
|
switch ($rule) { |
|
115
|
4 |
|
case 'minWidth': |
|
116
|
1 |
|
$text[] = Yii::t('app.validators', 'Min. width') . ' ' . $value . 'px'; |
|
117
|
1 |
|
break; |
|
118
|
3 |
|
case 'minHeight': |
|
119
|
1 |
|
$text[] = Yii::t('app.validators', 'Min. height') . ' ' . $value . 'px'; |
|
120
|
1 |
|
break; |
|
121
|
2 |
|
case 'maxWidth': |
|
122
|
1 |
|
$text[] = Yii::t('app.validators', 'Max. width') . ' ' . $value . 'px'; |
|
123
|
1 |
|
break; |
|
124
|
1 |
|
case 'maxHeight': |
|
125
|
1 |
|
$text[] = Yii::t('app.validators', 'Max. height') . ' ' . $value . 'px'; |
|
126
|
1 |
|
break; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
7 |
|
return implode('<br>', array_filter($text)); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Get a description of the validation rules in as text |
|
135
|
|
|
* |
|
136
|
|
|
* @param array $rules Validation rules |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
10 |
|
public static function toText(array $rules): string |
|
140
|
|
|
{ |
|
141
|
10 |
|
$text = []; |
|
142
|
10 |
|
$text[] = self::imageSizeDescription(ArrayHelper::getValue($rules, 'imageSize')); |
|
143
|
10 |
|
$text[] = self::extensionDescription(ArrayHelper::getValue($rules, 'extensions')); |
|
144
|
10 |
|
$text[] = self::maxSizeDescription(ArrayHelper::getValue($rules, 'maxSize')); |
|
145
|
10 |
|
$text[] = self::maxFilesDescription(ArrayHelper::getValue($rules, 'maxFiles')); |
|
146
|
|
|
|
|
147
|
10 |
|
return implode('<br>', array_filter($text)); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|