|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Relations; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use MongoDB\Driver\Query; |
|
7
|
|
|
use Nip\Records\AbstractModels\Record; |
|
8
|
|
|
use Nip\Records\Collections\Collection; |
|
9
|
|
|
use Nip\Records\AbstractModels\RecordManager; |
|
10
|
|
|
use Nip\Records\Relations\Exceptions\ModelNotLoadedInRelation; |
|
11
|
|
|
use Nip\Records\Relations\Traits\HasMorphTypeTrait; |
|
12
|
|
|
use Nip\HelperBroker; |
|
13
|
|
|
use Nip_Helper_Arrays as ArraysHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class MorphToMany |
|
17
|
|
|
* @package Nip\Records\Relations |
|
18
|
|
|
*/ |
|
19
|
|
|
class MorphTo extends BelongsTo |
|
20
|
|
|
{ |
|
21
|
|
|
use HasMorphTypeTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
|
24
|
|
|
* @return string |
|
25
|
|
|
* @throws ModelNotLoadedInRelation |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function getWithClass() |
|
28
|
|
|
{ |
|
29
|
3 |
|
$type = $this->getMorphType(); |
|
30
|
2 |
|
$typePlural = inflector()->pluralize($type); |
|
31
|
2 |
|
return $typePlural; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
* @throws ModelNotLoadedInRelation |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public function getMorphType() |
|
39
|
|
|
{ |
|
40
|
3 |
|
if ($this->getItem() instanceof Record) { |
|
|
|
|
|
|
41
|
2 |
|
return $this->getItem()->{$this->getMorphTypeField()}; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
1 |
|
throw new ModelNotLoadedInRelation( |
|
44
|
1 |
|
$this->debugString() |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param $params |
|
50
|
|
|
* @throws Exception |
|
51
|
|
|
*/ |
|
52
|
1 |
|
public function addParams($params) |
|
53
|
|
|
{ |
|
54
|
1 |
|
$this->checkParamMorphPrefix($params); |
|
55
|
1 |
|
$this->checkParamMorphTypeField($params); |
|
56
|
1 |
|
parent::addParams($params); |
|
57
|
1 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getEagerResults($collection) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($collection->count() < 1) { |
|
65
|
|
|
if ($this->getItem() instanceof Record) { |
|
|
|
|
|
|
66
|
|
|
return $this->getWith()->newCollection(); |
|
67
|
|
|
} else { |
|
68
|
|
|
return new Collection(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
$types = $this->getTypesFromCollection($collection); |
|
72
|
|
|
$results = new Collection(); |
|
73
|
|
|
foreach ($types as $type) { |
|
74
|
|
|
$manager = $this->getModelManagerInstance($type); |
|
75
|
|
|
$query = $this->getEagerQueryType($collection, $manager); |
|
76
|
|
|
$typeCollection = $manager->findByQuery($query); |
|
|
|
|
|
|
77
|
|
|
foreach ($typeCollection as $item) { |
|
78
|
|
|
$results->add($item); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $results; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param Collection $collection |
|
87
|
|
|
* @param $manager |
|
88
|
|
|
* @return Query |
|
89
|
|
|
* @throws Exception |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function getEagerQueryType(Collection $collection, $manager) |
|
92
|
|
|
{ |
|
93
|
2 |
|
$fkList = $this->getEagerFkListType($collection, $manager); |
|
94
|
2 |
|
$query = $manager->newQuery(); |
|
95
|
2 |
|
$query->where($manager->getPrimaryKey() . ' IN ?', $fkList); |
|
96
|
2 |
|
return $query; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Collection $collection |
|
101
|
|
|
* @param RecordManager $manager |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function getEagerFkListType(Collection $collection, $manager) |
|
105
|
|
|
{ |
|
106
|
2 |
|
$foreignKey = $this->getFK(); |
|
107
|
2 |
|
$typeField = $this->getMorphTypeField(); |
|
|
|
|
|
|
108
|
2 |
|
$type = $manager->getMorphName(); |
|
|
|
|
|
|
109
|
2 |
|
$return = []; |
|
110
|
|
|
|
|
111
|
2 |
|
foreach ($collection as $item) { |
|
112
|
2 |
|
if ($item->{$typeField} == $type) { |
|
113
|
2 |
|
$return[] = $item->{$foreignKey}; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
return array_unique($return); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
|
121
|
|
|
* @param Record $record |
|
122
|
|
|
* @return array |
|
123
|
|
|
* @throws \Exception |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getDictionaryKey(Record $record) |
|
126
|
|
|
{ |
|
127
|
|
|
$key = $record->getManager()->getMorphName(); |
|
128
|
|
|
$key .= '-' . $record->getPrimaryKey(); |
|
129
|
|
|
|
|
130
|
|
|
return $key; |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $dictionary |
|
135
|
|
|
* @param $collection |
|
136
|
|
|
* @param $record |
|
137
|
|
|
* @return mixed |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getResultsFromCollectionDictionary($dictionary, $collection, $record) |
|
140
|
|
|
{ |
|
141
|
|
|
$dictionaryKey = $record->{$this->getMorphTypeField()}; |
|
|
|
|
|
|
142
|
|
|
$dictionaryKey .= '-' . $record->{$this->getFK()}; |
|
143
|
|
|
if (isset($dictionary[$dictionaryKey])) { |
|
144
|
|
|
return $dictionary[$dictionaryKey]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param Query $query |
|
152
|
|
|
* @param array $fkList |
|
153
|
|
|
* @return Query |
|
154
|
|
|
* @throws Exception |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function populateEagerQueryFromFkList($query, $fkList) |
|
157
|
|
|
{ |
|
158
|
|
|
return $query; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param $collection |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getTypesFromCollection($collection) |
|
166
|
|
|
{ |
|
167
|
|
|
$type = $this->getMorphTypeField(); |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** @var ArraysHelper $arrayHelper */ |
|
170
|
|
|
$arrayHelper = HelperBroker::get('Arrays'); |
|
171
|
|
|
return $arrayHelper->pluck($collection, $type); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|