|
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 Cycle\ORM\Promise\Collection\CollectionPromiseInterface; |
|
15
|
|
|
use Cycle\ORM\Promise\PromiseInterface; |
|
16
|
|
|
use Doctrine\Common\Collections\AbstractLazyCollection; |
|
17
|
|
|
use Doctrine\Common\Collections\Criteria; |
|
18
|
|
|
use Doctrine\Common\Collections\Selectable; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Collection at top of pivoted (entity + context entity) promise. |
|
22
|
|
|
*/ |
|
23
|
|
|
final class PivotedCollectionPromise extends AbstractLazyCollection implements |
|
24
|
|
|
CollectionPromiseInterface, |
|
25
|
|
|
PivotedCollectionInterface, |
|
26
|
|
|
Selectable |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var PivotedPromise */ |
|
29
|
|
|
protected $promise; |
|
30
|
|
|
|
|
31
|
|
|
/** @var PivotedCollectionInterface */ |
|
32
|
|
|
protected $collection; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param PivotedPromise $promise |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(PivotedPromise $promise) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->promise = $promise; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getPromise(): PromiseInterface |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->promise; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function hasPivot($element): bool |
|
54
|
|
|
{ |
|
55
|
|
|
$this->initialize(); |
|
56
|
|
|
return $this->collection->hasPivot($element); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getPivot($element) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->initialize(); |
|
65
|
|
|
return $this->collection->getPivot($element); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritdoc |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setPivot($element, $pivot): void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->initialize(); |
|
74
|
|
|
$this->collection->setPivot($element, $pivot); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritdoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getPivotContext(): \SplObjectStorage |
|
81
|
|
|
{ |
|
82
|
|
|
$this->initialize(); |
|
83
|
|
|
return $this->collection->getPivotContext(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritDoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function matching(Criteria $criteria) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->initialize(); |
|
92
|
|
|
|
|
93
|
|
|
return $this->collection->matching($criteria); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function doInitialize(): void |
|
100
|
|
|
{ |
|
101
|
|
|
$storage = $this->promise->__resolve(); |
|
102
|
|
|
$this->collection = new PivotedCollection($storage->getElements(), $storage->getContext()); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|