Completed
Branch master (a17b64)
by Rémi
15:50
created

CollectionProxy::contains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Analogue\ORM\System\Proxies;
4
5
use Closure;
6
use CachingIterator;
7
use Analogue\ORM\EntityCollection;
8
use ProxyManager\Proxy\ProxyInterface;
9
use Analogue\ORM\System\Manager;
10
11
class CollectionProxy extends EntityCollection implements ProxyInterface
12
{
13
    /** 
14
     * Indicate if the relationship has been lazy loaded
15
     * @var boolean
16
     */
17
    protected $relationshipLoaded = false;
18
19
    protected $addedItems = [];
20
21
	/**
22
     * Create a new collection.
23
     *
24
     * @param mixed $entity
25
     * @param string $relation
26
     * 
27
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
28
     */
29
    public function __construct($entity, $relation)
30
    {
31
        $this->parentEntity = $entity;
32
        $this->relationshipMethod = $relation;
33
    }
34
35
    /**
36
     * Return Items that has been added without lady loading
37
     * the underlying collection
38
     *
39
     * @return array
40
     */
41
    public function getAddedItems()
42
    {
43
        return $this->addedItems;
44
    }
45
46
    /**
47
     * Force initialization of the proxy
48
     *
49
     * @return bool true if the proxy could be initialized
50
     */
51
    public function initializeProxy() : bool
52
    {
53
        if($this->isProxyInitialized() ) return true;
54
        
55
        $relation = $this->relationshipMethod;
56
        $entity = $this->parentEntity;
57
58
        $entityMap = Manager::getMapper($entity)->getEntityMap();
59
60
        $this->items = $entityMap->$relation($entity)->getResults($relation)->all() + $this->addedItems;
61
62
        $this->relationshipLoaded = true;
63
64
        return true;
65
    }
66
67
    /**
68
     * Retrieves current initialization status of the proxy
69
     *
70
     * @return bool
71
     */
72
    public function isProxyInitialized() : bool
73
    {
74
        return $this->relationshipLoaded;
75
    }
76
77
    /**
78
     * Get all of the items in the collection.
79
     *
80
     * @return array
81
     */
82
    public function all()
83
    {
84
    	$this->initializeProxy();
85
86
        return parent::all();
87
    }
88
89
    /**
90
     * Get the average value of a given key.
91
     *
92
     * @param  callable|string|null  $callback
93
     * @return mixed
94
     */
95
    public function avg($callback = null)
96
    {
97
        $this->initializeProxy();
98
99
        return parent::avg($callback);
100
    }
101
102
    /**
103
     * Get the median of a given key.
104
     *
105
     * @param  null $key
106
     * @return mixed|null
107
     */
108
    public function median($key = null)
109
    {
110
        $this->initializeProxy();
111
112
        return parent::median($key);
113
    }
114
115
    /**
116
     * Get the mode of a given key.
117
     *
118
     * @param  mixed  $key
119
     * @return array
120
     */
121
    public function mode($key = null)
122
    {
123
        $this->initializeProxy();
124
125
        return parent::mode($key);
126
    }
127
128
    /**
129
     * Collapse the collection of items into a single array.
130
     *
131
     * @return static
132
     */
133
    public function collapse()
134
    {
135
    	$this->initializeProxy();
136
137
        return parent::collapse();
138
    }
139
140
    /**
141
     * Determine if an item exists in the collection.
142
     *
143
     * @param  mixed  $key
144
     * @param  mixed  $value
145
     * @return bool
146
     */
147
    public function contains($key, $value = null)
148
    {
149
        $this->initializeProxy();
150
151
        return parent::contains($key, $value);
152
    }
153
154
    /**
155
     * Determine if an item exists in the collection using strict comparison.
156
     *
157
     * @param  mixed  $key
158
     * @param  mixed  $value
159
     * @return bool
160
     */
161
    public function containsStrict($key, $value = null)
162
    {
163
        $this->initializeProxy();
164
165
        return parent::containsStrict($key, $value);
166
    }
167
168
    /**
169
     * Get the items in the collection that are not present in the given items.
170
     *
171
     * @param  mixed  $items
172
     * @return static
173
     */
174
    public function diff($items)
175
    {
176
        $this->initializeProxy();
177
178
        return parent::diff($items);
179
    }
180
181
    /**
182
     * Get the items in the collection whose keys are not present in the given items.
183
     *
184
     * @param  mixed  $items
185
     * @return static
186
     */
187
    public function diffKeys($items)
188
    {
189
        $this->initializeProxy();
190
191
        return parent::diffKeys($items);
192
    }
193
194
    /**
195
     * Execute a callback over each item.
196
     *
197
     * @param  callable  $callback
198
     * @return $this
199
     */
200
    public function each(callable $callback)
201
    {
202
        $this->initializeProxy();
203
204
        return parent::each($callback);
205
    }
206
207
    /**
208
     * Create a new collection consisting of every n-th element.
209
     *
210
     * @param  int  $step
211
     * @param  int  $offset
212
     * @return static
213
     */
214
    public function every($step, $offset = 0)
215
    {
216
        $this->initializeProxy();
217
218
        return parent::every($step, $offset);
219
    }
220
221
    /**
222
     * Get all items except for those with the specified keys.
223
     *
224
     * @param  mixed  $keys
225
     * @return static
226
     */
227
    public function except($keys)
228
    {
229
        $this->initializeProxy();
230
231
        return parent::except($keys);
232
    }
233
234
    /**
235
     * Run a filter over each of the items.
236
     *
237
     * @param  callable|null  $callback
238
     * @return static
239
     */
240
    public function filter(callable $callback = null)
241
    {
242
        $this->initializeProxy();
243
244
        return parent::filter($callback);
245
    }
246
247
    /**
248
     * Filter items by the given key value pair.
249
     *
250
     * @param  string  $key
251
     * @param  mixed  $operator
252
     * @param  mixed  $value
253
     * @return static
254
     */
255
    public function where($key, $operator, $value = null)
256
    {
257
        $this->initializeProxy();
258
259
        return parent::where($key, $operator, $value);
260
    }
261
262
    /**
263
     * Filter items by the given key value pair using strict comparison.
264
     *
265
     * @param  string  $key
266
     * @param  mixed  $value
267
     * @return static
268
     */
269
    public function whereStrict($key, $value)
270
    {
271
        $this->initializeProxy();
272
273
        return parent::whereStrict($key, $value);
274
    }
275
276
    /**
277
     * Filter items by the given key value pair.
278
     *
279
     * @param  string  $key
280
     * @param  mixed  $values
281
     * @param  bool  $strict
282
     * @return static
283
     */
284
    public function whereIn($key, $values, $strict = false)
285
    {
286
        $this->initializeProxy();
287
288
        return parent::whereIn($key, $values, $strict);
289
    }
290
291
    /**
292
     * Filter items by the given key value pair using strict comparison.
293
     *
294
     * @param  string  $key
295
     * @param  mixed  $values
296
     * @return static
297
     */
298
    public function whereInStrict($key, $values)
299
    {
300
    	$this->initializeProxy();
301
302
        return parent::whereInStrict($key, $values);
303
    }
304
305
    /**
306
     * Get the first item from the collection.
307
     *
308
     * @param  callable|null  $callback
309
     * @param  mixed  $default
310
     * @return mixed
311
     */
312
    public function first(callable $callback = null, $default = null)
313
    {
314
    	// TODO Consider partial loading
315
    	$this->initializeProxy();
316
317
        return parent::first($callback, $default);
318
    }
319
320
    /**
321
     * Get a flattened array of the items in the collection.
322
     *
323
     * @param  int  $depth
324
     * @return static
325
     */
326
    public function flatten($depth = INF)
327
    {
328
        $this->initializeProxy();
329
330
        return parent::flatten($depth);
331
    }
332
333
    /**
334
     * Flip the items in the collection.
335
     *
336
     * @return static
337
     */
338
    public function flip()
339
    {
340
        $this->initializeProxy();
341
342
        return parent::flip();
343
    }
344
345
    /**
346
     * Remove an item from the collection by key.
347
     *
348
     * @param  string|array  $keys
349
     * @return $this
350
     */
351
    public function forget($keys)
352
    {
353
    	// TODO, we could consider these as 
354
    	// 'pending deletion', the same way that
355
    	// we treat added items
356
        $this->initializeProxy();
357
358
        return parent::forget($keys);
359
    }
360
361
    /**
362
     * Get an item from the collection by key.
363
     *
364
     * @param  mixed  $key
365
     * @param  mixed  $default
366
     * @return mixed
367
     */
368
    public function get($key, $default = null)
369
    {
370
    	// TODO : We could also consider partial loading
371
    	// here
372
        $this->initializeProxy();
373
374
        return parent::get($key, $default);
375
    }
376
377
    /**
378
     * Group an associative array by a field or using a callback.
379
     *
380
     * @param  callable|string  $groupBy
381
     * @param  bool  $preserveKeys
382
     * @return static
383
     */
384
    public function groupBy($groupBy, $preserveKeys = false)
385
    {
386
        $this->initializeProxy();
387
388
        return parent::groupBy($groupBy, $preserveKeys);
389
    }
390
391
    /**
392
     * Key an associative array by a field or using a callback.
393
     *
394
     * @param  callable|string  $keyBy
395
     * @return static
396
     */
397
    public function keyBy($keyBy)
398
    {
399
        $this->initializeProxy();
400
401
        return parent::keyBy($keyBy);
402
    }
403
404
    /**
405
     * Determine if an item exists in the collection by key.
406
     *
407
     * @param  mixed  $key
408
     * @return bool
409
     */
410
    public function has($key)
411
    {
412
    	// TODO : we could do automagic here by directly
413
    	// calling the database if the collection hasn't 
414
    	// been initialized yet. 
415
    	// Potential issue is that several calls to this
416
    	// could cause a lot queries vs a single get query.
417
        $this->initializeProxy();
418
419
        return parent::has($key);
420
    }
421
422
    /**
423
     * Concatenate values of a given key as a string.
424
     *
425
     * @param  string  $value
426
     * @param  string  $glue
427
     * @return string
428
     */
429
    public function implode($value, $glue = null)
430
    {
431
        $this->initializeProxy();
432
433
        return parent::implode($value, $glue);
434
    }
435
436
    /**
437
     * Intersect the collection with the given items.
438
     *
439
     * @param  mixed  $items
440
     * @return static
441
     */
442
    public function intersect($items)
443
    {
444
        $this->initializeProxy();
445
446
        return parent::intersect($items);
447
    }
448
449
    /**
450
     * Determine if the collection is empty or not.
451
     *
452
     * @return bool
453
     */
454
    public function isEmpty()
455
    {
456
        $this->initializeProxy();
457
458
        return parent::isEmpty();
459
    }
460
461
    /**
462
     * Get the keys of the collection items.
463
     *
464
     * @return static
465
     */
466
    public function keys()
467
    {
468
        $this->initializeProxy();
469
470
        return parent::keys();
471
    }
472
473
    /**
474
     * Get the last item from the collection.
475
     *
476
     * @param  callable|null  $callback
477
     * @param  mixed  $default
478
     * @return mixed
479
     */
480
    public function last(callable $callback = null, $default = null)
481
    {
482
    	// TODO : we could do partial loading there as well
483
        $this->initializeProxy();
484
485
        return parent::last($callback, $default);
486
    }
487
488
    /**
489
     * Get the values of a given key.
490
     *
491
     * @param  string  $value
492
     * @param  string|null  $key
493
     * @return static
494
     */
495
    public function pluck($value, $key = null)
496
    {
497
    	// TODO : automagic call to QB if not initialized
498
        $this->initializeProxy();
499
500
        return parent::pluck($value, $key);
501
    }
502
503
    /**
504
     * Run a map over each of the items.
505
     *
506
     * @param  callable  $callback
507
     * @return static
508
     */
509
    public function map(callable $callback)
510
    {
511
        $this->initializeProxy();
512
513
        return parent::map($callback);
514
    }
515
516
    /**
517
     * Run an associative map over each of the items.
518
     *
519
     * The callback should return an associative array with a single key/value pair.
520
     *
521
     * @param  callable  $callback
522
     * @return static
523
     */
524
    public function mapWithKeys(callable $callback)
525
    {
526
        $this->initializeProxy();
527
528
        return parent::mapWithKeys($callback);
529
    }
530
531
    /**
532
     * Map a collection and flatten the result by a single level.
533
     *
534
     * @param  callable  $callback
535
     * @return static
536
     */
537
    public function flatMap(callable $callback)
538
    {
539
        $this->initializeProxy();
540
541
        return parent::flatMap($callback);
542
    }
543
544
    /**
545
     * Get the max value of a given key.
546
     *
547
     * @param  callable|string|null  $callback
548
     * @return mixed
549
     */
550
    public function max($callback = null)
551
    {
552
        $this->initializeProxy();
553
554
        return parent::max($callback);
555
    }
556
557
    /**
558
     * Merge the collection with the given items.
559
     *
560
     * @param  mixed  $items
561
     * @return static
562
     */
563
    public function merge($items)
564
    {
565
    	// TODO : Check if the EntityCollection
566
    	// returns a native Collection, as it 
567
    	// is what we want here
568
        $this->initializeProxy();
569
570
        return parent::merge($items);
571
    }
572
573
    /**
574
     * Create a collection by using this collection for keys and another for its values.
575
     *
576
     * @param  mixed  $values
577
     * @return static
578
     */
579
    public function combine($values)
580
    {
581
        // TODO : Check if the EntityCollection
582
    	// returns a native Collection, as it 
583
    	// is what we want here
584
        $this->initializeProxy();
585
586
        return parent::combine($values);
587
    }
588
589
    /**
590
     * Union the collection with the given items.
591
     *
592
     * @param  mixed  $items
593
     * @return static
594
     */
595
    public function union($items)
596
    {
597
        // TODO : Check if the EntityCollection
598
    	// returns a native Collection, as it 
599
    	// is what we want here
600
        $this->initializeProxy();
601
602
        return parent::union($items);
603
    }
604
605
    /**
606
     * Get the min value of a given key.
607
     *
608
     * @param  callable|string|null  $callback
609
     * @return mixed
610
     */
611
    public function min($callback = null)
612
    {
613
        // TODO : we could rely on the QB
614
        // for thos, if initialization has not
615
        // take place yet
616
        $this->initializeProxy();
617
618
        return parent::min($callback);
619
    }
620
621
    /**
622
     * Get the items with the specified keys.
623
     *
624
     * @param  mixed  $keys
625
     * @return static
626
     */
627
    public function only($keys)
628
    {
629
        // TODO : we could rely on the QB if
630
        // the collection hasn't been initialized yet
631
        $this->initializeProxy();
632
633
        return parent::only($keys);
634
    }
635
636
    /**
637
     * "Paginate" the collection by slicing it into a smaller collection.
638
     *
639
     * @param  int  $page
640
     * @param  int  $perPage
641
     * @return static
642
     */
643
    public function forPage($page, $perPage)
644
    {
645
    	// TODO : check possibility of partial loading
646
    	// if not initialized
647
        $this->initializeProxy();
648
649
        return parent::forPage($page, $perPage);
650
    }
651
652
    /**
653
     * Partition the collection into two arrays using the given callback or key.
654
     *
655
     * @param  callable|string  $callback
656
     * @return static
657
     */
658
    public function partition($callback)
659
    {
660
        $this->initializeProxy();
661
662
        return parent::partition($callback);
663
    }
664
665
    /**
666
     * Pass the collection to the given callback and return the result.
667
     *
668
     * @param  callable $callback
669
     * @return mixed
670
     */
671
    public function pipe(callable $callback)
672
    {
673
        $this->initializeProxy();
674
675
        return parent::pipe($callback);
676
    }
677
678
    /**
679
     * Get and remove the last item from the collection.
680
     *
681
     * @return mixed
682
     */
683
    public function pop()
684
    {
685
        $this->initializeProxy();
686
687
        return parent::pop();
688
    }
689
690
    /**
691
     * Push an item onto the beginning of the collection.
692
     *
693
     * @param  mixed  $value
694
     * @param  mixed  $key
695
     * @return $this
696
     */
697
    public function prepend($value, $key = null)
698
    {
699
    	// TODO : partial adding of values.
700
    	// we could have a $prepended , and $pushed arrays
701
    	// which we would combine at full initialization
702
703
        $this->initializeProxy();
704
705
        return parent::prepend($value, $key);
706
    }
707
708
    /**
709
     * Push an item onto the end of the collection.
710
     *
711
     * @param  mixed  $value
712
     * @return $this
713
     */
714
    public function push($value)
715
    {
716
    	// TODO : partial adding of values.
717
    	// we could have a $prepended , and $pushed arrays
718
    	// which we would combine at full initialization
719
  
720
        $this->initializeProxy();
721
722
        return parent::push($value);
723
    }
724
725
    /**
726
     * Get and remove an item from the collection.
727
     *
728
     * @param  mixed  $key
729
     * @param  mixed  $default
730
     * @return mixed
731
     */
732
    public function pull($key, $default = null)
733
    {
734
    	// TODO : QB query if the collection
735
    	// hasn't been initialized yet
736
737
        $this->initializeProxy();
738
739
        return parent::pull($key, $default);
740
    }
741
742
    /**
743
     * Put an item in the collection by key.
744
     *
745
     * @param  mixed  $key
746
     * @param  mixed  $value
747
     * @return $this
748
     */
749
    public function put($key, $value)
750
    {
751
        // TODO : Partial loading ?
752
753
        $this->initializeProxy();
754
755
        return parent::put($key, $value);
756
    }
757
758
    /**
759
     * Get one or more items randomly from the collection.
760
     *
761
     * @param  int  $amount
762
     * @return mixed
763
     *
764
     * @throws \InvalidArgumentException
765
     */
766
    public function random($amount = 1)
767
    {
768
    	// TODO : we could optimize this by only 
769
    	// fetching the keys from the database
770
    	// and performing partial loading
771
772
        $this->initializeProxy();
773
774
        return parent::random($amount);
775
    }
776
777
    /**
778
     * Reduce the collection to a single value.
779
     *
780
     * @param  callable  $callback
781
     * @param  mixed     $initial
782
     * @return mixed
783
     */
784
    public function reduce(callable $callback, $initial = null)
785
    {
786
        $this->initializeProxy();
787
788
        return parent::reduce($callback, $initial);
789
    }
790
791
    /**
792
     * Create a collection of all elements that do not pass a given truth test.
793
     *
794
     * @param  callable|mixed  $callback
795
     * @return static
796
     */
797
    public function reject($callback)
798
    {
799
        $this->initializeProxy();
800
801
        return parent::reject($callback);
802
    }
803
804
    /**
805
     * Reverse items order.
806
     *
807
     * @return static
808
     */
809
    public function reverse()
810
    {
811
        $this->initializeProxy();
812
813
        return parent::reverse();
814
    }
815
816
    /**
817
     * Search the collection for a given value and return the corresponding key if successful.
818
     *
819
     * @param  mixed  $value
820
     * @param  bool   $strict
821
     * @return mixed
822
     */
823
    public function search($value, $strict = false)
824
    {
825
        $this->initializeProxy();
826
827
        return parent::search($value, $strict);
828
    }
829
830
    /**
831
     * Get and remove the first item from the collection.
832
     *
833
     * @return mixed
834
     */
835
    public function shift()
836
    {
837
    	// Todo : Partial Removing
838
    	// we could have a pending removal array
839
        $this->initializeProxy();
840
841
        return parent::shift();
842
    }
843
844
    /**
845
     * Shuffle the items in the collection.
846
     *
847
     * @param int $seed
848
     * @return static
849
     */
850
    public function shuffle($seed = null)
851
    {
852
    	$this->initializeProxy();
853
854
        return parent::shuffle($seed);
855
    }
856
857
    /**
858
     * Slice the underlying collection array.
859
     *
860
     * @param  int   $offset
861
     * @param  int   $length
862
     * @return static
863
     */
864
    public function slice($offset, $length = null)
865
    {
866
        $this->initializeProxy();
867
868
        return parent::slice($offset, $length);
869
    }
870
871
    /**
872
     * Split a collection into a certain number of groups.
873
     *
874
     * @param  int  $numberOfGroups
875
     * @return static
876
     */
877
    public function split($numberOfGroups)
878
    {
879
        $this->initializeProxy();
880
881
        return parent::split($numberOfGroups);
882
    }
883
884
    /**
885
     * Chunk the underlying collection array.
886
     *
887
     * @param  int   $size
888
     * @return static
889
     */
890
    public function chunk($size)
891
    {
892
    	// TODO : partial loading ?
893
        $this->initializeProxy();
894
895
        return parent::chunk($size);
896
    }
897
898
    /**
899
     * Sort through each item with a callback.
900
     *
901
     * @param  callable|null  $callback
902
     * @return static
903
     */
904
    public function sort(callable $callback = null)
905
    {
906
        $this->initializeProxy();
907
908
        return parent::sort($callback);
909
    }
910
911
    /**
912
     * Sort the collection using the given callback.
913
     *
914
     * @param  callable|string  $callback
915
     * @param  int   $options
916
     * @param  bool  $descending
917
     * @return static
918
     */
919
    public function sortBy($callback, $options = SORT_REGULAR, $descending = false)
920
    {
921
        $this->initializeProxy();
922
923
        return parent::sort($callback, $options, $descending);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (sort() instead of sortBy()). Are you sure this is correct? If so, you might want to change this to $this->sort().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
924
    }
925
926
    /**
927
     * Splice a portion of the underlying collection array.
928
     *
929
     * @param  int  $offset
930
     * @param  int|null  $length
931
     * @param  mixed  $replacement
932
     * @return static
933
     */
934
    public function splice($offset, $length = null, $replacement = [])
935
    {
936
        $this->initializeProxy();
937
938
        return parent::splice($offset, $length, $replacement);
939
    }
940
941
    /**
942
     * Get the sum of the given values.
943
     *
944
     * @param  callable|string|null  $callback
945
     * @return mixed
946
     */
947
    public function sum($callback = null)
948
    {
949
        $this->initializeProxy();
950
951
        return parent::sum($callback);
952
    }
953
954
    /**
955
     * Take the first or last {$limit} items.
956
     *
957
     * @param  int  $limit
958
     * @return static
959
     */
960
    public function take($limit)
961
    {	
962
    	// TODO: partial loading
963
        $this->initializeProxy();
964
965
        return parent::take($limit);
966
    }
967
968
    /**
969
     * Transform each item in the collection using a callback.
970
     *
971
     * @param  callable  $callback
972
     * @return $this
973
     */
974
    public function transform(callable $callback)
975
    {
976
        $this->initializeProxy();
977
978
        return parent::transform($callback);
979
    }
980
981
    /**
982
     * Return only unique items from the collection array.
983
     *
984
     * @param  string|callable|null  $key
985
     * @param  bool  $strict
986
     *
987
     * @return static
988
     */
989
    public function unique($key = null, $strict = false)
990
    {
991
        $this->initializeProxy();
992
993
        return parent::unique($key, $strict);
994
    }
995
996
    /**
997
     * Reset the keys on the underlying array.
998
     *
999
     * @return static
1000
     */
1001
    public function values()
1002
    {
1003
        $this->initializeProxy();
1004
1005
        return parent::values();
1006
    }
1007
1008
    /**
1009
     * Zip the collection together with one or more arrays.
1010
     *
1011
     * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]);
1012
     *      => [[1, 4], [2, 5], [3, 6]]
1013
     *
1014
     * @param  mixed ...$items
1015
     * @return static
1016
     */
1017
    public function zip($items)
1018
    {
1019
        $this->initializeProxy();
1020
1021
        return parent::zip($items);
1022
    }
1023
1024
    /**
1025
     * Get the collection of items as a plain array.
1026
     *
1027
     * @return array
1028
     */
1029
    public function toArray()
1030
    {
1031
    	// If this is called on all subsequent proxy, 
1032
    	// this would eventually trigger all lazy loading,
1033
    	// which is NOT what we would expect... 
1034
    	// TODO : must think of this. 
1035
        $this->initializeProxy();
1036
1037
        return parent::toArray();
1038
    }
1039
1040
    /**
1041
     * Convert the object into something JSON serializable.
1042
     *
1043
     * @return array
1044
     */
1045
    public function jsonSerialize()
1046
    {
1047
        // If this is called on all subsequent proxy, 
1048
    	// this would eventually trigger all lazy loading,
1049
    	// which is NOT what we would expect... 
1050
    	// TODO : must think of this. 
1051
        $this->initializeProxy();
1052
1053
        return parent::jsonSerialize();
1054
    }
1055
1056
    /**
1057
     * Get the collection of items as JSON.
1058
     *
1059
     * @param  int  $options
1060
     * @return string
1061
     */
1062
    public function toJson($options = 0)
1063
    {
1064
        // If this is called on all subsequent proxy, 
1065
    	// this would eventually trigger all lazy loading,
1066
    	// which is NOT what we would expect... 
1067
    	// TODO : must think of this. 
1068
        $this->initializeProxy();
1069
1070
        return parent::toJson($options);
1071
    }
1072
1073
    /**
1074
     * Get an iterator for the items.
1075
     *
1076
     * @return \ArrayIterator
1077
     */
1078
    public function getIterator()
1079
    {
1080
        $this->initializeProxy();
1081
1082
        return parent::getIterator();
1083
    }
1084
1085
    /**
1086
     * Get a CachingIterator instance.
1087
     *
1088
     * @param  int  $flags
1089
     * @return \CachingIterator
1090
     */
1091
    public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING)
1092
    {
1093
        $this->initializeProxy();
1094
1095
        return parent::getCachingIterator($flags);
1096
    }
1097
1098
    /**
1099
     * Count the number of items in the collection.
1100
     *
1101
     * @return int
1102
     */
1103
    public function count()
1104
    {
1105
    	// TODO rely on QB if not initialized
1106
        $this->initializeProxy();
1107
        
1108
        return parent::count();
1109
    }
1110
1111
    /**
1112
     * Get a base Support collection instance from this collection.
1113
     *
1114
     * @return \Illuminate\Support\Collection
1115
     */
1116
    public function toBase()
1117
    {
1118
    	$this->initializeProxy();
1119
1120
        return parent::toBase();
1121
    }
1122
1123
    /**
1124
     * Determine if an item exists at an offset.
1125
     *
1126
     * @param  mixed  $key
1127
     * @return bool
1128
     */
1129
    public function offsetExists($key)
1130
    {
1131
    	// TODO rely on QB if no collection
1132
    	// initialized
1133
    	$this->initializeProxy();
1134
1135
        return parent::offsetExists($key);
1136
    }
1137
1138
    /**
1139
     * Get an item at a given offset.
1140
     *
1141
     * @param  mixed  $key
1142
     * @return mixed
1143
     */
1144
    public function offsetGet($key)
1145
    {
1146
    	// TODO rely on partial init if no collection
1147
    	// initialized
1148
    	$this->initializeProxy();
1149
1150
        return parent::offsetGet($key);
1151
    }
1152
1153
    /**
1154
     * Set the item at a given offset.
1155
     *
1156
     * @param  mixed  $key
1157
     * @param  mixed  $value
1158
     * @return void
1159
     */
1160
    public function offsetSet($key, $value)
1161
    {
1162
    	// TODO : think of the use of it into a ProxyCollection
1163
    	// context
1164
        $this->initializeProxy();
1165
1166
        return parent::offsetSet($key, $value);
1167
    }
1168
1169
    /**
1170
     * Unset the item at a given offset.
1171
     *
1172
     * @param  string  $key
1173
     * @return void
1174
     */
1175
    public function offsetUnset($key)
1176
    {
1177
        // TODO : think of the use of it into a ProxyCollection
1178
    	// context
1179
        $this->initializeProxy();
1180
1181
        return parent::offsetUnset($key);
1182
    }
1183
1184
    /**
1185
     * Dynamically handle calls to the class.
1186
     *
1187
     * @param  string  $method
1188
     * @param  array   $parameters
1189
     * @return mixed
1190
     *
1191
     * @throws \BadMethodCallException
1192
     */
1193
    public function __call($method, $parameters)
1194
    {
1195
        $this->initializeProxy();
1196
1197
        return parent::__call($method, $parameters);
1198
    }
1199
1200
}
1201