@@ -12,42 +12,42 @@ |
||
12 | 12 | */ |
13 | 13 | class Text extends Parser { |
14 | 14 | |
15 | - use CaseSensitiveTrait; |
|
15 | + use CaseSensitiveTrait; |
|
16 | 16 | |
17 | - private $text = null; |
|
18 | - private $length = null; |
|
17 | + private $text = null; |
|
18 | + private $length = null; |
|
19 | 19 | |
20 | - public function __construct($text) |
|
21 | - { |
|
22 | - $this->text = $text; |
|
20 | + public function __construct($text) |
|
21 | + { |
|
22 | + $this->text = $text; |
|
23 | 23 | |
24 | - $this->length = mb_strlen($text); |
|
25 | - if ($this->length <= 0) { |
|
26 | - throw new \InvalidArgumentException('Empty argument'); |
|
27 | - } |
|
28 | - } |
|
24 | + $this->length = mb_strlen($text); |
|
25 | + if ($this->length <= 0) { |
|
26 | + throw new \InvalidArgumentException('Empty argument'); |
|
27 | + } |
|
28 | + } |
|
29 | 29 | |
30 | - protected function parse(&$input, $offset, Context $context) |
|
31 | - { |
|
32 | - $this->pushCaseSensitivityToContext($context); |
|
30 | + protected function parse(&$input, $offset, Context $context) |
|
31 | + { |
|
32 | + $this->pushCaseSensitivityToContext($context); |
|
33 | 33 | |
34 | - $text = $context->handleCase($this->text); |
|
35 | - for ($c = 0; $c < $this->length; $c++) { |
|
36 | - if ($offset + $c >= mb_strlen($input) || $text[$c] != $context->handleCase($input[$offset + $c])) { |
|
37 | - $this->popCaseSensitivityFromContext($context); |
|
34 | + $text = $context->handleCase($this->text); |
|
35 | + for ($c = 0; $c < $this->length; $c++) { |
|
36 | + if ($offset + $c >= mb_strlen($input) || $text[$c] != $context->handleCase($input[$offset + $c])) { |
|
37 | + $this->popCaseSensitivityFromContext($context); |
|
38 | 38 | |
39 | - return $this->failure($input, $offset, $c); |
|
40 | - } |
|
41 | - } |
|
39 | + return $this->failure($input, $offset, $c); |
|
40 | + } |
|
41 | + } |
|
42 | 42 | |
43 | - $this->popCaseSensitivityFromContext($context); |
|
43 | + $this->popCaseSensitivityFromContext($context); |
|
44 | 44 | |
45 | - return $this->success($input, $offset, $this->length); |
|
46 | - } |
|
45 | + return $this->success($input, $offset, $this->length); |
|
46 | + } |
|
47 | 47 | |
48 | - public function __toString() |
|
49 | - { |
|
50 | - return '"' . $this->text . '"'; |
|
51 | - } |
|
48 | + public function __toString() |
|
49 | + { |
|
50 | + return '"' . $this->text . '"'; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | } |
@@ -12,48 +12,48 @@ |
||
12 | 12 | */ |
13 | 13 | class Range extends Parser { |
14 | 14 | |
15 | - use CaseSensitiveTrait; |
|
15 | + use CaseSensitiveTrait; |
|
16 | 16 | |
17 | - private $first = null; |
|
18 | - private $last = null; |
|
19 | - private $in = true; |
|
17 | + private $first = null; |
|
18 | + private $last = null; |
|
19 | + private $in = true; |
|
20 | 20 | |
21 | - public function __construct($first, $last, $in = true) |
|
22 | - { |
|
23 | - if ($first === null && $last === null) { |
|
24 | - throw new \InvalidArgumentException('Empty arguments'); |
|
25 | - } |
|
21 | + public function __construct($first, $last, $in = true) |
|
22 | + { |
|
23 | + if ($first === null && $last === null) { |
|
24 | + throw new \InvalidArgumentException('Empty arguments'); |
|
25 | + } |
|
26 | 26 | |
27 | - $this->first = $first === null ? null : self::parseCharacter($first); |
|
28 | - $this->last = $last === null ? null : self::parseCharacter($last); |
|
29 | - $this->in = (bool) $in; |
|
30 | - } |
|
27 | + $this->first = $first === null ? null : self::parseCharacter($first); |
|
28 | + $this->last = $last === null ? null : self::parseCharacter($last); |
|
29 | + $this->in = (bool) $in; |
|
30 | + } |
|
31 | 31 | |
32 | - protected function parse(&$input, $offset, Context $context) |
|
33 | - { |
|
34 | - if ($offset >= mb_strlen($input)) { |
|
35 | - return $this->failure($input, $offset); |
|
36 | - } |
|
32 | + protected function parse(&$input, $offset, Context $context) |
|
33 | + { |
|
34 | + if ($offset >= mb_strlen($input)) { |
|
35 | + return $this->failure($input, $offset); |
|
36 | + } |
|
37 | 37 | |
38 | - $this->pushCaseSensitivityToContext($context); |
|
38 | + $this->pushCaseSensitivityToContext($context); |
|
39 | 39 | |
40 | - $first = ord($context->handleCase($this->first)); |
|
41 | - $last = ord($context->handleCase($this->last)); |
|
42 | - $ord = ord($context->handleCase($input[$offset])); |
|
43 | - if ($first <= $ord && ($this->last === null || $ord <= $last)) { |
|
44 | - $this->popCaseSensitivityFromContext($context); |
|
40 | + $first = ord($context->handleCase($this->first)); |
|
41 | + $last = ord($context->handleCase($this->last)); |
|
42 | + $ord = ord($context->handleCase($input[$offset])); |
|
43 | + if ($first <= $ord && ($this->last === null || $ord <= $last)) { |
|
44 | + $this->popCaseSensitivityFromContext($context); |
|
45 | 45 | |
46 | - return $this->in ? $this->success($input, $offset, 1) : $this->failure($input, $offset); |
|
47 | - } |
|
46 | + return $this->in ? $this->success($input, $offset, 1) : $this->failure($input, $offset); |
|
47 | + } |
|
48 | 48 | |
49 | - $this->popCaseSensitivityFromContext($context); |
|
49 | + $this->popCaseSensitivityFromContext($context); |
|
50 | 50 | |
51 | - return $this->in ? $this->failure($input, $offset) : $this->success($input, $offset, 1); |
|
52 | - } |
|
51 | + return $this->in ? $this->failure($input, $offset) : $this->success($input, $offset, 1); |
|
52 | + } |
|
53 | 53 | |
54 | - public function __toString() |
|
55 | - { |
|
56 | - return sprintf('x%02x-x%02x', ord($this->first), ord($this->last)); |
|
57 | - } |
|
54 | + public function __toString() |
|
55 | + { |
|
56 | + return sprintf('x%02x-x%02x', ord($this->first), ord($this->last)); |
|
57 | + } |
|
58 | 58 | |
59 | 59 | } |
@@ -12,14 +12,14 @@ |
||
12 | 12 | */ |
13 | 13 | class End extends Parser { |
14 | 14 | |
15 | - protected function parse(&$input, $offset, Context $context) |
|
16 | - { |
|
17 | - return $offset == mb_strlen($input) ? $this->success($input, $offset) : $this->failure($input, $offset); |
|
18 | - } |
|
15 | + protected function parse(&$input, $offset, Context $context) |
|
16 | + { |
|
17 | + return $offset == mb_strlen($input) ? $this->success($input, $offset) : $this->failure($input, $offset); |
|
18 | + } |
|
19 | 19 | |
20 | - public function __toString() |
|
21 | - { |
|
22 | - return 'end'; |
|
23 | - } |
|
20 | + public function __toString() |
|
21 | + { |
|
22 | + return 'end'; |
|
23 | + } |
|
24 | 24 | |
25 | 25 | } |
@@ -11,42 +11,42 @@ |
||
11 | 11 | */ |
12 | 12 | trait CaseSensitiveTrait { |
13 | 13 | |
14 | - /** |
|
15 | - * @var boolean |
|
16 | - */ |
|
17 | - private $caseSensitivity = null; |
|
18 | - |
|
19 | - private function pushCaseSensitivityToContext(Context $context) |
|
20 | - { |
|
21 | - if ($this->caseSensitivity !== null) { |
|
22 | - $context->pushCaseSensitivity($this->caseSensitivity); |
|
23 | - } |
|
24 | - } |
|
25 | - |
|
26 | - private function popCaseSensitivityFromContext(Context $context) |
|
27 | - { |
|
28 | - if ($this->caseSensitivity !== null) { |
|
29 | - $context->popCaseSensitivity(); |
|
30 | - } |
|
31 | - } |
|
32 | - |
|
33 | - public function setCaseSensitivity(string $preference) |
|
34 | - { |
|
35 | - $this->caseSensitivity = $preference; |
|
14 | + /** |
|
15 | + * @var boolean |
|
16 | + */ |
|
17 | + private $caseSensitivity = null; |
|
18 | + |
|
19 | + private function pushCaseSensitivityToContext(Context $context) |
|
20 | + { |
|
21 | + if ($this->caseSensitivity !== null) { |
|
22 | + $context->pushCaseSensitivity($this->caseSensitivity); |
|
23 | + } |
|
24 | + } |
|
25 | + |
|
26 | + private function popCaseSensitivityFromContext(Context $context) |
|
27 | + { |
|
28 | + if ($this->caseSensitivity !== null) { |
|
29 | + $context->popCaseSensitivity(); |
|
30 | + } |
|
31 | + } |
|
32 | + |
|
33 | + public function setCaseSensitivity(string $preference) |
|
34 | + { |
|
35 | + $this->caseSensitivity = $preference; |
|
36 | 36 | |
37 | - return $this; |
|
38 | - } |
|
37 | + return $this; |
|
38 | + } |
|
39 | 39 | |
40 | - public function caseSensitive() { |
|
41 | - $this->caseSensitivity = true; |
|
40 | + public function caseSensitive() { |
|
41 | + $this->caseSensitivity = true; |
|
42 | 42 | |
43 | - return $this; |
|
44 | - } |
|
43 | + return $this; |
|
44 | + } |
|
45 | 45 | |
46 | - public function caseInsensitive() { |
|
47 | - $this->caseSensitivity = false; |
|
46 | + public function caseInsensitive() { |
|
47 | + $this->caseSensitivity = false; |
|
48 | 48 | |
49 | - return $this; |
|
50 | - } |
|
49 | + return $this; |
|
50 | + } |
|
51 | 51 | |
52 | 52 | } |
@@ -12,44 +12,44 @@ |
||
12 | 12 | */ |
13 | 13 | class Regex extends Parser { |
14 | 14 | |
15 | - use CaseSensitiveTrait; |
|
15 | + use CaseSensitiveTrait; |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * @var string|null |
19 | 19 | */ |
20 | - private $pattern = null; |
|
20 | + private $pattern = null; |
|
21 | 21 | |
22 | - public function __construct($pattern) |
|
23 | - { |
|
24 | - if (empty($pattern)) { |
|
25 | - throw new \InvalidArgumentException('Empty pattern'); |
|
26 | - } |
|
22 | + public function __construct($pattern) |
|
23 | + { |
|
24 | + if (empty($pattern)) { |
|
25 | + throw new \InvalidArgumentException('Empty pattern'); |
|
26 | + } |
|
27 | 27 | |
28 | - if (@preg_match($pattern, null) === false) { |
|
29 | - throw new \InvalidArgumentException('Invalid pattern'); |
|
30 | - } |
|
28 | + if (@preg_match($pattern, null) === false) { |
|
29 | + throw new \InvalidArgumentException('Invalid pattern'); |
|
30 | + } |
|
31 | 31 | |
32 | - $this->pattern = $pattern; |
|
33 | - } |
|
34 | - |
|
35 | - protected function parse(&$input, $offset, Context $context) |
|
36 | - { |
|
37 | - $this->pushCaseSensitivityToContext($context); |
|
38 | - $pattern = $this->pattern . ($context->isCaseSensitive() ? '' : 'i'); |
|
39 | - $this->popCaseSensitivityFromContext($context); |
|
40 | - |
|
41 | - if (preg_match($pattern, $input, $match, 0, $offset) !== FALSE) { |
|
42 | - if (count($match) > 0 && mb_strlen($match[0]) > 0 && strpos($input, $match[0], $offset) == $offset) { |
|
43 | - return $this->success($input, $offset, mb_strlen($match[0])); |
|
44 | - } |
|
45 | - } |
|
32 | + $this->pattern = $pattern; |
|
33 | + } |
|
34 | + |
|
35 | + protected function parse(&$input, $offset, Context $context) |
|
36 | + { |
|
37 | + $this->pushCaseSensitivityToContext($context); |
|
38 | + $pattern = $this->pattern . ($context->isCaseSensitive() ? '' : 'i'); |
|
39 | + $this->popCaseSensitivityFromContext($context); |
|
40 | + |
|
41 | + if (preg_match($pattern, $input, $match, 0, $offset) !== FALSE) { |
|
42 | + if (count($match) > 0 && mb_strlen($match[0]) > 0 && strpos($input, $match[0], $offset) == $offset) { |
|
43 | + return $this->success($input, $offset, mb_strlen($match[0])); |
|
44 | + } |
|
45 | + } |
|
46 | 46 | |
47 | - return $this->failure($input, $offset); |
|
48 | - } |
|
47 | + return $this->failure($input, $offset); |
|
48 | + } |
|
49 | 49 | |
50 | - public function __toString() |
|
51 | - { |
|
52 | - return (string) $this->pattern; |
|
53 | - } |
|
50 | + public function __toString() |
|
51 | + { |
|
52 | + return (string) $this->pattern; |
|
53 | + } |
|
54 | 54 | |
55 | 55 | } |
@@ -14,14 +14,14 @@ |
||
14 | 14 | */ |
15 | 15 | class Nothing extends Parser { |
16 | 16 | |
17 | - protected function parse(&$input, $offset, Context $context) |
|
18 | - { |
|
19 | - return $this->makeMatch($offset <= mb_strlen($input), $input, 0); |
|
20 | - } |
|
17 | + protected function parse(&$input, $offset, Context $context) |
|
18 | + { |
|
19 | + return $this->makeMatch($offset <= mb_strlen($input), $input, 0); |
|
20 | + } |
|
21 | 21 | |
22 | - public function __toString() |
|
23 | - { |
|
24 | - return '0.'; |
|
25 | - } |
|
22 | + public function __toString() |
|
23 | + { |
|
24 | + return '0.'; |
|
25 | + } |
|
26 | 26 | |
27 | 27 | } |
@@ -38,7 +38,7 @@ |
||
38 | 38 | } |
39 | 39 | |
40 | 40 | $this->set = count_chars($set, 3); |
41 | - $this->include = (bool)$include; |
|
41 | + $this->include = (bool) $include; |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | protected function parse(&$input, $offset, Context $context) |
@@ -8,38 +8,38 @@ |
||
8 | 8 | */ |
9 | 9 | trait AssignTrait { |
10 | 10 | |
11 | - /** |
|
12 | - * List of result names to assign the matched text to. |
|
13 | - * @var callable[] |
|
14 | - */ |
|
15 | - private $assignCallbacks = []; |
|
11 | + /** |
|
12 | + * List of result names to assign the matched text to. |
|
13 | + * @var callable[] |
|
14 | + */ |
|
15 | + private $assignCallbacks = []; |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * Resolve all callbacks registered to this trait |
19 | 19 | * |
20 | 20 | * @param $text |
21 | 21 | */ |
22 | - private function resolveAssignCallbacks($text) |
|
23 | - { |
|
24 | - foreach ($this->assignCallbacks as $callback) { |
|
25 | - $callback($text); |
|
26 | - } |
|
27 | - } |
|
22 | + private function resolveAssignCallbacks($text) |
|
23 | + { |
|
24 | + foreach ($this->assignCallbacks as $callback) { |
|
25 | + $callback($text); |
|
26 | + } |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * After parsing, assign the matched input to the specified local variable. |
|
31 | - * Only assign if successfully matched entire parent up to root. |
|
32 | - * |
|
33 | - * @param mixed $variable |
|
34 | - * @return $this |
|
35 | - */ |
|
36 | - public function assignTo(&$variable) |
|
37 | - { |
|
38 | - $this->assignCallbacks[] = function($text) use (&$variable) { |
|
39 | - $variable = $text; |
|
40 | - }; |
|
29 | + /** |
|
30 | + * After parsing, assign the matched input to the specified local variable. |
|
31 | + * Only assign if successfully matched entire parent up to root. |
|
32 | + * |
|
33 | + * @param mixed $variable |
|
34 | + * @return $this |
|
35 | + */ |
|
36 | + public function assignTo(&$variable) |
|
37 | + { |
|
38 | + $this->assignCallbacks[] = function($text) use (&$variable) { |
|
39 | + $variable = $text; |
|
40 | + }; |
|
41 | 41 | |
42 | - return $this; |
|
43 | - } |
|
42 | + return $this; |
|
43 | + } |
|
44 | 44 | |
45 | 45 | } |
@@ -8,97 +8,97 @@ |
||
8 | 8 | */ |
9 | 9 | trait ResultTrait { |
10 | 10 | |
11 | - /** |
|
12 | - * List of result names to assign the matched text to. |
|
13 | - * @var callable[] |
|
14 | - */ |
|
15 | - private $resultCallbacks = []; |
|
11 | + /** |
|
12 | + * List of result names to assign the matched text to. |
|
13 | + * @var callable[] |
|
14 | + */ |
|
15 | + private $resultCallbacks = []; |
|
16 | 16 | |
17 | - private function resolveResultCallbacks(&$results, $text) |
|
18 | - { |
|
19 | - foreach ($this->resultCallbacks as $callback) { |
|
20 | - $callback($results, $text); |
|
21 | - } |
|
22 | - } |
|
17 | + private function resolveResultCallbacks(&$results, $text) |
|
18 | + { |
|
19 | + foreach ($this->resultCallbacks as $callback) { |
|
20 | + $callback($results, $text); |
|
21 | + } |
|
22 | + } |
|
23 | 23 | |
24 | - /** |
|
25 | - * After parsing, assign the matched input of this parser to the named result. |
|
24 | + /** |
|
25 | + * After parsing, assign the matched input of this parser to the named result. |
|
26 | 26 | * Only assign if successfully matched entire parent up to root. |
27 | - * |
|
28 | - * @param string|integer $key |
|
27 | + * |
|
28 | + * @param string|integer $key |
|
29 | 29 | * @param null|callable|string $value |
30 | - * @return $this |
|
31 | - */ |
|
32 | - public function setResult($key = null, $value = null) |
|
33 | - { |
|
34 | - $this->resultCallbacks[] = function(&$results, $text) use (&$key, &$value) { |
|
30 | + * @return $this |
|
31 | + */ |
|
32 | + public function setResult($key = null, $value = null) |
|
33 | + { |
|
34 | + $this->resultCallbacks[] = function(&$results, $text) use (&$key, &$value) { |
|
35 | 35 | if (is_callable($value)) { |
36 | 36 | $text = $value($text); |
37 | 37 | } elseif ($value !== null) { |
38 | 38 | $text = $value; |
39 | 39 | } |
40 | 40 | |
41 | - $results[$key] = $text; |
|
42 | - }; |
|
41 | + $results[$key] = $text; |
|
42 | + }; |
|
43 | 43 | |
44 | - return $this; |
|
45 | - } |
|
44 | + return $this; |
|
45 | + } |
|
46 | 46 | |
47 | - /** |
|
48 | - * If result exists, concatenate the matched text as a string, otherwise |
|
49 | - * create it. If result is an array, concat to the last entry. |
|
50 | - * |
|
51 | - * @param null|string $key |
|
47 | + /** |
|
48 | + * If result exists, concatenate the matched text as a string, otherwise |
|
49 | + * create it. If result is an array, concat to the last entry. |
|
50 | + * |
|
51 | + * @param null|string $key |
|
52 | 52 | * @param null|callable|string $value |
53 | 53 | * @return $this |
54 | - */ |
|
55 | - public function concatResult($key = null, $value = null) |
|
56 | - { |
|
57 | - $this->resultCallbacks[] = function(&$results, $text) use (&$key, &$value) { |
|
54 | + */ |
|
55 | + public function concatResult($key = null, $value = null) |
|
56 | + { |
|
57 | + $this->resultCallbacks[] = function(&$results, $text) use (&$key, &$value) { |
|
58 | 58 | if (is_callable($value)) { |
59 | 59 | $text = $value($text); |
60 | 60 | } elseif ($value !== null) { |
61 | 61 | $text = $value; |
62 | 62 | } |
63 | 63 | |
64 | - if (!isset($results[$key])) { |
|
65 | - $results[$key] = (string) $text; |
|
66 | - } elseif (is_array($results[$key])) { |
|
67 | - $results[$key][] = array_pop($results[$key]) . $text; |
|
68 | - } else { |
|
69 | - $results[$key] .= $text; |
|
70 | - } |
|
71 | - }; |
|
64 | + if (!isset($results[$key])) { |
|
65 | + $results[$key] = (string) $text; |
|
66 | + } elseif (is_array($results[$key])) { |
|
67 | + $results[$key][] = array_pop($results[$key]) . $text; |
|
68 | + } else { |
|
69 | + $results[$key] .= $text; |
|
70 | + } |
|
71 | + }; |
|
72 | 72 | |
73 | - return $this; |
|
74 | - } |
|
73 | + return $this; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Turn the result into an array and start a new entry. |
|
78 | - * |
|
76 | + /** |
|
77 | + * Turn the result into an array and start a new entry. |
|
78 | + * |
|
79 | 79 | * @param null|string $key |
80 | 80 | * @param null|callable|string $value |
81 | 81 | * @return $this |
82 | - */ |
|
83 | - public function pushResult($key = null, $value = null) |
|
84 | - { |
|
85 | - $this->resultCallbacks[] = function(&$results, $text) use (&$key, &$value) { |
|
82 | + */ |
|
83 | + public function pushResult($key = null, $value = null) |
|
84 | + { |
|
85 | + $this->resultCallbacks[] = function(&$results, $text) use (&$key, &$value) { |
|
86 | 86 | if (is_callable($value)) { |
87 | 87 | $text = $value($text); |
88 | 88 | } elseif ($value !== null) { |
89 | 89 | $text = $value; |
90 | 90 | } |
91 | 91 | |
92 | - if (!isset($results[$key])) { |
|
93 | - $results[$key] = [$text]; |
|
94 | - } elseif (is_array($results[$key])) { |
|
95 | - $results[$key][] = $text; |
|
96 | - } else { |
|
97 | - $results[$key] = [$results[$key], $text]; |
|
98 | - } |
|
99 | - }; |
|
92 | + if (!isset($results[$key])) { |
|
93 | + $results[$key] = [$text]; |
|
94 | + } elseif (is_array($results[$key])) { |
|
95 | + $results[$key][] = $text; |
|
96 | + } else { |
|
97 | + $results[$key] = [$results[$key], $text]; |
|
98 | + } |
|
99 | + }; |
|
100 | 100 | |
101 | - return $this; |
|
102 | - } |
|
101 | + return $this; |
|
102 | + } |
|
103 | 103 | |
104 | 104 | } |