UnmetSpecificationException::createFor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\Specification\Exception;
4
5
use BenTools\Specification\Specification;
6
7
class UnmetSpecificationException extends \RuntimeException implements \Countable
8
{
9
10
    /**
11
     * @var Specification[]
12
     */
13
    private $specifications = [];
14
15
    /**
16
     * @inheritDoc
17
     */
18
    public function __construct(Specification ...$specifications)
19
    {
20
        parent::__construct('Unmet specification.');
21
        $this->specifications = $specifications;
22
    }
23
24
    /**
25
     * @param Specification[] ...$specifications
26
     * @return UnmetSpecificationException
27
     */
28
    public static function createFor(Specification ...$specifications): self
29
    {
30
        return new self(...$specifications);
31
    }
32
33
    /**
34
     * @param Specification[] ...$specifications
35
     * @return UnmetSpecificationException
36
     */
37
    public function withUnmetSpecifications(Specification ...$specifications): self
38
    {
39
        $clone = new self(...$this->specifications);
40
        foreach ($specifications as $specification) {
41
            if (!in_array($specification, $clone->specifications, true)) {
42
                $clone->specifications[] = $specification;
43
            }
44
        }
45
        return $clone;
46
    }
47
48
    /**
49
     * @return Specification[]
50
     */
51
    public function getUnmetSpecifications(): array
52
    {
53
        return $this->specifications;
54
    }
55
56
    /**
57
     * @throws UnmetSpecificationException
58
     */
59
    public function throwIfUnmet()
60
    {
61
        if (count($this) > 0) {
62
            throw $this;
63
        }
64
    }
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function count(): int
70
    {
71
        return count($this->specifications);
72
    }
73
}
74