1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Framework\Validation\Tile; |
4
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\Framework\Validation\ValidationException; |
6
|
|
|
use AmaTeam\Image\Projection\Framework\Validation\ValidatorInterface; |
7
|
|
|
|
8
|
|
|
class TileTreeValidator implements ValidatorInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string[] |
12
|
|
|
*/ |
13
|
|
|
private $faces; |
14
|
|
|
/** |
15
|
|
|
* @var ValidatorInterface |
16
|
|
|
*/ |
17
|
|
|
private $faceValidator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string[] $faces |
21
|
|
|
* @param ValidatorInterface $faceValidator |
22
|
|
|
*/ |
23
|
|
|
public function __construct( |
24
|
|
|
array $faces, |
25
|
|
|
ValidatorInterface $faceValidator = null |
26
|
|
|
) { |
27
|
|
|
$this->faces = $faces; |
28
|
|
|
$this->faceValidator = $faceValidator; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritDoc |
33
|
|
|
*/ |
34
|
|
|
public function validate($value, array $path = []) |
35
|
|
|
{ |
36
|
|
|
if (!is_array($value)) { |
37
|
|
|
throw new ValidationException('Is empty', $path); |
38
|
|
|
} |
39
|
|
|
$missingFaces = $this->faces; |
40
|
|
|
$extraFaces = []; |
41
|
|
|
foreach ($value as $face => $grid) { |
42
|
|
|
if (in_array($face, $this->faces)) { |
43
|
|
|
$missingFaces = array_diff($missingFaces, [$face]); |
44
|
|
|
} else { |
45
|
|
|
$extraFaces[] = $face; |
46
|
|
|
} |
47
|
|
|
if ($this->faceValidator) { |
48
|
|
|
$facePath = array_merge($path, [$face]); |
49
|
|
|
$this->faceValidator->validate($grid, $facePath); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
View Code Duplication |
if (!empty($missingFaces)) { |
|
|
|
|
53
|
|
|
$message = 'Following faces were not found: ' . |
54
|
|
|
implode(', ', $missingFaces); |
55
|
|
|
throw new ValidationException($message); |
56
|
|
|
} |
57
|
|
View Code Duplication |
if (!empty($extraFaces)) { |
|
|
|
|
58
|
|
|
$message = 'Following extra faces were found: ' . |
59
|
|
|
implode(', ', $extraFaces); |
60
|
|
|
throw new ValidationException($message); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.