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\Counter. 41 * 42 * A counter. 43 * 44 * @copyright Copyright © 2007-2017 Hoa community 45 * @license New BSD License 46 */ 47class Counter implements Iterator 48{ 49 /** 50 * From (lower bound). 51 * 52 * @var int 53 */ 54 protected $_from = 0; 55 56 /** 57 * Current key. 58 * 59 * @var int 60 */ 61 protected $_key = 0; 62 63 /** 64 * Current index. 65 * 66 * @var int 67 */ 68 protected $_i = 0; 69 70 /** 71 * To (upper bound). 72 * 73 * @var int 74 */ 75 protected $_to = 0; 76 77 /** 78 * Step. 79 * 80 * @var int 81 */ 82 protected $_step = 0; 83 84 85 86 /** 87 * Constructor. 88 * Equivalent to: 89 * for($i = $from; $i < $to; $i += $step) 90 * 91 * @param int $from Start value. 92 * @param int $to Maximum value. 93 * @param int $step Step. 94 * @throws \Hoa\Iterator\Exception 95 */ 96 public function __construct($from, $to, $step) 97 { 98 if ($step <= 0) { 99 throw new Exception( 100 'The step must be non-negative; given %d.', 101 0, 102 $step 103 ); 104 } 105 106 $this->_from = $from; 107 $this->_to = $to; 108 $this->_step = $step; 109 110 return; 111 } 112 113 /** 114 * Return the current element. 115 * 116 * @return int 117 */ 118 public function current() 119 { 120 return $this->_i; 121 } 122 123 /** 124 * Return the key of the current element. 125 * 126 * @return int 127 */ 128 public function key() 129 { 130 return $this->_key; 131 } 132 133 /** 134 * Move forward to next element. 135 * 136 * @return void 137 */ 138 public function next() 139 { 140 ++$this->_key; 141 $this->_i += $this->_step; 142 143 return; 144 } 145 146 /** 147 * Rewind the iterator to the first element. 148 * 149 * @return void 150 */ 151 public function rewind() 152 { 153 $this->_key = 0; 154 $this->_i = $this->_from; 155 156 return; 157 } 158 159 /** 160 * Check if current position is valid. 161 * 162 * @return bool 163 */ 164 public function valid() 165 { 166 return $this->_i < $this->_to; 167 } 168} 169