@@ 8-61 (lines=54) @@ | ||
5 | use AmaTeam\Image\Projection\Framework\Validation\ValidationException; |
|
6 | use AmaTeam\Image\Projection\Framework\Validation\ValidatorInterface; |
|
7 | ||
8 | class TileFaceValidator implements ValidatorInterface |
|
9 | { |
|
10 | /** |
|
11 | * @var int|null |
|
12 | */ |
|
13 | private $height; |
|
14 | /** |
|
15 | * @var bool |
|
16 | */ |
|
17 | private $allowedEmpty = false; |
|
18 | /** |
|
19 | * @var ValidatorInterface|null |
|
20 | */ |
|
21 | private $rowValidator = null; |
|
22 | ||
23 | /** |
|
24 | * @param int|null $height |
|
25 | * @param bool $allowedEmpty |
|
26 | * @param ValidatorInterface|null $rowValidator |
|
27 | * |
|
28 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
29 | */ |
|
30 | public function __construct( |
|
31 | $height = null, |
|
32 | $allowedEmpty = false, |
|
33 | $rowValidator = null |
|
34 | ) { |
|
35 | $this->height = $height; |
|
36 | $this->allowedEmpty = $allowedEmpty; |
|
37 | $this->rowValidator = $rowValidator; |
|
38 | } |
|
39 | ||
40 | /** |
|
41 | * @inheritDoc |
|
42 | */ |
|
43 | public function validate($value, array $path = []) |
|
44 | { |
|
45 | if (!is_array($value)) { |
|
46 | throw new ValidationException('Is not an array', $path); |
|
47 | } |
|
48 | if (!$this->allowedEmpty && empty($value)) { |
|
49 | throw new ValidationException('Is empty', $path); |
|
50 | } |
|
51 | if (is_int($this->height) && sizeof($value) !== $this->height) { |
|
52 | $template = 'Height is %d (%d expected)'; |
|
53 | $message = sprintf($template, sizeof($value), $this->height); |
|
54 | throw new ValidationException($message, $path); |
|
55 | } |
|
56 | if ($this->rowValidator) { |
|
57 | foreach ($value as $y => $row) { |
|
58 | $rowPath = array_merge($path, [$y]); |
|
59 | $this->rowValidator->validate($row, $rowPath); |
|
60 | } |
|
61 | } |
|
62 | } |
|
63 | } |
|
64 |
@@ 8-61 (lines=54) @@ | ||
5 | use AmaTeam\Image\Projection\Framework\Validation\ValidationException; |
|
6 | use AmaTeam\Image\Projection\Framework\Validation\ValidatorInterface; |
|
7 | ||
8 | class TileRowValidator implements ValidatorInterface |
|
9 | { |
|
10 | /** |
|
11 | * @var int|null |
|
12 | */ |
|
13 | private $size; |
|
14 | /** |
|
15 | * @var bool |
|
16 | */ |
|
17 | private $allowedEmpty = false; |
|
18 | /** |
|
19 | * @var ValidatorInterface|null |
|
20 | */ |
|
21 | private $tileValidator; |
|
22 | ||
23 | /** |
|
24 | * @param int|null $size |
|
25 | * @param bool $allowedEmpty |
|
26 | * @param ValidatorInterface|null $tileValidator |
|
27 | * |
|
28 | * @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
|
29 | */ |
|
30 | public function __construct( |
|
31 | $size = null, |
|
32 | $allowedEmpty = false, |
|
33 | ValidatorInterface $tileValidator = null |
|
34 | ) { |
|
35 | $this->size = $size; |
|
36 | $this->allowedEmpty = $allowedEmpty; |
|
37 | $this->tileValidator = $tileValidator; |
|
38 | } |
|
39 | ||
40 | /** |
|
41 | * @inheritDoc |
|
42 | */ |
|
43 | public function validate($value, array $path = []) |
|
44 | { |
|
45 | if (!is_array($value)) { |
|
46 | throw new ValidationException('Is not an array', $path); |
|
47 | } |
|
48 | if (!$this->allowedEmpty && empty($value)) { |
|
49 | throw new ValidationException('Is empty array', $path); |
|
50 | } |
|
51 | if (is_int($this->size) && sizeof($value) !== $this->size) { |
|
52 | $template = 'Has size of %d (%d expected)'; |
|
53 | $message = sprintf($template, sizeof($value), $this->size); |
|
54 | throw new ValidationException($message); |
|
55 | } |
|
56 | if ($this->tileValidator) { |
|
57 | foreach ($value as $offset => $tile) { |
|
58 | $tilePath = array_merge($path, [$offset]); |
|
59 | $this->tileValidator->validate($tile, $tilePath); |
|
60 | } |
|
61 | } |
|
62 | } |
|
63 | } |
|
64 |