Total Complexity | 12 |
Total Lines | 58 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | class SizeValidator extends ValidatorAbstract implements ValidatorInterface |
||
10 | { |
||
11 | protected $upscale; |
||
12 | protected $regex = [ |
||
13 | ',[0-9]+', |
||
14 | '\^,[0-9]+', |
||
15 | '[0-9]+,', |
||
16 | '\^[0-9]+,', |
||
17 | '{!}?[0-9]+,[0-9]+', |
||
18 | ]; |
||
19 | |||
20 | public function valid($value) |
||
21 | { |
||
22 | if ($value === 'max') { |
||
23 | return true; |
||
24 | } |
||
25 | |||
26 | if ($value === '^max') { |
||
27 | throw new NotImplementedException("Maximum size is not implemented."); |
||
28 | } |
||
29 | |||
30 | $this->upscale = str_starts_with($value, '^'); |
||
31 | |||
32 | if ($this->upscale) { |
||
33 | throw new NotImplementedException("Upscaling is not allowed."); |
||
34 | } |
||
35 | |||
36 | $isPercent = str_contains($value, 'pct:'); |
||
37 | |||
38 | if ($isPercent) { |
||
39 | return $this->isValidPercent($value); |
||
40 | } |
||
41 | |||
42 | $all_regex = implode('|', $this->regex); |
||
43 | if (preg_match('/' . $all_regex . '/', $value)) { |
||
44 | return true; |
||
45 | } |
||
46 | |||
47 | throw new BadRequestException("Size $value is invalid."); |
||
48 | } |
||
49 | |||
50 | protected function getPercentValue($value) |
||
57 | } |
||
58 | |||
59 | protected function isValidPercent($value) |
||
69 |