-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAbstractTitleCollection.php
More file actions
174 lines (150 loc) · 4.24 KB
/
AbstractTitleCollection.php
File metadata and controls
174 lines (150 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* Deep
*
* @package rsanchez\Deep
* @author Rob Sanchez <info@robsanchez.com>
*/
namespace rsanchez\Deep\Collection;
use rsanchez\Deep\Model\Title;
use rsanchez\Deep\Repository\ChannelRepository;
/**
* Collection of \rsanchez\Deep\Model\Title
*/
abstract class AbstractTitleCollection extends AbstractModelCollection implements FilterableInterface
{
use FilterableTrait;
/**
* All of the entry IDs in this collection (including related entries)
* @var array
*/
protected $entryIds = array();
/**
* Channels used by this collection
* @var \rsanchez\Deep\Collection\ChannelCollection
*/
protected $channels;
/**
* Instantiate a collection of models
* @param array $models
* @param \rsanchez\Deep\Repository\ChannelRepository $channelRepository
* @return \rsanchez\Deep\Collection\AbstractTitleCollection
*/
public static function create(array $models, ChannelRepository $channelRepository)
{
$collection = new static($models);
$collection->setChannelRepository($channelRepository);
$channelIds = array();
foreach ($models as $model) {
$collection->entryIds[] = $model->entry_id;
if (! in_array($model->channel_id, $channelIds)) {
$channelIds[] = $model->channel_id;
}
}
$collection->setChannels($channelRepository->getChannelsById($channelIds));
return $collection;
}
/**
* Create a new collection using the current instance's properties
* as injected dependencies.
*
* Useful when making a collection of a subset of this collection
* @param array $models
* @return \rsanchez\Deep\Collection\AbstractTitleCollection
*/
public function createChildCollection(array $models)
{
return self::create($models, $this->channelRepository);
}
/**
* Set the Channel Repository
* @param \rsanchez\Deep\Repository\ChannelRepository $channelRepository
* @return void
*/
public function setChannelRepository(ChannelRepository $channelRepository)
{
$this->channelRepository = $channelRepository;
}
/**
* Set the channels used by this collection
* @param \rsanchez\Deep\Collection\ChannelCollection $channels
* @return void
*/
public function setChannels(ChannelCollection $channels)
{
$this->channels = $channels;
}
/**
* Get the channels used by this collection
* @return \rsanchez\Deep\Collection\ChannelCollection
*/
public function getChannels()
{
return $this->channels;
}
/**
* Get all the entry Ids from this collection.
* This includes both the entries directly in this collection,
* and entries found in Playa/Relationship fields
*
* @return array
*/
public function getEntryIds()
{
return $this->entryIds;
}
/**
* Add an additional entry id to this collection
*
* @param string|int $entryId
* @return void
*/
public function addEntryId($entryId)
{
if (! in_array($entryId, $this->entryIds)) {
$this->entryIds[$entryId] = $entryId;
}
}
/**
* Add additional entry ids to this collection
*
* @param array $entryIds
* @return void
*/
public function addEntryIds(array $entryIds)
{
foreach ($entryIds as $entryId) {
$this->addEntryId($entryId);
}
}
/**
* Whether or not this collection supports custom fields
*
* @return bool
*/
public function hasCustomFields()
{
return false;
}
/**
* Determine if all items in the collection are fully hydrated
*
* @return bool
*/
public function isHydrated()
{
return $this->reduce(function ($carry, $item) {
return $carry && $item->hydrated;
}, true);
}
/**
* {@inheritdoc}
*/
public function toJson($options = 0)
{
if (func_num_args() === 0) {
$options = JSON_NUMERIC_CHECK;
}
return parent::toJson($options);
}
}