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