Completed
Push — master ( a47a63...7aba9f )
by Nikola
9s
created

CompositeConstraint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Version package.
5
 *
6
 * Copyright (c) Nikola Posa <[email protected]>
7
 *
8
 * For full copyright and license information, please refer to the LICENSE file,
9
 * located at the package root folder.
10
 */
11
12
namespace Version\Constraint;
13
14
use Version\Version;
15
use Version\Exception\InvalidCompositeConstraintException;
16
17
/**
18
 * @author Nikola Posa <[email protected]>
19
 */
20
class CompositeConstraint implements ConstraintInterface
21
{
22
    const TYPE_AND = 'AND';
23
    const TYPE_OR = 'OR';
24
25
    /**
26
     * @var string
27
     */
28
    protected $type;
29
30
    /**
31
     * @var ConstraintInterface[]
32
     */
33
    protected $constraints;
34
35 8
    private function __construct()
36
    {
37 8
    }
38
39
    /**
40
     * @param string $type
41
     * @param array $constraints
42
     * @return self
43
     */
44 10
    public static function fromProperties($type, array $constraints)
45
    {
46 10
        if (!in_array($type, [self::TYPE_AND, self::TYPE_OR])) {
47 1
            throw InvalidCompositeConstraintException::forType($type);
48
        }
49
50 9
        foreach ($constraints as $constraint) {
51 9
            if (!$constraint instanceof ConstraintInterface) {
52 1
                throw InvalidCompositeConstraintException::forConstraint($constraint);
53
            }
54 9
        }
55
56 8
        $compositeConstraint = new self();
57
58 8
        $compositeConstraint->type = $type;
59 8
        $compositeConstraint->constraints = $constraints;
60
61 8
        return $compositeConstraint;
62
    }
63
64
    /**
65
     * @param array $constraints
66
     * @return self
67
     */
68 5
    public static function fromAndConstraints(array $constraints)
69
    {
70 5
        return self::fromProperties(self::TYPE_AND, $constraints);
71
    }
72
73
    /**
74
     * @param array $constraints
75
     * @return self
76
     */
77 3
    public static function fromOrConstraints(array $constraints)
78
    {
79 3
        return self::fromProperties(self::TYPE_OR, $constraints);
80
    }
81
82
    /**
83
     * @return string
84
     */
85 5
    public function getType()
86
    {
87 5
        return $this->type;
88
    }
89
90
    /**
91
     * @return array
92
     */
93 3
    public function getConstraints()
94
    {
95 3
        return $this->constraints;
96
    }
97
98
    /**
99
     * {@inheritDoc}
100
     */
101 2
    public function assert(Version $version)
102
    {
103 2
        if ($this->type == self::TYPE_AND) {
104 1
            return $this->assertAnd($version);
105
        }
106
107 1
        return $this->assertOr($version);
108
    }
109
110 1
    protected function assertAnd(Version $version)
111
    {
112 1
        foreach ($this->constraints as $constraint) {
113
            /* @var $constraint ConstraintInterface */
114
115 1
            if (!$constraint->assert($version)) {
116 1
                return false;
117
            }
118 1
        }
119
120 1
        return true;
121
    }
122
123 1
    protected function assertOr(Version $version)
124
    {
125 1
        foreach ($this->constraints as $constraint) {
126
            /* @var $constraint ConstraintInterface */
127
128 1
            if ($constraint->assert($version)) {
129 1
                return true;
130
            }
131 1
        }
132
133 1
        return false;
134
    }
135
}
136