Completed
Push — master ( f45cc5...2edd5b )
by Igor
05:32
created

FileRulesDescription::imageSizeDescription()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

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