Debian Codegraph @

vendor/mck89/peast/lib/Peast/Syntax/Node/NumericLiteral.php

raw
1 <?php
2 /**
3 * This file is part of the Peast package
4 *
5 * (c) Marco Marchiò <[email protected]>
6 *
7 * For the full copyright and license information refer to the LICENSE file
8 * distributed with this source code
9 */
10 namespace Peast\Syntax\Node;
11
12 /**
13 * A node that represents a numeric literal.
14 *
15 * @author Marco Marchiò <[email protected]>
16 */
17 class NumericLiteral extends Literal
18 {
19 /**
20 * Map of node properties
21 *
22 * @var array
23 */
24 protected $propertiesMap = array(
25 "format" => false
26 );
27
28 //Format constants
29 /**
30 * Decimal number format
31 */
32 const DECIMAL = "decimal";
33
34 /**
35 * Hexadecimal number format
36 */
37 const HEXADECIMAL = "hexadecimal";
38
39 /**
40 * Octal number format
41 */
42 const OCTAL = "octal";
43
44 /**
45 * Binary number format
46 */
47 const BINARY = "binary";
48
49 /**
50 * Node's numeric format
51 *
52 * @var string
53 */
54 protected $format = self::DECIMAL;
55
56 /**
57 * Numeric forms conversion rules
58 *
59 * @var array
60 */
61 protected $forms = array(
62 "b" => array(
63 "check" => "/^0b[01]+[01_]*$/i",
64 "conv" => "bindec",
65 "format" => self::BINARY
66 ),
67 "o" => array(
68 "check" => "/^0o[0-7]+[0-7_]*$/i",
69 "conv" => "octdec",
70 "format" => self::OCTAL
71 ),
72 "x" => array(
73 "check" => "/^0x[0-9a-f]+[0-9a-f_]*$/i",
74 "conv" => "hexdec",
75 "format" => self::HEXADECIMAL
76 ),
77 );
78
79 /**
80 * Sets node's value
81 *
82 * @param float $value Value
83 *
84 * @return $this
85 */
86 public function setValue($value)
87 {
88 $value = (float) $value;
89 $intValue = (int) $value;
90 if ($value == $intValue) {
91 $value = $intValue;
92 }
93 $this->value = $value;
94 //Force recalculation of the raw value
95 return $this->setFormat($this->format);
96 }
97
98 /**
99 * Sets node's raw value
100 *
101 * @param mixed $raw Raw value
102 *
103 * @return $this
104 *
105 * @throws \Exception
106 */
107 public function setRaw($raw)
108 {
109 $value = $raw;
110 $format = self::DECIMAL;
111 if (is_string($value) && $value !== "") {
112 //Hexadecimal, binary or octal
113 $startZero = $value[0] === "0";
114 $form = $startZero && isset($value[1]) ? strtolower($value[1]) : null;
115 //Numeric separator cannot appear at the beginning or at the end of the number
116 if (preg_match("/^_|_$/", $value)) {
117 throw new \Exception("Invalid numeric value");
118 } elseif (isset($this->forms[$form !== null ? $form : ''])) {
119 $formDef = $this->forms[$form];
120 if (!preg_match($formDef["check"], $value)) {
121 throw new \Exception("Invalid " . $formDef["format"]);
122 }
123 $value = str_replace("_", "", $value);
124 $value = $formDef["conv"]($value);
125 $format = $formDef["format"];
126 } elseif ($startZero && preg_match("/^0[0-7_]+$/", $value)) {
127 //Legacy octal form
128 $value = str_replace("_", "", $value);
129 $value = octdec($value);
130 $format = self::OCTAL;
131 } elseif (
132 preg_match("/^([\d_]*\.?[\d_]*)(?:e[+\-]?[\d_]+)?$/i", $value, $match) &&
133 $match[1] !== "" &&
134 $match[1] !== "." &&
135 !preg_match("/_e|e[+-]?_|_$/", $value)
136 ) {
137 $value = str_replace("_", "", $value);
138 } else {
139 throw new \Exception("Invalid numeric value");
140 }
141 } elseif (!is_int($value) && !is_float($value)) {
142 throw new \Exception("Invalid numeric value");
143 }
144 $value = (float) $value;
145 //Do not attempt to cast to int if it's not possible
146 $checkInt = true;
147 if (defined("PHP_INT_MAX") && defined("PHP_INT_MIN")) {
148 $checkInt = $value <= PHP_INT_MAX && $value >= PHP_INT_MIN;
149 }
150 if ($checkInt) {
151 $intValue = (int) $value;
152 if ($value == $intValue) {
153 $value = $intValue;
154 }
155 }
156 $this->format = $format;
157 $this->value = $value;
158 $this->raw = $raw;
159 return $this;
160 }
161
162 /**
163 * Returns node's numeric format
164 *
165 * @return string
166 */
167 public function getFormat()
168 {
169 return $this->format;
170 }
171
172 /**
173 * Sets node's numeric format
174 *
175 * @param string $format Format, one of the format constants
176 *
177 * @return $this
178 */
179 public function setFormat($format)
180 {
181 $this->format = $format;
182 switch ($format) {
183 case self::BINARY:
184 $this->raw = "0b" . decbin($this->value);
185 break;
186 case self::OCTAL:
187 $this->raw = "0o" . decoct($this->value);
188 break;
189 case self::HEXADECIMAL:
190 $this->raw = "0x" . dechex($this->value);
191 break;
192 default:
193 $this->raw = (string) $this->value;
194 break;
195 }
196 return $this;
197 }
198 }