CollectionProxy::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Analogue\ORM\System\Proxies;
4
5
use ArrayAccess;
6
use Countable;
7
use JsonSerializable;
8
use IteratorAggregate;
9
use Illuminate\Contracts\Support\Jsonable;
10
use Illuminate\Contracts\Support\Arrayable;
11
use Analogue\ORM\EntityCollection;
12
13
/**
14
 * Class CollectionProxy
15
 *
16
 * @mixin EntityCollection
17
 */
18
class CollectionProxy extends Proxy implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable
19
{
20
    /**
21
     * Underlying Lazyloaded collection
22
     * @var EntityCollection
23
     */
24
    protected $loadedCollection;
25
26
    /**
27
     * Added Items Collection
28
     * @var EntityCollection
29
     */
30
    protected $addedItems;
31
32
    /**
33
     * @param mixed  $parentEntity
34
     * @param string $relation relationship method handled by the proxy.
35
     */
36
    public function __construct($parentEntity, $relation)
37
    {
38
        $this->addedItems = new EntityCollection;
39
40
        parent::__construct($parentEntity, $relation);
41
    }
42
43
    /**
44
     * Add an entity to the proxy collection, weither it's loaded or not
45
     *
46
     * @param mixed $entity
47
     * @return self|void
48
     */
49
    public function add($entity)
50
    {
51
        if ($this->isLoaded()) {
52
            return $this->loadedCollection->add($entity);
53
        } else {
54
            $this->addedItems->add($entity);
55
        }
56
    }
57
58
    /**
59
     * Check if Proxy collection has been lazy-loaded
60
     *
61
     * @return boolean
62
     */
63
    public function isLoaded()
64
    {
65
        return !is_null($this->loadedCollection);
66
    }
67
68
    /**
69
     * Return the underlying collection
70
     *
71
     * @return EntityCollection
72
     */
73
    public function getUnderlyingCollection()
74
    {
75
        return $this->loadedCollection;
76
    }
77
78
    /**
79
     * Return Items that has been added prior to lazy-loading
80
     *
81
     * @return EntityCollection
82
     */
83
    public function getAddedItems()
84
    {
85
        return $this->addedItems;
86
    }
87
88
    /**
89
     * Load the underlying relation
90
     *
91
     * @return void
92
     */
93
    protected function loadOnce()
94
    {
95
        if ($this->isLoaded()) {
96
            return;
97
        }
98
        
99
        $this->loadedCollection = $this->load();
100
101
        foreach ($this->addedItems as $entity) {
102
            $this->loadedCollection->add($entity);
103
        }
104
105
        $this->addedItems = null;
106
    }
107
    
108
    /**
109
     * Count the number of items in the collection.
110
     *
111
     * @return int
112
     */
113
    public function count()
114
    {
115
        $this->loadOnce();
116
117
        return $this->getUnderlyingCollection()->count();
118
    }
119
120
    /**
121
     * Determine if an item exists at an offset.
122
     *
123
     * @param  mixed $key
124
     * @return bool
125
     */
126
    public function offsetExists($key)
127
    {
128
        $this->loadOnce();
129
130
        return $this->getUnderlyingCollection()->offsetExists($key);
131
    }
132
133
    /**
134
     * Get an item at a given offset.
135
     *
136
     * @param  mixed $key
137
     * @return mixed
138
     */
139
    public function offsetGet($key)
140
    {
141
        $this->loadOnce();
142
143
        return $this->getUnderlyingCollection()->offsetGet($key);
144
    }
145
146
    /**
147
     * Set the item at a given offset.
148
     *
149
     * @param mixed $key
150
     * @param mixed $value
151
     */
152
    public function offsetSet($key, $value)
153
    {
154
        $this->loadOnce();
155
156
        $this->getUnderlyingCollection()->offsetSet($key, $value);
157
    }
158
159
    /**
160
     * Unset the item at a given offset.
161
     *
162
     * @param string $key
163
     */
164
    public function offsetUnset($key)
165
    {
166
        $this->loadOnce();
167
168
        $this->getUnderlyingCollection()->offsetUnset($key);
169
    }
170
171
    /**
172
     * Get the collection of items as a plain array.
173
     *
174
     * @return array
175
     */
176
    public function toArray()
177
    {
178
        $this->loadOnce();
179
180
        return $this->getUnderlyingCollection()->toArray();
181
    }
182
183
    /**
184
     * Convert the object into something JSON serializable.
185
     *
186
     * @return array
187
     */
188
    public function jsonSerialize()
189
    {
190
        $this->loadOnce();
191
192
        return $this->getUnderlyingCollection()->jsonSerialize();
193
    }
194
195
    /**
196
     * Get the collection of items as JSON.
197
     *
198
     * @param  int $options
199
     * @return string
200
     */
201
    public function toJson($options = 0)
202
    {
203
        $this->loadOnce();
204
205
        return $this->getUnderlyingCollection()->toJson();
206
    }
207
208
    /**
209
     * Get an iterator for the items.
210
     *
211
     * @return \ArrayIterator
212
     */
213
    public function getIterator()
214
    {
215
        $this->loadOnce();
216
217
        return $this->getUnderlyingCollection()->getIterator();
218
    }
219
220
221
    /**
222
     * @param  $method
223
     * @param  $parameters
224
     * @return mixed
225
     */
226
    public function __call($method, $parameters)
227
    {
228
        if (!$this->isLoaded()) {
229
            $this->loadOnce();
230
        }
231
232
        return call_user_func_array([$this->loadedCollection, $method], $parameters);
233
    }
234
}
235