platine-php /
template
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Platine Template |
||
| 5 | * |
||
| 6 | * Platine Template is a template engine that has taken a lot of inspiration from Django. |
||
| 7 | * |
||
| 8 | * This content is released under the MIT License (MIT) |
||
| 9 | * |
||
| 10 | * Copyright (c) 2020 Platine Template |
||
| 11 | * Copyright (c) 2014 Guz Alexander, http://guzalexander.com |
||
| 12 | * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com |
||
| 13 | * Copyright (c) 2006 Mateo Murphy |
||
| 14 | * |
||
| 15 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
| 16 | * of this software and associated documentation files (the "Software"), to deal |
||
| 17 | * in the Software without restriction, including without limitation the rights |
||
| 18 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
| 19 | * copies of the Software, and to permit persons to whom the Software is |
||
| 20 | * furnished to do so, subject to the following conditions: |
||
| 21 | * |
||
| 22 | * The above copyright notice and this permission notice shall be included in all |
||
| 23 | * copies or substantial portions of the Software. |
||
| 24 | * |
||
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
| 26 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 27 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
| 28 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
| 29 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
| 30 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
| 31 | * SOFTWARE. |
||
| 32 | */ |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @file Lexer.php |
||
| 36 | * |
||
| 37 | * The template lexer class |
||
| 38 | * |
||
| 39 | * @package Platine\Template\Parser |
||
| 40 | * @author Platine Developers Team |
||
| 41 | * @copyright Copyright (c) 2020 |
||
| 42 | * @license http://opensource.org/licenses/MIT MIT License |
||
| 43 | * @link https://www.platine-php.com |
||
| 44 | * @version 1.0.0 |
||
| 45 | * @filesource |
||
| 46 | */ |
||
| 47 | |||
| 48 | declare(strict_types=1); |
||
| 49 | |||
| 50 | namespace Platine\Template\Parser; |
||
| 51 | |||
| 52 | use Stringable; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @class Lexer |
||
| 56 | * @package Platine\Template\Parser |
||
| 57 | */ |
||
| 58 | class Lexer implements Stringable |
||
| 59 | { |
||
| 60 | /** |
||
| 61 | * The lexer pattern |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected string $pattern; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The lexer matches result |
||
| 68 | * @var array<int, mixed> |
||
| 69 | */ |
||
| 70 | protected array $matches = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Create new instance |
||
| 74 | * @param string $pattern |
||
| 75 | */ |
||
| 76 | public function __construct(string $pattern) |
||
| 77 | { |
||
| 78 | $this->pattern = (substr($pattern, 0, 1) !== '/') |
||
| 79 | ? '/' . $this->quote($pattern) . '/' |
||
| 80 | : $pattern; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Return the array of matches for the given value |
||
| 85 | * @param string $value |
||
| 86 | * @return array<int|string, array<int|string, mixed>> |
||
| 87 | */ |
||
| 88 | public function scan(string $value): array |
||
| 89 | { |
||
| 90 | $matches = []; |
||
| 91 | preg_match_all($this->pattern, $value, $matches); |
||
| 92 | |||
| 93 | if (count($matches) === 1) { |
||
| 94 | return $matches[0]; |
||
| 95 | } |
||
| 96 | |||
| 97 | array_shift($matches); |
||
| 98 | $result = []; |
||
| 99 | foreach ($matches as $matchKey => $subMatches) { |
||
| 100 | foreach ($subMatches as $subMatchKey => $subMatch) { |
||
| 101 | $result[$subMatchKey][$matchKey] = $subMatch; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return $result; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Whether the given value match regex |
||
| 110 | * @param string $value |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function match(string $value): bool |
||
| 114 | { |
||
| 115 | return (bool) preg_match($this->pattern, $value, $this->matches); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Whether the given value match all regex |
||
| 120 | * @param string $value |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | public function matchAll(string $value): bool |
||
| 124 | { |
||
| 125 | return (bool) preg_match_all($this->pattern, $value, $this->matches); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * |
||
| 130 | * @param string $value |
||
| 131 | * @param int $limit |
||
| 132 | * @return array<int, string> |
||
| 133 | */ |
||
| 134 | public function split(string $value, int $limit = -1): array |
||
| 135 | { |
||
| 136 | $result = preg_split($this->pattern, $value, $limit); |
||
| 137 | return $result !== false ? $result : []; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The string representation of this class |
||
| 142 | * useful in debug situation |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | public function __toString(): string |
||
| 146 | { |
||
| 147 | return $this->pattern; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Return result of matches |
||
| 152 | * @param int $index |
||
| 153 | * @return mixed |
||
| 154 | */ |
||
| 155 | public function getArrayMatch(int $index = -1): mixed |
||
| 156 | { |
||
| 157 | if ($index === -1) { |
||
| 158 | return $this->matches; |
||
| 159 | } |
||
| 160 | |||
| 161 | if (array_key_exists($index, $this->matches)) { |
||
| 162 | return $this->matches[$index]; |
||
| 163 | } |
||
| 164 | |||
| 165 | return []; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Return result of match for string |
||
| 170 | * @param int $index |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getStringMatch(int $index): string |
||
| 174 | { |
||
| 175 | if (array_key_exists($index, $this->matches)) { |
||
| 176 | return $this->matches[$index]; |
||
| 177 | } |
||
| 178 | |||
| 179 | return ''; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Return result of match for mixed type |
||
| 184 | * @param int $index |
||
| 185 | * @return mixed |
||
| 186 | */ |
||
| 187 | public function getMixedMatch(int $index): mixed |
||
| 188 | { |
||
| 189 | if (array_key_exists($index, $this->matches)) { |
||
| 190 | return $this->matches[$index]; |
||
| 191 | } |
||
| 192 | |||
| 193 | return null; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Check wether the match for given index exists |
||
| 198 | * @param int $index |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function isMatchNotNull(int $index): bool |
||
| 202 | { |
||
| 203 | return isset($this->matches[$index]); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Quote the given value in order to use in regex expression |
||
| 208 | * @param string $value |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | protected function quote(string $value): string |
||
| 212 | { |
||
| 213 | return preg_quote($value, '/'); |
||
| 214 | } |
||
| 215 | } |
||
| 216 |