Passed
Push — master ( 4e0852...046d68 )
by Adrian
02:13
created

Collection::castElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
        return $this->castingFunction ? $this->castingFunction($data) : $data;
0 ignored issues
show
Bug introduced by
The method castingFunction() does not exist on Sirius\Orm\Collection\Collection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        return $this->castingFunction ? $this->/** @scrutinizer ignore-call */ castingFunction($data) : $data;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
    }
31
32
    public function add($element)
33
    {
34
        $element = $this->castElement($element);
35
        $this->change('added', $element);
36
37
        return parent::add($element);
38
    }
39
40
    public function set($key, $value)
41
    {
42
        $value = $this->castElement($value);
43
        parent::set($key, $value);
44
    }
45
46
    public function remove($key)
47
    {
48
        $removed = parent::remove($key);
49
        if ($removed) {
50
            $this->change('removed', $removed);
51
        }
52
53
        return $removed;
54
    }
55
56
    public function removeElement($element)
57
    {
58
        $element = $this->castElement($element);
59
        $removed = parent::removeElement($element);
60
        if ($removed) {
61
            $this->change('removed', $element);
62
        }
63
64
        return $removed;
65
    }
66
67
    public function getChanges(): array
68
    {
69
        $changes = [];
70
        foreach (array_keys($this->changes) as $t) {
71
            /** @var ArrayCollection $changeCollection */
72
            $changeCollection = $this->changes[$t];
73
            $changes[$t]      = $changeCollection->getValues();
74
        }
75
76
        return $changes;
77
    }
78
79
    public function getArrayCopy()
80
    {
81
        $result = [];
82
        foreach ($this as $element) {
83
            if (is_object($element) && method_exists($element, 'getArrayCopy')) {
84
                $result[] = $element->getArrayCopy();
85
            } else {
86
                $result[] = $element;
87
            }
88
        }
89
        return $result;
90
    }
91
92
    protected function change($type, $element)
93
    {
94
        foreach (array_keys($this->changes) as $t) {
95
            /** @var ArrayCollection $changeCollection */
96
            $changeCollection = $this->changes[$t];
97
            if ($t == $type) {
98
                if (! $changeCollection->contains($element)) {
99
                    $changeCollection->add($element);
100
                }
101
            } else {
102
                $this->removeElement($element);
103
            }
104
        }
105
    }
106
}
107