Collection   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 44
c 2
b 0
f 0
dl 0
loc 96
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A change() 0 11 4
A add() 0 6 1
A getChanges() 0 10 2
A remove() 0 8 2
A set() 0 4 1
A removeElement() 0 9 2
A castElement() 0 4 2
A getArrayCopy() 0 11 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Collection;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
8
class Collection extends ArrayCollection
9
{
10
    protected $changes = [
11
        'removed' => [],
12
        'added'   => []
13
    ];
14
    /**
15
     * @var callable
16
     */
17
    protected $castingFunction;
18
19
    public function __construct(array $elements = [], callable $castingFunction = null)
20
    {
21
        parent::__construct($elements);
22
        $this->changes['removed'] = new ArrayCollection();
23
        $this->changes['added']   = new ArrayCollection();
24
        $this->castingFunction = $castingFunction;
25
    }
26
27
    protected function castElement($data)
28
    {
29
        $castFunction = $this->castingFunction;
30
        return $castFunction ? call_user_func($castFunction, $data) : $data;
31
    }
32
33
    public function add($element)
34
    {
35
        $element = $this->castElement($element);
36
        $this->change('added', $element);
37
38
        return parent::add($element);
39
    }
40
41
    public function set($key, $value)
42
    {
43
        $value = $this->castElement($value);
44
        parent::set($key, $value);
45
    }
46
47
    public function remove($key)
48
    {
49
        $removed = parent::remove($key);
50
        if ($removed) {
51
            $this->change('removed', $removed);
52
        }
53
54
        return $removed;
55
    }
56
57
    public function removeElement($element)
58
    {
59
        $element = $this->castElement($element);
60
        $removed = parent::removeElement($element);
61
        if ($removed) {
62
            $this->change('removed', $element);
63
        }
64
65
        return $removed;
66
    }
67
68
    public function getChanges(): array
69
    {
70
        $changes = [];
71
        foreach (array_keys($this->changes) as $t) {
72
            /** @var ArrayCollection $changeCollection */
73
            $changeCollection = $this->changes[$t];
74
            $changes[$t]      = $changeCollection->getValues();
75
        }
76
77
        return $changes;
78
    }
79
80
    public function getArrayCopy()
81
    {
82
        $result = [];
83
        foreach ($this as $element) {
84
            if (is_object($element) && method_exists($element, 'getArrayCopy')) {
85
                $result[] = $element->getArrayCopy();
86
            } else {
87
                $result[] = $element;
88
            }
89
        }
90
        return $result;
91
    }
92
93
    protected function change($type, $element)
94
    {
95
        foreach (array_keys($this->changes) as $t) {
96
            /** @var ArrayCollection $changeCollection */
97
            $changeCollection = $this->changes[$t];
98
            if ($t == $type) {
99
                if (! $changeCollection->contains($element)) {
100
                    $changeCollection->add($element);
101
                }
102
            } else {
103
                $this->removeElement($element);
104
            }
105
        }
106
    }
107
}
108