ContainsInstancesOfConstraint   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 33
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A matches() 0 12 3
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Validation\Constraint;
26
27
use Brickoo\Component\Common\Assert;
28
29
/**
30
 * ContainsInstancesOfConstraint
31
 *
32
 * Constraint to assert that an array or traversable
33
 * contains just values of the expected instance type.
34
 * @author Celestino Diaz <[email protected]>
35
 */
36
class ContainsInstancesOfConstraint implements Constraint {
37
38
    /** @var string */
39
    private $expectedInstanceOf;
40
41
    /**
42
     * Class constructor.
43
     * @param string $expectedInstanceType the values expected type
44
     * @throws \InvalidArgumentException if an argument is not valid.
45
     */
46 2
    public function __construct($expectedInstanceType) {
47 2
        Assert::isString($expectedInstanceType);
48 1
        $this->expectedInstanceOf = $expectedInstanceType;
49 1
    }
50
51
    /**
52
     * {@inheritDoc}
53
     * @param array|\Traversable $traversable
54
     */
55 5
    public function matches($traversable) {
56 5
        Assert::isTraversable($traversable);
57
58 4
        $result = true;
59 4
        foreach ($traversable as $value) {
60 4
            if (!$value instanceof $this->expectedInstanceOf) {
61 2
                $result = false;
62 2
                break;
63
            }
64 4
        }
65 4
        return $result;
66
    }
67
68
}
69