1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace donatj\Printf; |
4
|
|
|
|
5
|
|
|
class ArgumentLexeme extends Lexeme { |
6
|
|
|
|
7
|
|
|
/** @var string the argument is treated as an integer and presented as a binary number. */ |
8
|
|
|
public const T_INT_AS_BINARY = 'b'; |
9
|
|
|
/** @var string the argument is treated as an integer and presented as the character with that ASCII value. */ |
10
|
|
|
public const T_INT_AS_CHARACTER = 'c'; |
11
|
|
|
/** @var string the argument is treated as an integer and presented as a (signed) decimal number. */ |
12
|
|
|
public const T_INT = 'd'; |
13
|
|
|
/** |
14
|
|
|
* @var string the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the |
15
|
|
|
* number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of |
16
|
|
|
* significant digits (one less). |
17
|
|
|
*/ |
18
|
|
|
public const T_DOUBLE_AS_SCI = 'e'; |
19
|
|
|
/** @var string like %e but uses uppercase letter (e.g. 1.2E+2). */ |
20
|
|
|
public const T_DOUBLE_AS_SCI_CAP = 'E'; |
21
|
|
|
/** @var string the argument is treated as a float and presented as a floating-point number (locale aware). */ |
22
|
|
|
public const T_FLOAT_LOCALE = 'f'; |
23
|
|
|
/** |
24
|
|
|
* @var string the argument is treated as a float and presented as a floating-point number (non-locale aware). |
25
|
|
|
* Available since PHP 5.0.3. |
26
|
|
|
*/ |
27
|
|
|
public const T_FLOAT_NO_LOCALE = 'F'; |
28
|
|
|
/** @var string shorter of %e and %f. */ |
29
|
|
|
public const T_FLOAT_AUTO_SCI = 'g'; |
30
|
|
|
/** @var string shorter of %E and %F. */ |
31
|
|
|
public const T_FLOAT_AUTO_SCI_CAP = 'G'; |
32
|
|
|
/** @var string the argument is treated as an integer and presented as an octal number. */ |
33
|
|
|
public const T_INT_AS_OCTAL = 'o'; |
34
|
|
|
/** @var string the argument is treated as and presented as a string. */ |
35
|
|
|
public const T_STRING = 's'; |
36
|
|
|
/** @var string the argument is treated as an integer and presented as an unsigned decimal number. */ |
37
|
|
|
public const T_INT_UNSIGNED = 'u'; |
38
|
|
|
/** @var string the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). */ |
39
|
|
|
public const T_INT_HEX = 'x'; |
40
|
|
|
/** @var string the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). */ |
41
|
|
|
public const T_INT_HEX_CAP = 'X'; |
42
|
|
|
|
43
|
|
|
public const VALID_T_TYPES = [ |
44
|
|
|
self::T_INT_AS_BINARY, |
45
|
|
|
self::T_INT_AS_CHARACTER, |
46
|
|
|
self::T_INT, |
47
|
|
|
self::T_DOUBLE_AS_SCI, |
48
|
|
|
self::T_DOUBLE_AS_SCI_CAP, |
49
|
|
|
self::T_FLOAT_LOCALE, |
50
|
|
|
self::T_FLOAT_NO_LOCALE, |
51
|
|
|
self::T_FLOAT_AUTO_SCI, |
52
|
|
|
self::T_FLOAT_AUTO_SCI_CAP, |
53
|
|
|
self::T_INT_AS_OCTAL, |
54
|
|
|
self::T_STRING, |
55
|
|
|
self::T_INT_UNSIGNED, |
56
|
|
|
self::T_INT_HEX, |
57
|
|
|
self::T_INT_HEX_CAP, |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
public const ARG_TYPE_MISSING = ''; |
61
|
|
|
public const ARG_TYPE_INT = 'int'; |
62
|
|
|
public const ARG_TYPE_DOUBLE = 'float'; |
63
|
|
|
public const ARG_TYPE_STRING = 'string'; |
64
|
|
|
|
65
|
|
|
/** @var string[] string s */ |
66
|
|
|
public const STRING_TYPES = [ self::T_STRING ]; |
67
|
|
|
|
68
|
|
|
/** @var string[] integer d, u, c, o, x, X, b */ |
69
|
|
|
public const INTEGER_TYPES = [ |
70
|
|
|
self::T_INT, self::T_INT_UNSIGNED, |
71
|
|
|
self::T_INT_AS_CHARACTER, self::T_INT_AS_OCTAL, |
72
|
|
|
self::T_INT_HEX, self::T_INT_HEX_CAP, |
73
|
|
|
self::T_INT_AS_BINARY, |
74
|
|
|
]; |
75
|
|
|
|
76
|
|
|
/** @var string[] double g, G, e, E, f, F */ |
77
|
|
|
public const DOUBLE_TYPES = [ |
78
|
|
|
self::T_FLOAT_AUTO_SCI, self::T_FLOAT_AUTO_SCI_CAP, |
79
|
|
|
self::T_DOUBLE_AS_SCI, self::T_DOUBLE_AS_SCI_CAP, |
80
|
|
|
self::T_FLOAT_LOCALE, self::T_FLOAT_NO_LOCALE, |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
private ?int $arg; |
84
|
|
|
private bool $showPositive; |
85
|
|
|
private ?string $padChar; |
86
|
|
|
private ?int $padWidth; |
87
|
|
|
private bool $leftJustified; |
88
|
|
|
private ?int $precision; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* ArgumentLexeme constructor. |
92
|
|
|
*/ |
93
|
14 |
|
public function __construct( |
94
|
|
|
string $lexItemType, string $val, int $pos, |
95
|
|
|
?int $arg, bool $showPositive, ?string $padChar, ?int $padWidth, bool $leftJustified, ?int $precision |
96
|
|
|
) { |
97
|
14 |
|
parent::__construct($lexItemType, $val, $pos); |
98
|
|
|
|
99
|
14 |
|
$this->arg = $arg; |
100
|
14 |
|
$this->showPositive = $showPositive; |
101
|
14 |
|
$this->padChar = $padChar; |
102
|
14 |
|
$this->padWidth = $padWidth; |
103
|
14 |
|
$this->leftJustified = $leftJustified; |
104
|
14 |
|
$this->precision = $precision; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* The position specifier, such as `%3$s` would return 3 and `%s` would return null |
109
|
|
|
* @return int|null null on unspecified |
110
|
|
|
*/ |
111
|
14 |
|
public function getArg() : ?int { |
112
|
14 |
|
return $this->arg; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Is the "Prefix positive numbers with a plus sign +" flag enabled |
117
|
|
|
*/ |
118
|
10 |
|
public function getShowPositive() : bool { |
119
|
10 |
|
return $this->showPositive; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Specified pad character flag |
124
|
|
|
* |
125
|
|
|
* @return string|null null on unspecified |
126
|
|
|
*/ |
127
|
10 |
|
public function getPadChar() : ?string { |
128
|
10 |
|
return $this->padChar; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Specified pad width |
133
|
|
|
* |
134
|
|
|
* @return int|null null on unspecified |
135
|
|
|
*/ |
136
|
10 |
|
public function getPadWidth() : ?int { |
137
|
10 |
|
return $this->padWidth; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Is left-justification flag enabled? |
142
|
|
|
*/ |
143
|
10 |
|
public function getLeftJustified() : bool { |
144
|
10 |
|
return $this->leftJustified; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* The Lexeme's indicated precision. |
149
|
|
|
* |
150
|
|
|
* @return int|null null on unspecified |
151
|
|
|
*/ |
152
|
10 |
|
public function getPrecision() : ?int { |
153
|
10 |
|
return $this->precision; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns based on the type of argument one of the following |
158
|
|
|
* |
159
|
|
|
* ArgumentLexeme::ARG_TYPE_MISSING |
160
|
|
|
* ArgumentLexeme::ARG_TYPE_INT |
161
|
|
|
* ArgumentLexeme::ARG_TYPE_DOUBLE |
162
|
|
|
* ArgumentLexeme::ARG_TYPE_STRING |
163
|
|
|
*/ |
164
|
4 |
|
public function argType() : string { |
165
|
4 |
|
if( in_array($this->lexItemType, self::INTEGER_TYPES, true) ) { |
166
|
2 |
|
return self::ARG_TYPE_INT; |
167
|
|
|
} |
168
|
|
|
|
169
|
4 |
|
if( in_array($this->lexItemType, self::DOUBLE_TYPES, true) ) { |
170
|
1 |
|
return self::ARG_TYPE_DOUBLE; |
171
|
|
|
} |
172
|
|
|
|
173
|
3 |
|
if( in_array($this->lexItemType, self::STRING_TYPES, true) ) { |
174
|
2 |
|
return self::ARG_TYPE_STRING; |
175
|
|
|
} |
176
|
|
|
|
177
|
2 |
|
return self::ARG_TYPE_MISSING; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|