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\Repeater.
41 *
42 * Repeat an iterator n-times.
43 *
44 * @copyright  Copyright © 2007-2017 Hoa community
45 * @license    New BSD License
46 */
47class Repeater implements Iterator
48{
49    /**
50     * Current iterator.
51     *
52     * @var \Traversable
53     */
54    protected $_iterator = null;
55
56    /**
57     * Maximum repetition.
58     *
59     * @var int
60     */
61    protected $_n        = 1;
62
63    /**
64     * Current repetition.
65     *
66     * @var int
67     */
68    protected $_i        = 1;
69
70    /**
71     * Body (callable to execute each time).
72     *
73     * @var callable
74     */
75    protected $_body     = null;
76
77
78
79    /**
80     * Constructor.
81     *
82     * @param   \Traversable  $iterator    Iterator.
83     * @param   int           $n           Repeat $n-times.
84     * @param   callable      $body        Body.
85     * @throws  \Hoa\Iterator\Exception
86     */
87    public function __construct(\Traversable $iterator, $n, $body = null)
88    {
89        if (0 >= $n) {
90            throw new Exception(
91                'n must be greater than 0, given %d.',
92                0,
93                $n
94            );
95        }
96
97        if ($iterator instanceof \IteratorAggregate) {
98            $iterator = $iterator->getIterator();
99        }
100
101        $this->_iterator = $iterator;
102        $this->_n        = $n;
103        $this->_body     = $body;
104
105        return;
106    }
107
108    /**
109     * Return the current element.
110     *
111     * @return  mixed
112     */
113    public function current()
114    {
115        return $this->_iterator->current();
116    }
117
118    /**
119     * Return the key of the current element.
120     *
121     * @return  mixed
122     */
123    public function key()
124    {
125        return $this->_iterator->key();
126    }
127
128    /**
129     * Move forward to next element.
130     *
131     * @return  void
132     */
133    public function next()
134    {
135        return $this->_iterator->next();
136    }
137
138    /**
139     * Rewind the iterator to the first element.
140     *
141     * @return  void
142     */
143    public function rewind()
144    {
145        return $this->_iterator->rewind();
146    }
147
148    /**
149     * Check if current position is valid.
150     *
151     * @return  bool
152     */
153    public function valid()
154    {
155        $valid = $this->_iterator->valid();
156
157        if (true === $valid) {
158            return true;
159        }
160
161        if (null !== $this->_body) {
162            $handle = &$this->_body;
163            $handle($this->_i);
164        }
165
166        $this->rewind();
167
168        if ($this->_n <= $this->_i++) {
169            $this->_i = 1;
170
171            return false;
172        }
173
174        return true;
175    }
176}
177