Completed
Push — lazy_content_type ( 0f043a...242e7c )
by André
19:48 queued 07:55
created

GeneratorCollection::count()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Values;
10
11
use ArrayAccess;
12
use Countable;
13
use Generator;
14
use Iterator;
15
16
/**
17
 * Class GeneratorCollection, allows for lazy loaded arrays using Generators.
18
 *
19
 * This collection class takes Generator as argument, and allows user to threat it like any kind of iterable. It will take
20
 * care about storing the values returned from generator so user can iterate over it several times, it also allows for
21
 * count usage and offset usage.
22
 */
23
class GeneratorCollection implements Iterator, ArrayAccess, Countable
24
{
25
    private $array = [];
26
    private $generator;
27
    private $generatorUsed = false;
28
29
    /**
30
     * GeneratorCollection constructor.
31
     *
32
     * @param Generator $generator
33
     */
34
    public function __construct(Generator $generator)
35
    {
36
        $this->generator = $generator;
37
    }
38
39 View Code Duplication
    public function rewind()
40
    {
41
        if (isset($this->generator)) {
42
            // If generator has already been used, given generators can't start over we will need to load all
43
            if ($this->generatorUsed) {
44
                $this->loadAll();
45
            } else {
46
                $this->generatorUsed = true;
47
                $this->generator->rewind();
48
            }
49
        }
50
51
        reset($this->array);
52
    }
53
54
    public function current()
55
    {
56
        if (isset($this->generator)) {
57
            // As the generator is iterated, set the values on array here so we can also iterate on it later
58
            return $this->array[$this->generator->key()] = $this->generator->current();
59
        }
60
61
        return current($this->array);
62
    }
63
64
    public function key()
65
    {
66
        if (isset($this->generator)) {
67
            return $this->generator->key();
68
        }
69
70
        return key($this->array);
71
    }
72
73 View Code Duplication
    public function next()
74
    {
75
        if (isset($this->generator)) {
76
            $this->generatorUsed = true;
77
            $this->generator->next();
78
        }
79
80
        next($this->array);
81
    }
82
83
    public function valid()
84
    {
85
        if (isset($this->generator)) {
86
            if ($this->generator->valid()) {
87
                return true;
88
            }
89
90
            // If not valid we are at the end of the array, for future iteration we'll unset this and use array only
91
            unset($this->generator);
92
        }
93
94
        return key($this->array) !== null;
95
    }
96
97
    public function offsetExists($offset)
98
    {
99
        if (isset($this->generator)) {
100
            $this->loadAll();
101
        }
102
103
        return isset($this->array[$offset]);
104
    }
105
106
    public function offsetGet($offset)
107
    {
108
        if (isset($this->generator)) {
109
            $this->loadAll();
110
        }
111
112
        return $this->array[$offset];
113
    }
114
115
    public function offsetSet($offset, $value)
116
    {
117
        // Read only collection, so not handled here (for making it writable it would have to loadAll first if needed)
118
    }
119
120
    public function offsetUnset($offset)
121
    {
122
        // Read only collection, so not handled here (for making it writable it would have to loadAll first if needed)
123
    }
124
125
    public function count()
126
    {
127
        if (isset($this->generator)) {
128
            $this->loadAll();
129
        }
130
        return count($this->array);
131
    }
132
133
    /**
134
     * Loads the whole/remaining items in the Generator into internal array.
135
     */
136
    private function loadAll()
137
    {
138
        // As we might have loaded some elements already we use array union to retain everything
139
        $this->array = iterator_to_array($this->generator) + $this->array;
140
        unset($this->generator);
141
    }
142
}
143