Passed
Push — master ( 4246ff...f46652 )
by Jesse
01:49
created

ArgumentLexeme::getShowPositive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace donatj\Printf;
4
5
class ArgumentLexeme extends Lexeme {
6
7
	public const T_INT_AS_BINARY      = 'b'; // b - the argument is treated as an integer and presented as a binary number.
8
	public const T_INT_AS_CHARACTER   = 'c'; // c - the argument is treated as an integer and presented as the character with that ASCII value.
9
	public const T_INT                = 'd'; // d - the argument is treated as an integer and presented as a (signed) decimal number.
10
	public const T_DOUBLE_AS_SCI      = 'e'; // e - the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less).
11
	public const T_DOUBLE_AS_SCI_CAP  = 'E'; // E - like %e but uses uppercase letter (e.g. 1.2E+2).
12
	public const T_FLOAT_LOCALE       = 'f'; // f - the argument is treated as a float and presented as a floating-point number (locale aware).
13
	public const T_FLOAT_NO_LOCALE    = 'F'; // F - the argument is treated as a float and presented as a floating-point number (non-locale aware). Available since PHP 5.0.3.
14
	public const T_FLOAT_AUTO_SCI     = 'g'; // g - shorter of %e and %f.
15
	public const T_FLOAT_AUTO_SCI_CAP = 'G'; // G - shorter of %E and %F.
16
	public const T_INT_AS_OCTAL       = 'o'; // o - the argument is treated as an integer and presented as an octal number.
17
	public const T_STRING             = 's'; // s - the argument is treated as and presented as a string.
18
	public const T_INT_UNSIGNED       = 'u'; // u - the argument is treated as an integer and presented as an unsigned decimal number.
19
	public const T_INT_HEX            = 'x'; // x - the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters).
20
	public const T_INT_HEX_CAP        = 'X'; // X - the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters).
21
22
	public const CHAR_MAP = [
23
		'b' => self::T_INT_AS_BINARY,
24
		'c' => self::T_INT_AS_CHARACTER,
25
		'd' => self::T_INT,
26
		'e' => self::T_DOUBLE_AS_SCI,
27
		'E' => self::T_DOUBLE_AS_SCI_CAP,
28
		'f' => self::T_FLOAT_LOCALE,
29
		'F' => self::T_FLOAT_NO_LOCALE,
30
		'g' => self::T_FLOAT_AUTO_SCI,
31
		'G' => self::T_FLOAT_AUTO_SCI_CAP,
32
		'o' => self::T_INT_AS_OCTAL,
33
		's' => self::T_STRING,
34
		'u' => self::T_INT_UNSIGNED,
35
		'x' => self::T_INT_HEX,
36
		'X' => self::T_INT_HEX_CAP,
37
	];
38
39
	public const ARG_TYPE_MISSING = '';
40
	public const ARG_TYPE_INT     = 'int';
41
	public const ARG_TYPE_DOUBLE  = 'float';
42
	public const ARG_TYPE_STRING  = 'string';
43
44
	/** @var int[] string    s */
45
	public const STRING_TYPES = [ self::T_STRING ];
46
47
	/** @var int[] integer    d, u, c, o, x, X, b */
48
	public const INTEGER_TYPES = [
49
		self::T_INT, self::T_INT_UNSIGNED,
50
		self::T_INT_AS_CHARACTER, self::T_INT_AS_OCTAL,
51
		self::T_INT_HEX, self::T_INT_HEX_CAP,
52
		self::T_INT_AS_BINARY,
53
	];
54
55
	/** @var int[] double    g, G, e, E, f, F */
56
	public const DOUBLE_TYPES = [
57
		self::T_FLOAT_AUTO_SCI, self::T_FLOAT_AUTO_SCI_CAP,
58
		self::T_DOUBLE_AS_SCI, self::T_DOUBLE_AS_SCI_CAP,
59
		self::T_FLOAT_LOCALE, self::T_FLOAT_NO_LOCALE,
60
	];
61
62
	private $arg;
63
	private $showPositive;
64
	private $padChar;
65
	private $padWidth;
66
	private $leftJustified;
67
	private $precision;
68
69
	public function __construct(
70
		string $lexItemType, string $val, int $pos,
71
		?int $arg, ?bool $showPositive, ?string $padChar, ?int $padWidth, ?bool $leftJustified, ?int $precision
72
	) {
73
		parent::__construct($lexItemType, $val, $pos);
74
		$this->arg           = $arg;
75
		$this->showPositive  = $showPositive;
76
		$this->padChar       = $padChar;
77
		$this->padWidth      = $padWidth;
78
		$this->leftJustified = $leftJustified;
79
		$this->precision     = $precision;
80
	}
81
82
	public function getArg() : ?int {
83
		return $this->arg;
84
	}
85
86
	public function getShowPositive() : ?bool {
87
		return $this->showPositive;
88
	}
89
90
	public function getPadChar() : ?string {
91
		return $this->padChar;
92
	}
93
94
	public function getPadWidth() : ?int {
95
		return $this->padWidth;
96
	}
97
98
	public function getLeftJustified() : ?bool {
99
		return $this->leftJustified;
100
	}
101
102
	public function getPrecision() : ?int {
103
		return $this->precision;
104
	}
105
106
	/**
107
	 * Returns based on the type of argument one of the following
108
	 *
109
	 *   PrintfLexeme::ARG_TYPE_MISSING
110
	 *   PrintfLexeme::ARG_TYPE_INT
111
	 *   PrintfLexeme::ARG_TYPE_DOUBLE
112
	 *   PrintfLexeme::ARG_TYPE_STRING
113
	 */
114
	public function argType() : string {
115
		if( in_array($this->lexItemType, self::INTEGER_TYPES, true) ) {
116
			return self::ARG_TYPE_INT;
117
		}
118
119
		if( in_array($this->lexItemType, self::DOUBLE_TYPES, true) ) {
120
			return self::ARG_TYPE_DOUBLE;
121
		}
122
123
		if( in_array($this->lexItemType, self::STRING_TYPES, true) ) {
124
			return self::ARG_TYPE_STRING;
125
		}
126
127
		return self::ARG_TYPE_MISSING;
128
	}
129
130
}
131