Completed
Push — master ( 2e8683...7e20fc )
by Igor
07:20
created

FileRulesDescription::maxFilesDescription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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($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)
79
    {
80 9
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
81 9
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
82 9
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
83 9
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
84
85 9
        return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight);
86
    }
87
88 8
    private static function isImageWithMinAndMaxSize($rules)
89
    {
90 8
        return count($rules) == 4;
91
    }
92
93 7
    private static function isImageWithMinSize($rules)
94
    {
95 7
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
96 7
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
97
98 7
        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 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)
143
    {
144 10
        $text = [];
145 10
        $text[] = self::imageSizeDescription(ArrayHelper::getValue($rules, 'imageSize'));
146 10
        $text[] = self::extensionDescription(ArrayHelper::getValue($rules, 'extensions'));
147 10
        $text[] = self::maxSizeDescription(ArrayHelper::getValue($rules, 'maxSize'));
148 10
        $text[] = self::maxFilesDescription(ArrayHelper::getValue($rules, 'maxFiles'));
149
150 10
        return implode('<br>', array_filter($text));
151
    }
152
}
153