Passed
Push — master ( 59744a...ef8e28 )
by Anton
02:44
created

PivotedCollection::createFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Cycle DataMapper ORM
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Relation\Pivoted;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
16
/**
17
 * Collection with associated relation context. Attention, pivot context is lost when collection is partitioned or
18
 * filtered.
19
 */
20
final class PivotedCollection extends ArrayCollection implements PivotedCollectionInterface
21
{
22
    /** @var \SplObjectStorage */
23
    protected $pivotContext;
24
25
    /**
26
     * @param array                  $elements
27
     * @param \SplObjectStorage|null $pivotData
28
     */
29
    public function __construct(array $elements = [], \SplObjectStorage $pivotData = null)
30
    {
31
        parent::__construct($elements);
32
        $this->pivotContext = $pivotData ?? new \SplObjectStorage();
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function hasPivot($element): bool
39
    {
40
        return $this->pivotContext->offsetExists($element);
41
    }
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function getPivot($element)
47
    {
48
        return $this->pivotContext[$element] ?? null;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function setPivot($element, $pivot): void
55
    {
56
        $this->pivotContext[$element] = $pivot;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getPivotContext(): \SplObjectStorage
63
    {
64
        return $this->pivotContext;
65
    }
66
67
    /**
68
     * @param array $elements
69
     * @return PivotedCollection
70
     */
71
    protected function createFrom(array $elements)
72
    {
73
        $new = parent::createFrom($elements);
74
        $new->pivotContext = $this->pivotContext;
75
76
        return $new;
77
    }
78
}
79