Completed
Push — master ( 57c0ee...30a057 )
by Tom
10s
created

AllowRemoveByReference   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 24
ccs 9
cts 10
cp 0.9
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 18 3
1
<?php
2
3
namespace DoctrineModule\Stdlib\Hydrator\Strategy;
4
5
/**
6
 * When this strategy is used for Collections, if the new collection does not contain elements that are present in
7
 * the original collection, then this strategy remove elements from the original collection. For instance, if the
8
 * collection initially contains elements A and B, and that the new collection contains elements B and C, then the
9
 * final collection will contain elements B and C (while element A will be asked to be removed).
10
 *
11
 * This strategy is by reference, this means it won't use public API to add/remove elements to the collection
12
 *
13
 * @license MIT
14
 * @link    http://www.doctrine-project.org/
15
 * @since   0.7.0
16
 * @author  Michael Gallego <[email protected]>
17
 */
18
class AllowRemoveByReference extends AbstractCollectionStrategy
19
{
20
    /**
21
     * {@inheritDoc}
22
     */
23 5
    public function hydrate($value)
24
    {
25 5
        $collection      = $this->getCollectionFromObjectByReference();
26 5
        $collectionArray = $collection->toArray();
27
28 5
        $toAdd    = array_udiff($value, $collectionArray, [$this, 'compareObjects']);
29 5
        $toRemove = array_udiff($collectionArray, $value, [$this, 'compareObjects']);
30
31 5
        foreach ($toAdd as $element) {
32 4
            $collection->add($element);
33
        }
34
35 5
        foreach ($toRemove as $element) {
36
            $collection->removeElement($element);
37
        }
38
39 5
        return $collection;
40
    }
41
}
42