Completed
Push — develop ( dae12f...f18ec7 )
by Daan van
02:40
created

EntitySet::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace OpenConext\Value\Saml;
4
5
use ArrayIterator;
6
use Countable;
7
use IteratorAggregate;
8
9
final class EntitySet implements Countable, IteratorAggregate
10
{
11
    /**
12
     * @var Entity[]
13
     */
14
    private $entities = array();
15
16
    /**
17
     * @param Entity[] $entities
18
     */
19
    public function __construct(array $entities = array())
20
    {
21
        foreach ($entities as $entity) {
22
            $this->initializeWith($entity);
23
        }
24
    }
25
26
    /**
27
     * @param Entity $entity The entity to search for.
28
     * @return boolean 'true' if the collection contains the element, 'false' otherwise.
29
     */
30
    public function contains(Entity $entity)
31
    {
32
        foreach ($this->entities as $existingEntity) {
33
            if ($entity->equals($existingEntity)) {
34
                return true;
35
            }
36
        }
37
38
        return false;
39
    }
40
41
    /**
42
     * @param EntitySet $other
43
     * @return bool
44
     */
45
    public function equals(EntitySet $other)
46
    {
47
        if (count($this->entities) !== count($other->entities)) {
48
            return false;
49
        }
50
51
        foreach ($this->entities as $entity) {
52
            if (!$other->contains($entity)) {
53
                return false;
54
            }
55
        }
56
57
        return true;
58
    }
59
60
    public function getIterator()
61
    {
62
        return new ArrayIterator($this->entities);
63
    }
64
65
    public function count()
66
    {
67
        return count($this->entities);
68
    }
69
70
    /**
71
     * @param Entity $entity
72
     */
73
    private function initializeWith(Entity $entity)
74
    {
75
        if ($this->contains($entity)) {
76
            return;
77
        }
78
79
        $this->entities[] = $entity;
80
    }
81
}
82