TileTreeValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
     * @SuppressWarnings(PHPMD.ElseExpression)
35
     */
36
    public function validate($value, array $path = [])
37
    {
38
        if (!is_array($value)) {
39
            throw new ValidationException('Is empty', $path);
40
        }
41
        $missingFaces = $this->faces;
42
        $extraFaces = [];
43
        foreach ($value as $face => $grid) {
44
            if (in_array($face, $this->faces)) {
45
                $missingFaces = array_diff($missingFaces, [$face]);
46
            } else {
47
                $extraFaces[] = $face;
48
            }
49
            if ($this->faceValidator) {
50
                $facePath = array_merge($path, [$face]);
51
                $this->faceValidator->validate($grid, $facePath);
52
            }
53
        }
54 View Code Duplication
        if (!empty($missingFaces)) {
55
            $message = 'Following faces were not found: ' .
56
                implode(', ', $missingFaces);
57
            throw new ValidationException($message);
58
        }
59 View Code Duplication
        if (!empty($extraFaces)) {
60
            $message = 'Following extra faces were found: ' .
61
                implode(', ', $extraFaces);
62
            throw new ValidationException($message);
63
        }
64
    }
65
}
66