1 | <?php |
||
11 | class FileRulesDescription |
||
12 | { |
||
13 | 10 | private static function maxSizeDescription($value) |
|
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($value) |
|
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($value) |
|
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 | /** |
||
40 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
41 | */ |
||
42 | 10 | private static function imageSizeDescription($rules) |
|
43 | { |
||
44 | 10 | if ($rules === null) { |
|
45 | 1 | return ''; |
|
46 | } |
||
47 | |||
48 | 9 | $maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
|
49 | 9 | $minWidth = ArrayHelper::getValue($rules, 'minWidth'); |
|
50 | 9 | $maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
|
51 | 9 | $minHeight = ArrayHelper::getValue($rules, 'minHeight'); |
|
52 | |||
53 | 9 | $text = []; |
|
54 | switch ($rules) { |
||
55 | 9 | case self::isImageWithStrictSize($rules): |
|
56 | 1 | $text[] = Yii::t('app.validators', 'Image size') . ': ' . $maxWidth . 'x' . $maxHeight . 'px'; |
|
57 | 1 | break; |
|
58 | 8 | case self::isImageWithMinAndMaxSize($rules): |
|
59 | 1 | $text[] = Yii::t('app.validators', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px'; |
|
60 | 1 | $text[] = Yii::t('app.validators', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px'; |
|
61 | 1 | break; |
|
62 | 7 | case self::isImageWithMinSize($rules): |
|
63 | 2 | $text[] = Yii::t('app.validators', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px'; |
|
64 | 2 | $text[] = self::prepareImageSizeDescription($rules, ['minWidth', 'minHeight']); |
|
65 | 2 | break; |
|
66 | 5 | case self::isImageWithMaxSize($rules): |
|
67 | 1 | $text[] = Yii::t('app.validators', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px'; |
|
68 | 1 | $text[] = self::prepareImageSizeDescription($rules, ['maxWidth', 'maxHeight']); |
|
69 | 1 | break; |
|
70 | default: |
||
71 | 4 | $text[] = self::prepareImageSizeDescription($rules); |
|
72 | 4 | break; |
|
73 | } |
||
74 | |||
75 | 9 | return implode('<br>', array_filter($text)); |
|
76 | } |
||
77 | |||
78 | 9 | private static function isImageWithStrictSize($rules) |
|
87 | |||
88 | 8 | private static function isImageWithMinAndMaxSize($rules) |
|
92 | |||
93 | 7 | private static function isImageWithMinSize($rules) |
|
100 | |||
101 | 5 | private static function isImageWithMaxSize($rules) |
|
102 | { |
||
103 | 5 | $maxWidth = ArrayHelper::getValue($rules, 'maxWidth'); |
|
104 | 5 | $maxHeight = ArrayHelper::getValue($rules, 'maxHeight'); |
|
105 | |||
106 | 5 | return (count($rules) == 2 || count($rules) == 3) && $maxWidth && $maxHeight; |
|
107 | } |
||
108 | |||
109 | 7 | private static function prepareImageSizeDescription($rules, $exclude = []) |
|
110 | { |
||
111 | 7 | foreach ($exclude as $item) { |
|
112 | 3 | unset($rules[$item]); |
|
113 | } |
||
114 | |||
115 | 7 | $text = []; |
|
116 | 7 | foreach ($rules as $rule => $value) { |
|
117 | switch ($rule) { |
||
118 | 4 | case 'minWidth': |
|
119 | 1 | $text[] = Yii::t('app.validators', 'Min. width') . ' ' . $value . 'px'; |
|
120 | 1 | break; |
|
121 | 3 | case 'minHeight': |
|
122 | 1 | $text[] = Yii::t('app.validators', 'Min. height') . ' ' . $value . 'px'; |
|
123 | 1 | break; |
|
124 | 2 | case 'maxWidth': |
|
125 | 1 | $text[] = Yii::t('app.validators', 'Max. width') . ' ' . $value . 'px'; |
|
126 | 1 | break; |
|
127 | 1 | case 'maxHeight': |
|
128 | 1 | $text[] = Yii::t('app.validators', 'Max. height') . ' ' . $value . 'px'; |
|
129 | 1 | break; |
|
130 | } |
||
131 | } |
||
132 | |||
133 | 7 | return implode('<br>', array_filter($text)); |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Get a description of the validation rules in as text |
||
138 | * |
||
139 | * @param array $rules Validation rules |
||
140 | * @return string |
||
141 | */ |
||
142 | 10 | public static function toText($rules) |
|
152 | } |
||
153 |