Overview
Comment: | Implement math and json_encode modifiers |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | dev |
Files: | files | file ages | folders |
SHA3-256: |
73e45ba01123a81d59ea20bc09a84f3a |
User & Date: | bohwaz on 2022-11-20 02:04:53 |
Other Links: | branch diff | manifest | tags |
Context
2022-11-20
| ||
02:05 | Move type detection to UserTemplate, add nicer error messages check-in: c67050ca7e user: bohwaz tags: dev | |
02:04 | Implement math and json_encode modifiers check-in: 73e45ba011 user: bohwaz tags: dev | |
02:04 | Add keep parameter to include to be able to send variables to parent skeleton check-in: 45e169e77f user: bohwaz tags: dev | |
Changes
Modified src/include/lib/Garradin/UserTemplate/Modifiers.php from [02103e14a8] to [aaa136b6f0].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php namespace Garradin\UserTemplate; use Garradin\Utils; use Garradin\UserException; use Garradin\Entities\Users\Email; use KD2\SMTP; use KD2\Brindille_Exception; class Modifiers { const PHP_MODIFIERS_LIST = [ 'strtolower', 'strtoupper', | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php namespace Garradin\UserTemplate; use Garradin\Utils; use Garradin\UserException; use Garradin\Entities\Users\Email; use KD2\SMTP; use KD2\Brindille; use KD2\Brindille_Exception; class Modifiers { const PHP_MODIFIERS_LIST = [ 'strtolower', 'strtoupper', |
︙ | ︙ | |||
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | const MODIFIERS_LIST = [ 'truncate', 'excerpt', 'protect_contact', 'atom_date', 'xml_escape', 'replace', 'regexp_replace', 'regexp_match', 'match', 'remove_leading_number', 'get_leading_number', 'spell_out_number', 'parse_date', 'math', 'money_int' => [Utils::class, 'moneyToInteger'], 'check_email', ]; const LEADING_NUMBER_REGEXP = '/^([\d.]+)\s*[.\)]\s*/'; static public function __callStatic(string $name, array $arguments) { if (!in_array($name, self::PHP_MODIFIERS_LIST)) { | > > > | 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | const MODIFIERS_LIST = [ 'truncate', 'excerpt', 'protect_contact', 'atom_date', 'xml_escape', 'json_encode', 'replace', 'regexp_replace', 'regexp_match', 'match', 'remove_leading_number', 'get_leading_number', 'spell_out_number', 'parse_date', 'math', 'money_int' => [Utils::class, 'moneyToInteger'], 'array_transpose' => [Utils::class, 'array_transpose'], 'check_email', 'validate_date_string', ]; const LEADING_NUMBER_REGEXP = '/^([\d.]+)\s*[.\)]\s*/'; static public function __callStatic(string $name, array $arguments) { if (!in_array($name, self::PHP_MODIFIERS_LIST)) { |
︙ | ︙ | |||
90 91 92 93 94 95 96 | static public function replace($str, $find, $replace): string { return str_replace($find, $replace, $str); } static public function regexp_replace($str, $pattern, $replace) { | | | | 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | static public function replace($str, $find, $replace): string { return str_replace($find, $replace, $str); } static public function regexp_replace($str, $pattern, $replace) { return preg_replace((string) $pattern, (string) $replace, (string) $str); } static public function regexp_match($str, $pattern) { return (int) preg_match((string) $pattern, (string) $str); } static public function match($str, $pattern) { return (int) (stripos($str, $pattern) !== false); } |
︙ | ︙ | |||
191 192 193 194 195 196 197 198 199 200 201 202 203 204 | return Utils::date_fr($date, DATE_ATOM); } static public function xml_escape($str) { return htmlspecialchars($str, ENT_XML1 | ENT_QUOTES); } static public function remove_leading_number($str): string { return preg_replace(self::LEADING_NUMBER_REGEXP, '', trim($str)); } static public function get_leading_number($str): ?string | > > > > > | 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | return Utils::date_fr($date, DATE_ATOM); } static public function xml_escape($str) { return htmlspecialchars($str, ENT_XML1 | ENT_QUOTES); } static public function json_encode($str) { return json_encode($str, JSON_PRETTY_PRINT); } static public function remove_leading_number($str): string { return preg_replace(self::LEADING_NUMBER_REGEXP, '', trim($str)); } static public function get_leading_number($str): ?string |
︙ | ︙ | |||
232 233 234 235 236 237 238 | return $value; } else { return false; } } | | < < < | | > > | > > > > > > > > > | > > > > | | > > > > | | | | | > | > > | > | | | < > | | > > | | > | 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | return $value; } else { return false; } } static public function validate_date_string(string $value): bool { return (bool) strtotime($value); } static public function math(string $expression, ... $params) { static $tokens_list = [ 'function' => '(?:round|ceil|floor|cos|sin|tan|asin|acos|atan|sinh|cosh|tanh|abs|max|min|exp|sqrt|log10|log|pi)\(', 'open' => '\(', 'close' => '\)', 'number' => '-?\d+(?:[,\.]\d+)?', 'sign' => '[+\-\*\/]', 'separator' => ',', ]; $expression = vsprintf($expression, $params); $expression = preg_replace('/\s+/', '', $expression); try { $tokens = Brindille::tokenize($expression, $tokens_list); } catch (\InvalidArgumentException $e) { throw new Brindille_Exception('Invalid value for math modifier: ' . $e->getMessage()); } $stack = []; $expression = ''; foreach ($tokens as $i => $token) { if ($token->type == 'function') { $stack[] = ['function' => $token->value, 'value' => &$value]; } elseif ($token->type == 'open') { $stack[] = ['function' => null, 'value' => &$value]; } elseif ($token->type == 'close') { $last = array_pop($stack); if (!$last) { throw new Brindille_Exception('Invalid closing parenthesis in math modifier on position ' . $token->offset); } } elseif ($token->type == 'number') { $token->value = str_replace(',', '.', $token->value); } $expression .= $token->value; } if (count($stack)) { throw new Brindille_Exception('Unmatched open parenthesis in math modifier on position ' . $token->offset); } return @eval('return ' . $expression . ';') ?: 0; } } |