UniqueObjectCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 10 3
A addMany() 0 4 2
A __construct() 0 3 1
A addOne() 0 7 2
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