Completed
Pull Request — master (#1)
by Thibaud
18:37
created

LazyMetadataIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/*
4
 * This file is part of alchemy/resource-component.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Resource\Metadata;
13
14
class LazyMetadataIterator implements MetadataIterator
15
{
16
    /**
17
     * @var MetadataIterator
18
     */
19
    private $iterator;
20
21
    /**
22
     * @var callable
23
     */
24
    private $factory;
25
26
    /**
27
     * @param callable $factory A callback that returns a MetadataIterator instance
28
     */
29
    public function __construct(callable $factory)
30
    {
31
        $this->factory = $factory;
32
    }
33
34
    private function getIterator()
35
    {
36
        if ($this->iterator === null) {
37
            $factory = $this->factory;
38
            $this->iterator = $factory();
39
        }
40
41
        return $this->iterator;
42
    }
43
44
    /**
45
     * (PHP 5 &gt;= 5.0.0)<br/>
46
     * Move forward to next element
47
     * @link http://php.net/manual/en/iterator.next.php
48
     * @return void Any returned value is ignored.
49
     */
50
    public function next()
51
    {
52
        $this->getIterator()->next();
53
    }
54
55
    /**
56
     * (PHP 5 &gt;= 5.0.0)<br/>
57
     * Return the key of the current element
58
     * @link http://php.net/manual/en/iterator.key.php
59
     * @return mixed scalar on success, or null on failure.
60
     */
61
    public function key()
62
    {
63
        return $this->getIterator()->key();
64
    }
65
66
    /**
67
     * (PHP 5 &gt;= 5.0.0)<br/>
68
     * Checks if current position is valid
69
     * @link http://php.net/manual/en/iterator.valid.php
70
     * @return boolean The return value will be casted to boolean and then evaluated.
71
     * Returns true on success or false on failure.
72
     */
73
    public function valid()
74
    {
75
        return $this->getIterator()->valid();
76
    }
77
78
    /**
79
     * (PHP 5 &gt;= 5.0.0)<br/>
80
     * Rewind the Iterator to the first element
81
     * @link http://php.net/manual/en/iterator.rewind.php
82
     * @return void Any returned value is ignored.
83
     */
84
    public function rewind()
85
    {
86
        $this->getIterator()->rewind();
87
    }
88
89
    /**
90
     * @return Metadata
91
     */
92
    public function current()
93
    {
94
        return $this->getIterator()->current();
95
    }
96
}
97