LazyCollection::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Collections\LazyCollection;
12
13
use Cubiche\Core\Collections\CollectionInterface;
14
use Cubiche\Core\Comparable\ComparatorInterface;
15
use Cubiche\Core\Specification\SpecificationInterface;
16
17
/**
18
 * Lazy Collection.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 * @author Karel Osorio Ramírez <[email protected]>
22
 */
23
abstract class LazyCollection implements CollectionInterface
24
{
25
    /**
26
     * @var CollectionInterface
27
     */
28
    protected $collection;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $initialized = false;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function clear()
39
    {
40
        $this->lazyInitialize();
41
42
        $this->collection()->clear();
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function isEmpty()
49
    {
50
        $this->lazyInitialize();
51
52
        return $this->collection()->isEmpty();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function toArray()
59
    {
60
        $this->lazyInitialize();
61
62
        return $this->collection()->toArray();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getIterator()
69
    {
70
        $this->lazyInitialize();
71
72
        return $this->collection()->getIterator();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function count()
79
    {
80
        $this->lazyInitialize();
81
82
        return $this->collection()->count();
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function slice($offset, $length = null)
89
    {
90
        $this->lazyInitialize();
91
92
        return $this->collection()->slice($offset, $length);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function find(SpecificationInterface $criteria)
99
    {
100
        $this->lazyInitialize();
101
102
        return $this->collection()->find($criteria);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function sorted(ComparatorInterface $criteria)
109
    {
110
        $this->lazyInitialize();
111
112
        return $this->collection()->sorted($criteria);
113
    }
114
115
    /**
116
     * Lazy initialize.
117
     */
118
    protected function lazyInitialize()
119
    {
120
        if (!$this->isInitialized()) {
121
            $this->initialize();
122
            $this->initialized = true;
123
        }
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    protected function isInitialized()
130
    {
131
        return $this->initialized;
132
    }
133
134
    /**
135
     * Initialize the collection.
136
     */
137
    abstract protected function initialize();
138
139
    /**
140
     * @return CollectionInterface
141
     */
142
    protected function collection()
143
    {
144
        return $this->collection;
145
    }
146
}
147