Skip to content

[Intl] Add PHP 8.5 IntlListFormatter as installable polyfill #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"symfony/polyfill-intl-grapheme": "self.version",
"symfony/polyfill-intl-icu": "self.version",
"symfony/polyfill-intl-messageformatter": "self.version",
"symfony/polyfill-intl-listformatter": "self.version",
"symfony/polyfill-intl-idn": "self.version",
"symfony/polyfill-intl-normalizer": "self.version",
"symfony/polyfill-mbstring": "self.version",
Expand All @@ -62,6 +63,7 @@
"classmap": [
"src/Intl/Icu/Resources/stubs",
"src/Intl/MessageFormatter/Resources/stubs",
"src/Intl/ListFormatter/Resources/stubs",
"src/Intl/Normalizer/Resources/stubs",
"src/Php85/Resources/stubs",
"src/Php84/Resources/stubs",
Expand Down
206 changes: 206 additions & 0 deletions src/Intl/ListFormatter/IntlListFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Polyfill\Intl\ListFormatter;

/**
* A polyfill implementation of the IntlListFormatter class provided by the intl extension.
*
* @author Ayesh Karunaratne <[email protected]>
*
* @internal
*/
class IntlListFormatter

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be final, non-serializable and not able to clone it.

{
public const TYPE_AND = 0;
public const TYPE_OR = 1;
public const TYPE_UNITS = 2;

public const WIDTH_WIDE = 0;
public const WIDTH_SHORT = 1;
public const WIDTH_NARROW = 2;

/**
* @var string
*/
private $locale;
/**
* @var int
*/
private $type;
/**
* @var int
*/
private $width;

protected static $listPatterns = [
'en' => [

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make it clear that this is en_US since it's using the Oxford comma?

'listPattern-type-standard' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, and {1}',
2 => '{0} and {1}',
],
'listPattern-type-or' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, or {1}',
2 => '{0} or {1}',
],
'listPattern-type-or-narrow' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, or {1}',
2 => '{0} or {1}',
],
'listPattern-type-or-short' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, or {1}',
2 => '{0} or {1}',
],
'listPattern-type-standard-narrow' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, {1}',
2 => '{0}, {1}',
],
'listPattern-type-standard-short' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, & {1}',
2 => '{0} & {1}',
],
'listPattern-type-unit' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, {1}',
2 => '{0}, {1}',
],
'listPattern-type-unit-narrow' => [
'start' => '{0} {1}',
'middle' => '{0} {1}',
'end' => '{0} {1}',
2 => '{0} {1}',
],
'listPattern-type-unit-short' => [
'start' => '{0}, {1}',
'middle' => '{0}, {1}',
'end' => '{0}, {1}',
2 => '{0}, {1}',
],
],
];

public function __construct(
string $locale,
int $type = self::TYPE_AND,
int $width = self::WIDTH_WIDE
) {
$exceptionClass = PHP_VERSION_ID >= 80000 ? \ValueError::class : \InvalidArgumentException::class;
if ($locale !== 'en' && strpos($locale, 'en') !== 0) {
throw new $exceptionClass('Invalid locale, only "en" and "en-*" locales are supported');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some mixed feelings about this. Since it's using the en-US set, for en-gb it would return a different result than the real class.

}

if ($type !== self::TYPE_AND && $type !== self::TYPE_OR && $type !== self::TYPE_UNITS) {
throw new $exceptionClass('Argument #2 ($type) must be one of IntlListFormatter::TYPE_AND, IntlListFormatter::TYPE_OR, or IntlListFormatter::TYPE_UNITS.');
}

if ($width !== self::WIDTH_WIDE && $width !== self::WIDTH_SHORT && $width !== self::WIDTH_NARROW) {
throw new $exceptionClass('Argument #3 ($width) must be one of IntlListFormatter::WIDTH_WIDE, IntlListFormatter::WIDTH_SHORT, or IntlListFormatter::WIDTH_NARROW.');
}


$this->locale = 'en';
$this->type = $type;
$this->width = $width;
}

public function format(array $strings): string
{
$itemCount = count($strings);

if ($itemCount === 0) {
return '';
}

$strings = array_values($strings);

if ($itemCount === 1) {
return (string) $strings[0];
}

$pattern = $this->getListPattern();

switch ($this->type) {
case self::TYPE_AND:
$lookupKeyType = 'standard';
break;
case self::TYPE_OR:
$lookupKeyType = 'or';
break;
case self::TYPE_UNITS:
$lookupKeyType = 'unit';
break;
}

switch ($this->width) {
case self::WIDTH_WIDE:
$lookupKeyWidth = '';
break;
case self::WIDTH_SHORT:
$lookupKeyWidth = '-short';
break;
case self::WIDTH_NARROW:
$lookupKeyWidth = '-narrow';
break;
}

$pattern = $pattern['listPattern-type-' . $lookupKeyType . $lookupKeyWidth];

if ($itemCount === 2) {
return strtr($pattern[2], ['{0}' => (string) $strings[0], '{1}' => (string) $strings[1]]);
}

if ($itemCount === 3) {
$start = strtr($pattern['start'], ['{0}' => (string) $strings[0], '{1}' => (string) $strings[1]]);
return strtr($pattern['end'], ['{0}' => $start, '{1}' => (string) $strings[2]]);
}

$result = strtr($pattern['start'], ['{0}' => (string) $strings[0], '{1}' => (string) $strings[1]]);

for ($i = 2; $i < $itemCount - 1; $i++) {
$result = strtr($pattern["middle"], [
"{0}" => $result,
"{1}" => $strings[$i],
]);
}

return strtr($pattern["end"], [
"{0}" => $result,
"{1}" => $strings[$itemCount - 1],
]);
}

protected function getListPattern(): array {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not private?

return self::$listPatterns[$this->locale];
}

public function getErrorCode()
{
return 0;
}

public function getErrorMessage()
{
return '';
}
}
19 changes: 19 additions & 0 deletions src/Intl/ListFormatter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
14 changes: 14 additions & 0 deletions src/Intl/ListFormatter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Symfony Polyfill / Intl: ListFormatter
=========================================

This component provides a fallback implementation for the
[`ListFormatter`](https://php.net/ListFormatter) class provided
by the [Intl](https://php.net/intl) extension.

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

License
=======

This library is released under the [MIT license](LICENSE).
14 changes: 14 additions & 0 deletions src/Intl/ListFormatter/Resources/stubs/IntlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class IntlException extends Exception
{
}
14 changes: 14 additions & 0 deletions src/Intl/ListFormatter/Resources/stubs/IntlListFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

final class IntlListFormatter extends Symfony\Polyfill\Intl\ListFormatter\IntlListFormatter
{
}
35 changes: 35 additions & 0 deletions src/Intl/ListFormatter/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "symfony/polyfill-intl-listformatter",
"type": "library",
"description": "Symfony polyfill for intl's ListFormatter class and related functions",
"keywords": ["polyfill", "shim", "compatibility", "portable", "intl", "listformatter"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Nicolas Grekas",
"email": "[email protected]"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=7.2"
},
"autoload": {
"psr-4": { "Symfony\\Polyfill\\Intl\\ListFormatter\\": "" },
"classmap": [ "Resources/stubs" ]
},
"suggest": {
"ext-intl": "For best performance"
},
"minimum-stability": "dev",
"extra": {
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
}
}
Loading
Loading