Completed
Push — master ( 574186...c63227 )
by Igor
05:18
created

FormatValidation::isImageWithMinAndMaxSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * @link https://github.com/rkit/filemanager-yii2
5
 * @copyright Copyright (c) 2015 Igor Romanov
6
 * @license [MIT](http://opensource.org/licenses/MIT)
7
 */
8
9
namespace rkit\filemanager\helpers;
10
11
use Yii;
12
use yii\helpers\ArrayHelper;
13
14
/**
15
 * Formatting of validation rules
16
 */
17
class FormatValidation
18
{
19
    /**
20
     * Get description for max size of file
21
     *
22
     * @param int $rules
23
     * @return string
24
     */
25 1
    private static function getMaxSizeDescription($rules)
26
    {
27 1
        $maxSize = Yii::$app->formatter->asShortSize($rules);
28 1
        return Yii::t('filemanager-yii2', 'Max. file size') . ': ' . $maxSize . ' ';
29
    }
30
31
    /**
32
     * Get description for extensions of file
33
     *
34
     * @param array $rules
35
     * @return string
36
     */
37 1
    private static function getExtensionDescription($rules)
38
    {
39 1
        $extensions = strtoupper(implode(', ', $rules));
40 1
        return Yii::t('filemanager-yii2', 'File types') . ': ' . $extensions . ' ';
41
    }
42
43
    /**
44
     * Get description for size of image
45
     *
46
     * @param array $rules
47
     * @return string
48
     */
49 9
    private static function getImageSizeDescription($rules)
50
    {
51 9
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
52 9
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
53 9
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
54 9
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
55
56 9
        $text = '';
57 9
        if (self::isImageWithStrictSize($rules)) {
58 1
            $text .= Yii::t('filemanager-yii2', 'Image size') . ': ' . $maxWidth . 'x' . $maxHeight . 'px;';
59 9
        } elseif (self::isImageWithMinAndMaxSize($rules)) {
60 1
            $text .= Yii::t('filemanager-yii2', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px;';
61 1
            $text .= Yii::t('filemanager-yii2', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px;';
62 8
        } elseif (self::isImageWithMinSize($rules)) {
63 2
            $text .= Yii::t('filemanager-yii2', 'Min. size of image') . ': ' . $minWidth . 'x' . $minHeight . 'px;';
64 2
            $text .= self::prepareImageSizeDescription($rules, ['minWidth', 'minHeight']);
65 7
        } elseif (self::isImageWithMaxSize($rules)) {
66 1
            $text .= Yii::t('filemanager-yii2', 'Max. size of image') . ': ' . $maxWidth . 'x' . $maxHeight . 'px;';
67 1
            $text .= self::prepareImageSizeDescription($rules, ['maxWidth', 'maxHeight']);
68 1
        } else {
69 4
            $text .= self::prepareImageSizeDescription($rules);
70
        }
71
72 9
        $text = mb_substr($text, 0, -1);
73 9
        $text = str_replace(';', '<br>', $text);
74
75 9
        return $text;
76
    }
77
78
    /**
79
     * This rules for image with strict size
80
     *
81
     * @param array $rules
82
     * @return bool
83
     */
84 9
    private static function isImageWithStrictSize($rules)
85
    {
86 9
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
87 9
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
88 9
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
89 9
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
90
91 9
        return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight);
92
    }
93
94
    /**
95
     * This rules for image with min and max size
96
     *
97
     * @param array $rules
98
     * @return bool
99
     */
100 8
    private static function isImageWithMinAndMaxSize($rules)
101
    {
102 8
        return count($rules) == 4;
103
    }
104
105
    /**
106
     * This rules for image with min size
107
     *
108
     * @param array $rules
109
     * @return bool
110
     */
111 7
    private static function isImageWithMinSize($rules)
112
    {
113 7
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
114 7
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
115
116 7
        return (count($rules) == 2 || count($rules) == 3) && $minWidth && $minHeight;
117
    }
118
119
    /**
120
     * This rules for image with max size
121
     *
122
     * @param array $rules
123
     * @return bool
124
     */
125 5
    private static function isImageWithMaxSize($rules)
126
    {
127 5
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
128 5
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
129
130 5
        return (count($rules) == 2 || count($rules) == 3) && $maxWidth && $maxHeight;
131
    }
132
133
    /**
134
     * Prepare description for size of image
135
     *
136
     * @param array $rules
137
     * @return string
138
     */
139 7
    private static function prepareImageSizeDescription($rules, $exclude = [])
140
    {
141 7
        foreach ($exclude as $item) {
142 3
            unset($rules[$item]);
143 7
        }
144
145 7
        $text = '';
146 7
        foreach ($rules as $rule => $value) {
147
            switch ($rule) {
148 4
                case 'minWidth':
149 1
                    $text .= Yii::t('filemanager-yii2', 'Min. width') . ' ' . $value . 'px;';
150 1
                    break;
151 3
                case 'minHeight':
152 1
                    $text .= Yii::t('filemanager-yii2', 'Min. height') . ' ' . $value . 'px;';
153 1
                    break;
154 2
                case 'maxWidth':
155 1
                    $text .= Yii::t('filemanager-yii2', 'Max. width') . ' ' . $value . 'px;';
156 1
                    break;
157 1
                case 'maxHeight':
158 1
                    $text .= Yii::t('filemanager-yii2', 'Max. height') . ' ' . $value . 'px;';
159 1
                    break;
160
            }
161 7
        }
162
163 7
        return $text;
164
    }
165
166
    /**
167
     * Get rules description
168
     *
169
     * @param array $rules Validation rules
170
     * @return string
171
     */
172 9
    public static function getDescription($rules)
173
    {
174 9
        $text = '';
175 9
        if (isset($rules['imageSize'])) {
176 9
            $text .= self::getImageSizeDescription($rules['imageSize']);
177 9
            $text = !empty($text) ? $text . '<br>' : $text;
178 9
        }
179
180 9
        if (isset($rules['extensions'])) {
181 1
            $text .= self::getExtensionDescription($rules['extensions']);
182 1
            $text = isset($rules['maxSize']) ? $text . '<br>' : $text;
183 1
        }
184
185 9
        if (isset($rules['maxSize'])) {
186 1
            $text .= self::getMaxSizeDescription($rules['maxSize']);
187 1
        }
188
189 9
        return $text;
190
    }
191
}
192