Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
AreaCoordsTester | |
100.00% |
15 / 15 |
|
100.00% |
3 / 3 |
12 | |
100.00% |
1 / 1 |
testValue | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
10 | |||
getShape | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setShape | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
5 | */ |
6 | declare(strict_types=1); |
7 | |
8 | namespace pvc\html\val_tester; |
9 | |
10 | use pvc\interfaces\validator\ValTesterInterface; |
11 | |
12 | /** |
13 | * Class ValTesterAreaCoords |
14 | * @implements ValTesterInterface<string> |
15 | * coordinates define the placement and extent of the shape which is specified in the area element. There is a |
16 | * valid shape called default which does not accept any coordinates. |
17 | */ |
18 | class AreaCoordsTester implements ValTesterInterface |
19 | { |
20 | protected string $shape = 'rect'; |
21 | |
22 | /** |
23 | * testValue |
24 | * @param string $value |
25 | * @return bool |
26 | * I don't think browsers will kick out nonsensical numbers like a circle with a diameter of 0 or a |
27 | * rectangle with no height and/or no width |
28 | */ |
29 | public function testValue(mixed $value): bool |
30 | { |
31 | $coords = explode(',', $value); |
32 | $callable = function (bool $carry, string $x): bool { |
33 | return $carry && ctype_digit(trim($x)); |
34 | }; |
35 | if (!array_reduce($coords, $callable, true)) { |
36 | return false; |
37 | } |
38 | |
39 | /** |
40 | * rectangles take 4 coordinates |
41 | */ |
42 | if ($this->getShape() == 'rect' && count($coords) != 4) { |
43 | return false; |
44 | } |
45 | |
46 | /** |
47 | * circles take 3 coordinates: the first two are the center of the circle, the third is the radius |
48 | */ |
49 | if ($this->getShape() == 'circle' && count($coords) != 3) { |
50 | return false; |
51 | } |
52 | |
53 | /** |
54 | * polygons must have an even number of coordinates greater than 4 |
55 | */ |
56 | if ($this->getShape() == 'poly' && (count($coords) < 4 || count($coords) % 2 != 0)) { |
57 | return false; |
58 | } |
59 | |
60 | return true; |
61 | } |
62 | |
63 | public function getShape(): string |
64 | { |
65 | return $this->shape; |
66 | } |
67 | |
68 | public function setShape(string $shape): void |
69 | { |
70 | $this->shape = strtolower($shape); |
71 | } |
72 | } |