0 ? 'Infinity' : ($v < 0 ? '-Infinity' : 'NaN'); } if ($v === 0.0) { return '0'; } $s = rtrim(rtrim(sprintf('%.17G', $v), '0'), '.'); for ($p = 1; $p <= 17; $p++) { $c = sprintf('%.' . $p . 'G', $v); if ((float) $c === $v) { $s = $c; break; } } /* %G yields E+16; JavaScript yields e+16. */ $s = str_replace(['E+', 'E-', 'E'], ['e+', 'e-', 'e+'], $s); if (str_contains($s, 'e')) { [$m, $e] = explode('e', $s, 2); $m = rtrim(rtrim($m, '0'), '.'); $sign = $e[0] === '-' ? '-' : '+'; $s = $m . 'e' . $sign . ltrim(ltrim($e, '+-'), '0'); } return $s; } /** A number becomes pixels; anything else is passed through as written. */ public static function px($v): string { return (is_int($v) || is_float($v)) ? self::str($v) . 'px' : self::str($v); } /** * Whether the array's keys are 0, 1, 2 … with no gaps. * * `array_is_list()` would say the same thing, but it arrived in PHP 8.1 and WordPress only * began shipping a fallback for it in 6.5. This plugin supports PHP 7.4 and WordPress 6.2, * where calling it is a fatal error on sight — and a development machine always has it, so * nothing here would ever have shown that. */ public static function isList(array $a): bool { $i = 0; foreach ($a as $k => $_) { if ($k !== $i) { return false; } $i++; } return true; } /** * True when the value came from a JSON object rather than a JSON array. * * `json_decode(…, true)` renders both `{}` and `[]` as an empty PHP array, so an empty * array answers false here and the callers treat it as "no value" — which is what the * editor's compiler does for both shapes as well. */ public static function isMap($v): bool { return is_array($v) && $v !== [] && !self::isList($v); } /** * True when the value would be a plain object in the editor's compiler. * * An empty array counts, because `json_decode(…, true)` gives `[]` for `{}` as well and * an empty table is the overwhelmingly more likely source. A populated list does not. */ public static function isObjectish($v): bool { return is_array($v) && ($v === [] || !self::isList($v)); } /** Read a key out of a decoded JSON object, or null when it is absent. */ public static function get($map, string $key) { return (is_array($map) && array_key_exists($key, $map)) ? $map[$key] : null; } }