Completed
Push — master ( f733ed...c064eb )
by Igor
04:01
created

prepareImageSizeDescription()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 26
ccs 18
cts 18
cp 1
rs 6.7272
cc 7
eloc 19
nc 12
nop 2
crap 7
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 13
    private static function maxSizeDescription(string $value = null): string
14
    {
15 13
        if ($value === null) {
16 8
            return '';
17
        }
18 5
        $value = Yii::$app->formatter->asShortSize($value);
19 5
        return Yii::t('app.validators', 'Max. file size') . ': ' . $value . ' ';
20
    }
21
22 13
    private static function maxFilesDescription(string $value = null): string
23
    {
24 13
        if ($value === null) {
25 12
            return '';
26
        }
27 3
        return Yii::t('app.validators', 'Max. file number') . ': ' . $value . ' ';
28
    }
29
30 13
    private static function extensionDescription(array $value = null): string
31
    {
32 13
        if ($value === null) {
33 8
            return '';
34
        }
35 5
        $value = strtoupper(implode(', ', $value));
36 5
        return Yii::t('app.validators', 'File types') . ': ' . $value . ' ';
37
    }
38
39 13
    private static function imageSizeDescription(array $rules = null): string
40
    {
41 13
        if ($rules === null) {
42 3
            return '';
43
        }
44
45 12
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
46 12
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
47 12
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
48 12
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
49
50 12
        $text = [];
51
        switch ($rules) {
52 12
            case self::isImageWithStrictSize($rules):
53 1
                $text[] = Yii::t('app.validators', 'Image size') . ': ' . $maxWidth . 'x' . $maxHeight . 'px';
54 1
                break;
55 11
            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 10
            case self::isImageWithMinSize($rules):
60 5
                $text[] = Yii::t('app.validators', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px';
61 5
                $text[] = self::prepareImageSizeDescription($rules, ['minWidth', 'minHeight']);
62 5
                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 12
        return implode('<br>', array_filter($text));
73
    }
74
75 12
    private static function isImageWithStrictSize(array $rules): bool
76
    {
77 12
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
78 12
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
79 12
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
80 12
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
81
82 12
        return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight);
83
    }
84
85 11
    private static function isImageWithMinAndMaxSize(array $rules): bool
86
    {
87 11
        return count($rules) == 4;
88
    }
89
90 10
    private static function isImageWithMinSize(array $rules): bool
91
    {
92 10
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
93 10
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
94
95 10
        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 10
    private static function prepareImageSizeDescription(array $rules, array $exclude = []): string
107
    {
108 10
        foreach ($exclude as $item) {
109 6
            unset($rules[$item]);
110
        }
111
112 10
        $text = [];
113 10
        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 10
        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 13
    public static function toText(array $rules): string
140
    {
141 13
        $text = [];
142 13
        $text[] = self::imageSizeDescription(ArrayHelper::getValue($rules, 'imageSize'));
143 13
        $text[] = self::extensionDescription(ArrayHelper::getValue($rules, 'extensions'));
144 13
        $text[] = self::maxSizeDescription(ArrayHelper::getValue($rules, 'maxSize'));
145 13
        $text[] = self::maxFilesDescription(ArrayHelper::getValue($rules, 'maxFiles'));
146
147 13
        return implode('<br>', array_filter($text));
148
    }
149
}
150