1: <?php
2:
3: /**
4: * Deep
5: *
6: * @package rsanchez\Deep
7: * @author Rob Sanchez <info@robsanchez.com>
8: */
9:
10: namespace rsanchez\Deep\Repository;
11:
12: use rsanchez\Deep\Collection\ChannelCollection;
13: use rsanchez\Deep\Model\Channel;
14: use rsanchez\Deep\Repository\RepositoryInterface;
15:
16: /**
17: * Repository of all Channels
18: */
19: class ChannelRepository implements RepositoryInterface
20: {
21: /**
22: * Repository Channel Model
23: * @var \rsanchez\Deep\Model\Channel
24: */
25: protected $model;
26:
27: /**
28: * Collection of all Channels
29: * @var \rsanchez\Deep\Collection\ChannelCollection
30: */
31: protected $collection;
32:
33: /**
34: * Array of Channels keyed by channel_id
35: * @var array
36: */
37: protected $channelsById = array();
38:
39: /**
40: * Array of Channels keyed by channel_name
41: * @var array
42: */
43: protected $channelsByName = array();
44:
45: /**
46: * Constructor
47: *
48: * @param \rsanchez\Deep\Model\Channel $model
49: */
50: public function __construct(Channel $model)
51: {
52: $this->model = $model;
53:
54: $this->collection = $this->model->all();
55:
56: foreach ($this->collection as $channel) {
57: $this->channelsById[$channel->channel_id] = $channel;
58: $this->channelsByName[$channel->channel_name] = $channel;
59: }
60: }
61:
62: /**
63: * Get Collection of Channels by channel ID
64: *
65: * @var array $channelIds
66: * @return \rsanchez\Deep\Collection\ChannelCollection
67: */
68: public function getChannelsById(array $channelIds)
69: {
70: if (empty($channelIds)) {
71: return new ChannelCollection();
72: }
73:
74: return $this->collection->filter(function ($channel) use ($channelIds) {
75: return in_array($channel->channel_id, $channelIds);
76: });
77: }
78:
79: /**
80: * Get Collection of Channels by channel name
81: *
82: * @var array $channelNames
83: * @return \rsanchez\Deep\Collection\ChannelCollection
84: */
85: public function getChannelsByName(array $channelNames)
86: {
87: if (empty($channelNames)) {
88: return new ChannelCollection();
89: }
90:
91: return $this->collection->filter(function ($channel) use ($channelNames) {
92: return in_array($channel->channel_name, $channelNames);
93: });
94: }
95:
96: /**
97: * Alias to getChannelById
98: * @var int $id
99: * @return \rsanchez\Deep\Model\Channel|null
100: */
101: public function find($id)
102: {
103: return $this->getChannelById($id);
104: }
105:
106: /**
107: * Get single Channel by channel ID
108: *
109: * @var int $channelId
110: * @return \rsanchez\Deep\Model\Channel|null
111: */
112: public function getChannelById($channelId)
113: {
114: return array_key_exists($channelId, $this->channelsById) ? $this->channelsById[$channelId] : null;
115: }
116:
117: /**
118: * Get single Channel by channel name
119: *
120: * @var string $channelName
121: * @return \rsanchez\Deep\Model\Channel|null
122: */
123: public function getChannelByName($channelName)
124: {
125: return array_key_exists($channelName, $this->channelsByName) ? $this->channelsByName[$channelName] : null;
126: }
127:
128: /**
129: * Get the Channel model
130: * @return \rsanchez\Deep\Model\Channel
131: */
132: public function getModel()
133: {
134: return $this->model;
135: }
136: }
137: