TileFaceValidator::validate()   B
last analyzed

Complexity

Conditions 8
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
eloc 12
nc 3
nop 2
dl 17
loc 17
rs 7.7777
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 View Code Duplication
class TileFaceValidator implements ValidatorInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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