|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of skillshare/formatphp |
| 5 | + * |
| 6 | + * skillshare/formatphp is open source software: you can distribute |
| 7 | + * it and/or modify it under the terms of the MIT License |
| 8 | + * (the "License"). You may not use this file except in |
| 9 | + * compliance with the License. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | + * implied. See the License for the specific language governing |
| 15 | + * permissions and limitations under the License. |
| 16 | + * |
| 17 | + * @copyright Copyright (c) Skillshare, Inc. <https://www.skillshare.com> |
| 18 | + * @license https://opensource.org/licenses/MIT MIT License |
| 19 | + */ |
| 20 | + |
| 21 | +declare(strict_types=1); |
| 22 | + |
| 23 | +namespace FormatPHP\Resources; |
| 24 | + |
| 25 | +use DateTimeImmutable; |
| 26 | +use FormatPHP\Intl\DateTimeFormat; |
| 27 | +use FormatPHP\Intl\DateTimeFormatOptions; |
| 28 | +use FormatPHP\Intl\Locale; |
| 29 | +use FormatPHP\Intl\NumberFormat; |
| 30 | +use FormatPHP\Intl\NumberFormatOptions; |
| 31 | +use Locale as PhpLocale; |
| 32 | +use Psr\Http\Message\ResponseInterface as Response; |
| 33 | +use Psr\Http\Message\ServerRequestInterface as Request; |
| 34 | +use Slim\Factory\AppFactory; |
| 35 | +use Slim\Views\Twig; |
| 36 | +use Slim\Views\TwigMiddleware; |
| 37 | +use Symfony\Component\VarExporter\VarExporter; |
| 38 | +use Throwable; |
| 39 | +use Twig\TwigFunction; |
| 40 | + |
| 41 | +use function array_walk; |
| 42 | +use function in_array; |
| 43 | +use function ksort; |
| 44 | +use function str_replace; |
| 45 | +use function trim; |
| 46 | + |
| 47 | +require_once __DIR__ . '/../../vendor/autoload.php'; |
| 48 | + |
| 49 | +const INTEGER_KEYS = [ |
| 50 | + 'fractionalSecondDigits', |
| 51 | + 'minimumIntegerDigits', |
| 52 | + 'minimumFractionDigits', |
| 53 | + 'maximumFractionDigits', |
| 54 | + 'minimumSignificantDigits', |
| 55 | + 'maximumSignificantDigits', |
| 56 | +]; |
| 57 | + |
| 58 | +const NUMERIC_KEYS = [ |
| 59 | + 'scale', |
| 60 | + 'number', |
| 61 | +]; |
| 62 | + |
| 63 | +(static function (): void { |
| 64 | + $systemLocale = str_replace(['_POSIX', '-POSIX', '_posix', '-posix'], '', PhpLocale::getDefault()); |
| 65 | + $systemLocale = str_replace('_', '-', $systemLocale); |
| 66 | + |
| 67 | + $twig = Twig::create(__DIR__ . '/../templates'); |
| 68 | + $twig->getEnvironment()->addFunction(new TwigFunction('export', fn ($value) => VarExporter::export($value))); |
| 69 | + |
| 70 | + $app = AppFactory::create(); |
| 71 | + $app->add(TwigMiddleware::create($app, $twig)); |
| 72 | + |
| 73 | + $app->get('/', function (Request $request, Response $response): Response { |
| 74 | + $view = Twig::fromRequest($request); |
| 75 | + |
| 76 | + return $view->render($response, 'home.twig'); |
| 77 | + }); |
| 78 | + |
| 79 | + $app->get('/skeleton/numbers', function (Request $request, Response $response) use ($systemLocale): Response { |
| 80 | + $params = $request->getQueryParams(); |
| 81 | + array_walk($params, function (&$value, $key) { |
| 82 | + if (trim($value) === '') { |
| 83 | + $value = null; |
| 84 | + } elseif (in_array($key, NUMERIC_KEYS)) { |
| 85 | + $value += 0; |
| 86 | + } elseif (in_array($key, INTEGER_KEYS)) { |
| 87 | + $value = (int) $value; |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + $localeParam = $params['locale'] ?? $systemLocale; |
| 92 | + $numberProvided = $params['number'] ?? 1234.5678; |
| 93 | + unset($params['locale'], $params['number']); |
| 94 | + |
| 95 | + $exception = null; |
| 96 | + $numberFormatter = null; |
| 97 | + $options = null; |
| 98 | + $skeleton = null; |
| 99 | + $formattedNumber = null; |
| 100 | + |
| 101 | + try { |
| 102 | + $options = new NumberFormatOptions($params); |
| 103 | + $numberFormatter = new NumberFormat(new Locale($localeParam), $options); |
| 104 | + $skeleton = $numberFormatter->getSkeleton(); |
| 105 | + $formattedNumber = $numberFormatter->format($numberProvided); |
| 106 | + } catch (Throwable $exception) { |
| 107 | + // Do nothing. |
| 108 | + } |
| 109 | + |
| 110 | + $view = Twig::fromRequest($request); |
| 111 | + $options = (array) $options; |
| 112 | + ksort($options); |
| 113 | + |
| 114 | + if ($options['style'] === 'currency') { |
| 115 | + unset($options['style'], $options['currency']); |
| 116 | + } |
| 117 | + |
| 118 | + return $view->render($response, 'numbers.twig', [ |
| 119 | + 'localeEvaluated' => $numberFormatter ? $numberFormatter->getEvaluatedLocale() : null, |
| 120 | + 'localeProvided' => $localeParam, |
| 121 | + 'formattedNumber' => $formattedNumber, |
| 122 | + 'numberProvided' => $numberProvided, |
| 123 | + 'skeleton' => $skeleton, |
| 124 | + 'options' => $options, |
| 125 | + 'params' => $params, |
| 126 | + 'exception' => $exception, |
| 127 | + ]); |
| 128 | + }); |
| 129 | + |
| 130 | + $app->get('/skeleton/dates', function (Request $request, Response $response) use ($systemLocale): Response { |
| 131 | + $params = $request->getQueryParams(); |
| 132 | + array_walk($params, function (&$value, $key) { |
| 133 | + if (trim($value) === '') { |
| 134 | + $value = null; |
| 135 | + } elseif ($key === 'hour12') { |
| 136 | + $value = (bool) $value; |
| 137 | + } elseif (in_array($key, INTEGER_KEYS)) { |
| 138 | + $value = (int) $value; |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + $localeParam = $params['locale'] ?? $systemLocale; |
| 143 | + $dateProvided = $params['date'] ?? 'now'; |
| 144 | + unset($params['locale'], $params['date']); |
| 145 | + |
| 146 | + $exception = null; |
| 147 | + $dateFormatter = null; |
| 148 | + $options = null; |
| 149 | + $skeleton = null; |
| 150 | + $formattedDate = null; |
| 151 | + |
| 152 | + try { |
| 153 | + $options = new DateTimeFormatOptions($params); |
| 154 | + $dateFormatter = new DateTimeFormat(new Locale($localeParam), $options); |
| 155 | + $skeleton = $dateFormatter->getSkeleton(); |
| 156 | + $date = new DateTimeImmutable($dateProvided); |
| 157 | + $formattedDate = $dateFormatter->format($date); |
| 158 | + } catch (Throwable $exception) { |
| 159 | + // Do nothing. |
| 160 | + } |
| 161 | + |
| 162 | + $view = Twig::fromRequest($request); |
| 163 | + $options = (array) $options; |
| 164 | + ksort($options); |
| 165 | + |
| 166 | + return $view->render($response, 'dates.twig', [ |
| 167 | + 'localeEvaluated' => $dateFormatter ? $dateFormatter->getEvaluatedLocale() : null, |
| 168 | + 'localeProvided' => $localeParam, |
| 169 | + 'formattedDate' => $formattedDate, |
| 170 | + 'dateProvided' => $dateProvided, |
| 171 | + 'skeleton' => $skeleton, |
| 172 | + 'options' => $options, |
| 173 | + 'params' => $params, |
| 174 | + 'exception' => $exception, |
| 175 | + ]); |
| 176 | + }); |
| 177 | + |
| 178 | + $app->run(); |
| 179 | +})(); |
0 commit comments