| @@ 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 | public function __construct( |
|
| 29 | $height = null, |
|
| 30 | $allowedEmpty = false, |
|
| 31 | $rowValidator = null |
|
| 32 | ) { |
|
| 33 | $this->height = $height; |
|
| 34 | $this->allowedEmpty = $allowedEmpty; |
|
| 35 | $this->rowValidator = $rowValidator; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @inheritDoc |
|
| 40 | */ |
|
| 41 | public function validate($value, array $path = []) |
|
| 42 | { |
|
| 43 | if (!is_array($value)) { |
|
| 44 | throw new ValidationException('Is not an array', $path); |
|
| 45 | } |
|
| 46 | if (!$this->allowedEmpty && empty($value)) { |
|
| 47 | throw new ValidationException('Is empty', $path); |
|
| 48 | } |
|
| 49 | if (is_int($this->height) && sizeof($value) !== $this->height) { |
|
| 50 | $template = 'Height is %d (%d expected)'; |
|
| 51 | $message = sprintf($template, sizeof($value), $this->height); |
|
| 52 | throw new ValidationException($message, $path); |
|
| 53 | } |
|
| 54 | if ($this->rowValidator) { |
|
| 55 | foreach ($value as $y => $row) { |
|
| 56 | $rowPath = array_merge($path, [$y]); |
|
| 57 | $this->rowValidator->validate($row, $rowPath); |
|
| 58 | } |
|
| 59 | } |
|
| 60 | } |
|
| 61 | } |
|
| 62 | ||
| @@ 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 | public function __construct( |
|
| 29 | $size = null, |
|
| 30 | $allowedEmpty = false, |
|
| 31 | ValidatorInterface $tileValidator = null |
|
| 32 | ) { |
|
| 33 | $this->size = $size; |
|
| 34 | $this->allowedEmpty = $allowedEmpty; |
|
| 35 | $this->tileValidator = $tileValidator; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * @inheritDoc |
|
| 40 | */ |
|
| 41 | public function validate($value, array $path = []) |
|
| 42 | { |
|
| 43 | if (!is_array($value)) { |
|
| 44 | throw new ValidationException('Is not an array', $path); |
|
| 45 | } |
|
| 46 | if (!$this->allowedEmpty && empty($value)) { |
|
| 47 | throw new ValidationException('Is empty array', $path); |
|
| 48 | } |
|
| 49 | if (is_int($this->size) && sizeof($value) !== $this->size) { |
|
| 50 | $template = 'Has size of %d (%d expected)'; |
|
| 51 | $message = sprintf($template, sizeof($value), $this->size); |
|
| 52 | throw new ValidationException($message); |
|
| 53 | } |
|
| 54 | if ($this->tileValidator) { |
|
| 55 | foreach ($value as $offset => $tile) { |
|
| 56 | $tilePath = array_merge($path, [$offset]); |
|
| 57 | $this->tileValidator->validate($tile, $tilePath); |
|
| 58 | } |
|
| 59 | } |
|
| 60 | } |
|
| 61 | } |
|
| 62 | ||