ResolutionConstraintCollection::__construct()   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
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace ArgumentResolver\Resolution;
4
5
class ResolutionConstraintCollection
6
{
7
    /**
8
     * @var ResolutionConstraint[]
9
     */
10
    private $constraints;
11
12
    /**
13
     * @param ResolutionConstraint[] $constraints
14
     */
15
    public function __construct(array $constraints)
16
    {
17
        $this->constraints = $constraints;
18
    }
19
20
    /**
21
     * @param int   $type
22
     * @param array $withParameters
23
     *
24
     * @return bool
25
     */
26
    public function hasConstraint($type, array $withParameters = [])
27
    {
28
        return count($this->getConstraints($type, $withParameters)) > 0;
29
    }
30
31
    /**
32
     * @param int   $type
33
     * @param array $withParameters
34
     *
35
     * @return ResolutionConstraint[]
36
     */
37
    public function getConstraints($type, array $withParameters = [])
38
    {
39
        $constraints = [];
40
        foreach ($this->constraints as $constraint) {
41
            if ($constraint->getType() === $type && $this->constraintHasParameters($constraint, $withParameters)) {
42
                $constraints[] = $constraint;
43
            }
44
        }
45
46
        return $constraints;
47
    }
48
49
    /**
50
     * @param ResolutionConstraint $constraint
51
     * @param array                $parameters
52
     *
53
     * @return bool
54
     */
55
    private function constraintHasParameters(ResolutionConstraint $constraint, array $parameters)
56
    {
57
        $constraintParameters = $constraint->getParameters();
58
        foreach ($parameters as $name => $value) {
59
            if (!array_key_exists($name, $constraintParameters)) {
60
                return false;
61
            } elseif (null !== $value && $constraintParameters[$name] !== $value) {
62
                return false;
63
            }
64
        }
65
66
        return true;
67
    }
68
}
69