1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Iterator;
38
39/**
40 * Class \Hoa\Iterator\Demultiplexer.
41 *
42 * Demux result from another iterator.
43 * This iterator is somehow the opposite of the Hoa\Iterator\Multiple iterator.
44 *
45 * @copyright  Copyright © 2007-2017 Hoa community
46 * @license    New BSD License
47 */
48class Demultiplexer implements Iterator
49{
50    /**
51     * Current iterator.
52     *
53     * @var \Traversable
54     */
55    protected $_iterator = null;
56
57    /**
58     * Current computed value.
59     *
60     * @var mixed
61     */
62    protected $_current  = null;
63
64    /**
65     * Demuxer (callable to execute each time).
66     *
67     * @var callable
68     */
69    protected $_demuxer  = null;
70
71
72
73    /**
74     * Constructor.
75     *
76     * @param   \Traversable  $iterator    Iterator.
77     * @param   callable      $demuxer     Demuxer.
78     * @throws  \Hoa\Iterator\Exception
79     */
80    public function __construct(\Traversable $iterator, $demuxer)
81    {
82        if ($iterator instanceof \IteratorAggregate) {
83            $iterator = $iterator->getIterator();
84        }
85
86        $this->_iterator = $iterator;
87        $this->_demuxer  = $demuxer;
88
89        return;
90    }
91
92    /**
93     * Return the current element.
94     *
95     * @return  mixed
96     */
97    public function current()
98    {
99        if (null !== $this->_current) {
100            return $this->_current;
101        }
102
103        $demuxer = $this->_demuxer;
104
105        return $this->_current = $demuxer($this->_iterator->current());
106    }
107
108    /**
109     * Return the key of the current element.
110     *
111     * @return  mixed
112     */
113    public function key()
114    {
115        return $this->_iterator->key();
116    }
117
118    /**
119     * Move forward to next element.
120     *
121     * @return  void
122     */
123    public function next()
124    {
125        $this->_current = null;
126
127        return $this->_iterator->next();
128    }
129
130    /**
131     * Rewind the iterator to the first element.
132     *
133     * @return  void
134     */
135    public function rewind()
136    {
137        return $this->_iterator->rewind();
138    }
139
140    /**
141     * Check if current position is valid.
142     *
143     * @return  bool
144     */
145    public function valid()
146    {
147        return $this->_iterator->valid();
148    }
149}
150