1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* portable-game-notation (https://github.com/chesszebra/portable-game-notation) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/chesszebra/portable-game-notation for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2017 Chess Zebra (https://chesszebra.com) |
7
|
|
|
* @license https://github.com/chesszebra/portable-game-notation/blob/master/LICENSE.md MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace ChessZebra\PortableGameNotation\Token; |
11
|
|
|
|
12
|
|
|
use Iterator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A token stream exists out of a set of tokens which belong to the same game. |
16
|
|
|
*/ |
17
|
|
|
final class TokenIterator implements Iterator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The tokens to iterate through. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $tokens; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The position of the iterator. |
28
|
|
|
* |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $position; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Initializes a new instance of this class. |
35
|
|
|
* |
36
|
|
|
* @param array $tokens The tokens to iterate through. |
37
|
|
|
*/ |
38
|
|
|
public function __construct(array $tokens) |
39
|
|
|
{ |
40
|
|
|
$this->tokens = $tokens; |
41
|
|
|
$this->position = 0; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return the current element |
46
|
|
|
* @link http://php.net/manual/en/iterator.current.php |
47
|
|
|
* @return mixed Can return any type. |
48
|
|
|
* @since 5.0.0 |
49
|
|
|
*/ |
50
|
|
|
public function current() |
51
|
|
|
{ |
52
|
|
|
return $this->tokens[$this->position]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Move forward to next element |
57
|
|
|
* @link http://php.net/manual/en/iterator.next.php |
58
|
|
|
* @return void Any returned value is ignored. |
59
|
|
|
* @since 5.0.0 |
60
|
|
|
*/ |
61
|
|
|
public function next() |
62
|
|
|
{ |
63
|
|
|
$this->position++; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return the key of the current element |
68
|
|
|
* @link http://php.net/manual/en/iterator.key.php |
69
|
|
|
* @return mixed scalar on success, or null on failure. |
70
|
|
|
* @since 5.0.0 |
71
|
|
|
*/ |
72
|
|
|
public function key() |
73
|
|
|
{ |
74
|
|
|
return $this->position; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Checks if current position is valid |
79
|
|
|
* @link http://php.net/manual/en/iterator.valid.php |
80
|
|
|
* @return boolean The return value will be casted to boolean and then evaluated. |
81
|
|
|
* Returns true on success or false on failure. |
82
|
|
|
* @since 5.0.0 |
83
|
|
|
*/ |
84
|
|
|
public function valid() |
85
|
|
|
{ |
86
|
|
|
return $this->position < count($this->tokens); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Rewind the Iterator to the first element |
91
|
|
|
* @link http://php.net/manual/en/iterator.rewind.php |
92
|
|
|
* @return void Any returned value is ignored. |
93
|
|
|
* @since 5.0.0 |
94
|
|
|
*/ |
95
|
|
|
public function rewind() |
96
|
|
|
{ |
97
|
|
|
$this->position = 0; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|