UniqueObjectCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Xsolve\Associate\ObjectCollection;
4
5
class UniqueObjectCollection implements ObjectCollectionInterface
6
{
7
    /**
8
     * @var \SplObjectStorage
9
     */
10
    protected $objects;
11
12
    public function __construct()
13
    {
14
        $this->objects = new \SplObjectStorage();
15
    }
16
17
    /**
18
     * {@inheritdoc}
19
     */
20
    public function addOne($object)
21
    {
22
        if (!is_object($object)) {
23
            $object = new NonObjectWrapper($object);
24
        }
25
26
        $this->objects->attach($object);
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function addMany(array $objects)
33
    {
34
        foreach ($objects as $object) {
35
            $this->addOne($object);
36
        }
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getAll(): array
43
    {
44
        $objects = [];
45
        foreach ($this->objects as $object) {
46
            $objects[] = ($object instanceof NonObjectWrapper)
47
                ? $object->unwrap()
48
                : $object;
49
        }
50
51
        return $objects;
52
    }
53
}
54