Skip to content

Commit 8864cab

Browse files
authored
docs: add tools to help generate sample code and explore skeletons (#46)
1 parent b95a718 commit 8864cab

File tree

10 files changed

+1406
-3
lines changed

10 files changed

+1406
-3
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2021 Skillshare, Inc. <https://www.skillshare.com>
1+
Copyright (c) 2021-2022 Skillshare, Inc. <https://www.skillshare.com>
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ over the formatting of numbers:
133133
* `always`: Always display the sign.
134134
* `auto`: Use the locale to determine when to display the sign.
135135
* `exceptZero`: Display the sign for positive and negative numbers, but never
136-
display the size for zero.
136+
display the sign for zero.
137137
* `never`: Never display the sign.
138138
* `roundingMode`: Controls rounding rules for the number. The default is
139139
`halfEven`. Possible values include:
@@ -208,6 +208,8 @@ using `minimumFractionDigits` or `maximumFractionDigits`).
208208

209209
When formatting currency, you may use the following properties.
210210

211+
* `currency`: An [ISO 4217 currency code](https://www.iso.org/iso-4217-currency-codes.html)
212+
to use when formatting currency. For example, `USD`, `EUR`, or `CNY`.
211213
* `currencySign`: In accounting, many locales format negative currency values
212214
using parentheses rather than the minus sign. You may enable this behavior by
213215
setting this property to `accounting`. The default value is `standard`.
@@ -220,6 +222,22 @@ When formatting currency, you may use the following properties.
220222
* `code`: Use the ISO currency code when formatting currency (e.g., "USD 100").
221223
* `name`: Use a localized name for the currency (e.g., "100 US dollars").
222224

225+
#### Formatting Units
226+
227+
* `unit`: When formatting units, you must provide a core unit identifier as the `unit`
228+
property. [UTS #35, Part 2, Section 6](https://unicode.org/reports/tr35/tr35-general.html#Unit_Elements)
229+
defines core unit identifiers. You may use any unit defined in the
230+
[CLDR data file](https://github.com/unicode-org/cldr/blob/main/common/validity/unit.xml).
231+
You may use the following units in these concise forms (without the prefixes
232+
defined in CLDR): `acre`, `bit`, `byte`, `celsius`, `centimeter`, `day`, `degree`,
233+
`fahrenheit`, `fluid-ounce`, `foot`, `gallon`, `gigabit`, `gigabyte`, `gram`,
234+
`hectare`, `hour`, `inch`, `kilobit`, `kilobyte`, `kilogram`, `kilometer`,
235+
`liter`, `megabit`, `megabyte`, `meter`, `mile`, `mile-scandinavian`, `milliliter`,
236+
`millimeter`, `millisecond`, `minute`, `month`, `ounce`, `percent`, `petabyte`,
237+
`pound`, `second`, `stone`, `terabit`, `terabyte`, `week`, `yard`, or `year`.
238+
* `unitDisplay`: How to display the unit. Possible values include `short`, `long`,
239+
and `narrow`. The default is `short`.
240+
223241
#### Compact Notation
224242

225243
If `notation` is `compact`, then you may specify the `compactDisplay` property

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
"require-dev": {
3333
"ramsey/devtools": "^1.7",
3434
"ramsey/devtools-lib": "1.3.0",
35-
"spatie/phpunit-snapshot-assertions": "^4.2"
35+
"slim/slim": "^4.9",
36+
"slim/twig-view": "^3.3",
37+
"spatie/phpunit-snapshot-assertions": "^4.2",
38+
"symfony/var-exporter": "^5.4",
39+
"twig/twig": "^3.3"
3640
},
3741
"minimum-stability": "dev",
3842
"prefer-stable": true,

resources/.gitkeep

Whitespace-only changes.

resources/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Resources
2+
3+
This is a sample application, giving you the ability to *play around* with
4+
formatting messages, dates, times, numbers, and currency.
5+
6+
To run the sample application, type the following in your terminal:
7+
8+
```shell
9+
git clone https://github.com/Skillshare/formatphp.git
10+
cd formatphp/
11+
composer update
12+
php -S localhost:9001 ./resources/public/index.php
13+
```
14+
15+
Then, you may access the application in a web browser at <http://localhost:9001>.

resources/public/index.php

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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

Comments
 (0)