1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace donatj\Printf; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* LexemeCollection is an immutable iterable collection of Lexemes with ArrayAccess |
7
|
|
|
* |
8
|
|
|
* @implements \IteratorAggregate<\donatj\Printf\Lexeme> |
9
|
|
|
* @implements \ArrayAccess<int,\donatj\Printf\Lexeme> |
10
|
|
|
*/ |
11
|
|
|
class LexemeCollection implements \ArrayAccess, \IteratorAggregate { |
12
|
|
|
|
13
|
|
|
/** @var Lexeme[] */ |
14
|
|
|
private $lexItems; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* LexemeCollection constructor. |
18
|
|
|
* |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
5 |
|
public function __construct( Lexeme ...$lexItems ) { |
22
|
5 |
|
$this->lexItems = $lexItems; |
23
|
5 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Retrieve the first invalid Lexeme or null if all are valid. |
27
|
|
|
* |
28
|
|
|
* This is useful for checking if a printf string parsed without error. |
29
|
|
|
*/ |
30
|
5 |
|
public function getInvalid() : ?Lexeme { |
31
|
5 |
|
foreach( $this->lexItems as $lexItem ) { |
32
|
5 |
|
if( $lexItem->getLexItemType() === Lexeme::T_INVALID ) { |
33
|
5 |
|
return $lexItem; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @internal |
42
|
|
|
*/ |
43
|
|
|
public function offsetSet( $offset, $value ) : void { |
44
|
|
|
if( $offset === null ) { |
45
|
|
|
$this->lexItems[] = $value; |
46
|
|
|
} else { |
47
|
|
|
$this->lexItems[$offset] = $value; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @internal |
53
|
|
|
*/ |
54
|
|
|
public function offsetExists( $offset ) : bool { |
55
|
|
|
return isset($this->lexItems[$offset]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @internal |
60
|
|
|
*/ |
61
|
|
|
public function offsetUnset( $offset ) : void { |
62
|
|
|
unset($this->lexItems[$offset]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @internal |
67
|
|
|
*/ |
68
|
|
|
public function offsetGet( $offset ) : ?Lexeme { |
69
|
|
|
return $this->lexItems[$offset] ?? null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return \ArrayIterator<int,\donatj\Printf\Lexeme> |
74
|
|
|
* @internal |
75
|
|
|
*/ |
76
|
5 |
|
public function getIterator() : \ArrayIterator { |
77
|
5 |
|
return new \ArrayIterator($this->lexItems); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the LexemeCollection as an ordered array of Lexemes |
82
|
|
|
* |
83
|
|
|
* @return Lexeme[] |
84
|
|
|
*/ |
85
|
|
|
public function toArray() : array { |
86
|
|
|
return $this->lexItems; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns the list of expected arguments a 1-indexed map of the following: |
91
|
|
|
* |
92
|
|
|
* ``` |
93
|
|
|
* ArgumentLexeme::ARG_TYPE_MISSING |
94
|
|
|
* ArgumentLexeme::ARG_TYPE_INT |
95
|
|
|
* ArgumentLexeme::ARG_TYPE_DOUBLE |
96
|
|
|
* ArgumentLexeme::ARG_TYPE_STRING |
97
|
|
|
* ``` |
98
|
|
|
* |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
5 |
|
public function argTypes() : array { |
102
|
5 |
|
$noNumInc = 1; |
103
|
5 |
|
$args = []; |
104
|
5 |
|
foreach( $this->lexItems as $item ) { |
105
|
5 |
|
if( $item instanceof ArgumentLexeme ) { |
106
|
4 |
|
$type = $item->argType(); |
107
|
|
|
|
108
|
4 |
|
if( $item->getArg() !== null ) { |
109
|
|
|
$args[$item->getArg()] = $type; |
110
|
|
|
} else { |
111
|
4 |
|
$args[$noNumInc] = $type; |
112
|
5 |
|
$noNumInc++; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
return $args; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|