Completed
Push — master ( c7544d...5e7707 )
by Igor
04:06
created

FileRulesDescription::imageSizeDescription()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 35
ccs 26
cts 26
cp 1
rs 8.439
cc 6
eloc 28
nc 6
nop 1
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 13
    private static function maxSizeDescription($value)
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($value)
23
    {
24 13
        if ($value === null) {
25 10
            return '';
26
        }
27 3
        return Yii::t('app.validators', 'Max. file number') . ': ' . $value . ' ';
28
    }
29
30 13
    private static function extensionDescription($value)
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
    /**
40
     * @SuppressWarnings(PHPMD.ElseExpression)
41
     */
42 13
    private static function imageSizeDescription($rules)
43
    {
44 13
        if ($rules === null) {
45 1
            return '';
46
        }
47
48 12
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
49 12
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
50 12
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
51 12
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
52
53 12
        $text = [];
54
        switch ($rules) {
55 12
            case self::isImageWithStrictSize($rules):
56 1
                $text[] = Yii::t('app.validators', 'Image size') . ': ' . $maxWidth . 'x' . $maxHeight . 'px';
57 1
                break;
58 11
            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 10
            case self::isImageWithMinSize($rules):
63 5
                $text[] = Yii::t('app.validators', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px';
64 5
                $text[] = self::prepareImageSizeDescription($rules, ['minWidth', 'minHeight']);
65 5
                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 12
        return implode('<br>', array_filter($text));
76
    }
77
78 12
    private static function isImageWithStrictSize($rules)
79
    {
80 12
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
81 12
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
82 12
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
83 12
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
84
85 12
        return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight);
86
    }
87
88 11
    private static function isImageWithMinAndMaxSize($rules)
89
    {
90 11
        return count($rules) == 4;
91
    }
92
93 10
    private static function isImageWithMinSize($rules)
94
    {
95 10
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
96 10
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
97
98 10
        return (count($rules) == 2 || count($rules) == 3) && $minWidth && $minHeight;
99
    }
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 10
    private static function prepareImageSizeDescription($rules, $exclude = [])
110
    {
111 10
        foreach ($exclude as $item) {
112 6
            unset($rules[$item]);
113
        }
114
115 10
        $text = [];
116 10
        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 4
                    break;
130
            }
131
        }
132
133 10
        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 13
    public static function toText($rules)
143
    {
144 13
        $text = [];
145 13
        $text[] = self::imageSizeDescription(ArrayHelper::getValue($rules, 'imageSize'));
146 13
        $text[] = self::extensionDescription(ArrayHelper::getValue($rules, 'extensions'));
147 13
        $text[] = self::maxSizeDescription(ArrayHelper::getValue($rules, 'maxSize'));
148 13
        $text[] = self::maxFilesDescription(ArrayHelper::getValue($rules, 'maxFiles'));
149
150 13
        return implode('<br>', array_filter($text));
151
    }
152
}
153