Repository navigation
Modernize for PHP 8.1+ and PHPUnit 11 #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2c7008a
Modernize for PHP 8.1+ and PHPUnit 11
joanfabregat 93e2f17
Fix Copilot review: drop typed constants, fix README
joanfabregat 9558a6e
Bump minimum PHP to 8.2 (PHPUnit 11 requirement)
joanfabregat ea09e7f
Restore public visibility of DEFAULT_ENCODING constant
joanfabregat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| name: CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [master] | ||
| pull_request: | ||
| branches: [master] | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| php-version: ['8.2', '8.3', '8.4'] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Set up PHP ${{ matrix.php-version }} | ||
| uses: shivammathur/setup-php@v2 | ||
| with: | ||
| php-version: ${{ matrix.php-version }} | ||
| extensions: mbstring | ||
|
|
||
| - name: Install dependencies | ||
| run: composer install --no-interaction --prefer-dist | ||
|
|
||
| - name: Run tests | ||
| run: vendor/bin/phpunit --testdox | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| /.idea | ||
| /composer.lock | ||
| /vendor | ||
| /.phpunit.cache | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,54 @@ | ||
| # Strip accents | ||
| This PHP 7 library removes accent from strings. It is based on the sample code [published here](http://www.infowebmaster.fr/tutoriel/php-enlever-accents). | ||
| # Strip Accents | ||
|
|
||
| [](https://packagist.org/packages/codeinc/strip-accents) | ||
| [](https://packagist.org/packages/codeinc/strip-accents) | ||
| [](https://packagist.org/packages/codeinc/strip-accents) | ||
| [](LICENSE) | ||
|
|
||
| A PHP library to remove accents and diacritics from strings. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| composer require codeinc/strip-accents | ||
| ``` | ||
|
|
||
| Requires PHP 8.2 or later with the `mbstring` extension. | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Strip accents | ||
|
|
||
| ## Usage | ||
| ```php | ||
| <?php | ||
| use CodeInc\StripAccents\StripAccents; | ||
|
|
||
| echo StripAccents::strip("C'est une super chaîne de caractères avec beaucoup d'accents"); | ||
| // echoes: C'est une super chaine de caracteres avec beaucoup d'accents | ||
| // "C'est une super chaine de caracteres avec beaucoup d'accents" | ||
|
|
||
| echo StripAccents::strip("ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ"); | ||
| // echoes: AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy | ||
| // "AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiieooooouuuuyy" | ||
| ``` | ||
|
|
||
| ### Strip accents and non-printable characters | ||
|
|
||
| ```php | ||
| use CodeInc\StripAccents\StripAccents; | ||
|
|
||
| echo StripAccents::stripNonPrint("ABC ÀÈÝ 是我这"); | ||
| // echoes: ABC AEY --- | ||
| // "ABC AEY " | ||
|
|
||
| // You can specify any encoding supported by htmlentities() as a second parameter | ||
| echo StripAccents::strip("A strïng with àccénts", "iso-8859-1"); | ||
| echo StripAccents::stripNonPrint("ABC ÀÈÝ 是我这", replaceWith: "-"); | ||
| // "ABC AEY ---------" (each multibyte character is replaced per-byte) | ||
| ``` | ||
|
|
||
| ### Custom encoding | ||
|
|
||
| ## Installation | ||
| This library is available through [Packagist](https://packagist.org/packages/codeinc/strip-accents) and can be installed using [Composer](https://getcomposer.org/): | ||
| Both methods accept an encoding parameter (any encoding supported by `htmlentities()`): | ||
|
|
||
| ```bash | ||
| composer require codeinc/strip-accents | ||
| ```php | ||
| echo StripAccents::strip("A strïng with àccénts", encoding: "iso-8859-1"); | ||
| ``` | ||
|
|
||
|
|
||
| ## License | ||
| This library is published under the MIT license (see the [LICENSE](LICENSE) file). | ||
|
|
||
| This library is published under the MIT license (see the [LICENSE](LICENSE) file). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd" | ||
| bootstrap="vendor/autoload.php" | ||
| colors="true" | ||
| cacheDirectory=".phpunit.cache"> | ||
| <testsuites> | ||
| <testsuite name="default"> | ||
| <directory>tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| <source> | ||
| <include> | ||
| <directory>src</directory> | ||
| </include> | ||
| </source> | ||
| </phpunit> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,77 +1,48 @@ | ||
| <?php | ||
| // | ||
| // +---------------------------------------------------------------------+ | ||
| // | CODE INC. SOURCE CODE | | ||
| // +---------------------------------------------------------------------+ | ||
| // | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. | | ||
| // | Visit https://www.codeinc.fr for more information about licensing. | | ||
| // +---------------------------------------------------------------------+ | ||
| // | NOTICE: All information contained herein is, and remains the | | ||
| // | property of Code Inc. SAS. The intellectual and technical concepts | | ||
| // | contained herein are proprietary to Code Inc. SAS are protected by | | ||
| // | trade secret or copyright law. Dissemination of this information or | | ||
| // | reproduction of this material is strictly forbidden unless prior | | ||
| // | written permission is obtained from Code Inc. SAS. | | ||
| // +---------------------------------------------------------------------+ | ||
| // | ||
| // Author: Joan Fabrégat <joan@codeinc.fr> | ||
| // Date: 15/03/2018 | ||
| // Time: 10:52 | ||
| // Project: StipAccents | ||
| // | ||
| declare(strict_types = 1); | ||
| namespace CodeInc\StripAccents; | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CodeInc\StripAccents; | ||
|
|
||
| /** | ||
| * Class StripAccents | ||
| * | ||
| * @link http://www.infowebmaster.fr/tutoriel/php-enlever-accents | ||
| * @package CodeInc\StripAccents | ||
| * @author Joan Fabrégat <joan@codeinc.fr> | ||
| * Removes accents and diacritics from strings. | ||
| */ | ||
| class StripAccents | ||
| { | ||
| public const DEFAULT_ENCODING = 'utf-8'; | ||
|
|
||
| /** | ||
| * @param string $string | ||
| * @param string $encoding | ||
| * @return string | ||
| * Strips accents and diacritics from a string. | ||
| */ | ||
| public static function strip(string $string, string $encoding = self::DEFAULT_ENCODING): string | ||
| { | ||
| // converting accents in HTML entities | ||
| $string = htmlentities($string, ENT_NOQUOTES, $encoding); | ||
|
|
||
| // replacing the HTML entities to extract the first letter | ||
| // examples: "&ecute;" => "e", "&Ecute;" => "E", "à" => "a" ... | ||
| // replace accented HTML entities with their base letter | ||
| // e.g. "é" => "e", "É" => "E", "à" => "a" | ||
| $string = preg_replace( | ||
| '#&([A-za-z])(?:acute|grave|cedil|circ|orn|ring|slash|th|tilde|uml);#', | ||
| '#&([A-Za-z])(?:acute|grave|cedil|circ|orn|ring|slash|th|tilde|uml);#', | ||
| '\1', | ||
| $string | ||
| ); | ||
|
|
||
| // replacing ligatures | ||
| // Exemple "œ" => "oe", "Æ" => "AE" | ||
| $string = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $string); | ||
| // replace ligatures e.g. "œ" => "oe", "Æ" => "AE" | ||
| $string = preg_replace('#&([A-Za-z]{2})(?:lig);#', '\1', $string); | ||
|
|
||
| // removing the remaining bits | ||
| // remove any remaining HTML entities | ||
| $string = preg_replace('#&[^;]+;#', '', $string); | ||
|
|
||
| return $string; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $string | ||
| * @param string $encoding | ||
| * @param string $replaceWith | ||
| * @return string | ||
| * Strips accents and non-printable characters from a string. | ||
| */ | ||
| public static function stripNonPrint(string $string, | ||
| string $replaceWith = '', | ||
| string $encoding = self::DEFAULT_ENCODING): string | ||
| { | ||
| public static function stripNonPrint( | ||
| string $string, | ||
| string $replaceWith = '', | ||
| string $encoding = self::DEFAULT_ENCODING, | ||
| ): string { | ||
| return preg_replace('/[[:^print:]]/', $replaceWith, self::strip($string, $encoding)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,63 +1,93 @@ | ||
| <?php | ||
| // | ||
| // +---------------------------------------------------------------------+ | ||
| // | CODE INC. SOURCE CODE | | ||
| // +---------------------------------------------------------------------+ | ||
| // | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. | | ||
| // | Visit https://www.codeinc.fr for more information about licensing. | | ||
| // +---------------------------------------------------------------------+ | ||
| // | NOTICE: All information contained herein is, and remains the | | ||
| // | property of Code Inc. SAS. The intellectual and technical concepts | | ||
| // | contained herein are proprietary to Code Inc. SAS are protected by | | ||
| // | trade secret or copyright law. Dissemination of this information or | | ||
| // | reproduction of this material is strictly forbidden unless prior | | ||
| // | written permission is obtained from Code Inc. SAS. | | ||
| // +---------------------------------------------------------------------+ | ||
| // | ||
| // Author: Joan Fabrégat <joan@codeinc.fr> | ||
| // Date: 15/03/2018 | ||
| // Time: 10:57 | ||
| // Project: StripAccents | ||
| // | ||
| declare(strict_types = 1); | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace CodeInc\StripAccents\Tests; | ||
|
|
||
| use CodeInc\StripAccents\StripAccents; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
|
|
||
| /** | ||
| * Class StripAccentsTest | ||
| * | ||
| * @package CodeInc\StripAccents\Tests | ||
| * @author Joan Fabrégat <joan@codeinc.fr> | ||
| */ | ||
| class StripAccentsTest extends TestCase | ||
| { | ||
| public const ACCENTS = [ | ||
| 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', | ||
| 'Ï', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', | ||
| 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ò', | ||
| 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ']; | ||
|
|
||
| public const LETTERS = [ | ||
| 'A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', | ||
| 'I', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 'a', 'a', 'a', | ||
| 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'e', 'o', | ||
| 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y' | ||
| private const ACCENT_MAP = [ | ||
| 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', | ||
| 'Ç' => 'C', | ||
| 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', | ||
| 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', | ||
| 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', | ||
| 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', | ||
| 'Ý' => 'Y', | ||
| 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', | ||
| 'ç' => 'c', | ||
| 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', | ||
| 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', | ||
| 'ð' => 'e', | ||
| 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', | ||
| 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', | ||
| 'ý' => 'y', 'ÿ' => 'y', | ||
| ]; | ||
|
|
||
| public function testOneByOne():void | ||
| public static function accentProvider(): iterable | ||
| { | ||
| foreach (self::ACCENTS as $key => $accent) { | ||
| self::assertTrue(StripAccents::strip($accent) == self::LETTERS[$key]); | ||
| foreach (self::ACCENT_MAP as $accented => $expected) { | ||
| yield $accented => [$accented, $expected]; | ||
| } | ||
| } | ||
|
|
||
| public function testAll():void | ||
| #[DataProvider('accentProvider')] | ||
| public function testIndividualAccent(string $accented, string $expected): void | ||
| { | ||
| $this->assertSame($expected, StripAccents::strip($accented)); | ||
| } | ||
|
|
||
| public function testAllAccentsConcatenated(): void | ||
| { | ||
| self::assertTrue( | ||
| StripAccents::strip(implode("", self::ACCENTS)) | ||
| == implode(self::LETTERS) | ||
| $input = implode('', array_keys(self::ACCENT_MAP)); | ||
| $expected = implode('', array_values(self::ACCENT_MAP)); | ||
| $this->assertSame($expected, StripAccents::strip($input)); | ||
| } | ||
|
|
||
| public function testSentence(): void | ||
| { | ||
| $this->assertSame( | ||
| "C'est une super chaine de caracteres avec beaucoup d'accents", | ||
| StripAccents::strip("C'est une super chaîne de caractères avec beaucoup d'accents"), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public function testAsciiPassthrough(): void | ||
| { | ||
| $ascii = 'Hello World 123 !@#'; | ||
| $this->assertSame($ascii, StripAccents::strip($ascii)); | ||
| } | ||
|
|
||
| public function testEmptyString(): void | ||
| { | ||
| $this->assertSame('', StripAccents::strip('')); | ||
| } | ||
|
|
||
| public function testLigatures(): void | ||
| { | ||
| $this->assertSame('oe', StripAccents::strip('œ')); | ||
| $this->assertSame('AE', StripAccents::strip('Æ')); | ||
| } | ||
|
|
||
| public function testStripNonPrint(): void | ||
| { | ||
| $this->assertSame('ABC AEY ', StripAccents::stripNonPrint("ABC ÀÈÝ 是我这")); | ||
| } | ||
|
|
||
| public function testStripNonPrintWithReplacement(): void | ||
| { | ||
| $result = StripAccents::stripNonPrint("ABC ÀÈÝ 是我这", '-'); | ||
| $this->assertStringStartsWith('ABC AEY ', $result); | ||
| // each CJK character is 3 bytes in UTF-8, each non-printable byte is replaced individually | ||
| $this->assertSame(9, substr_count($result, '-')); | ||
| } | ||
|
|
||
| public function testStripNonPrintEmptyString(): void | ||
| { | ||
| $this->assertSame('', StripAccents::stripNonPrint('')); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The workflow is restricted to
branches: [master]. If the default branch ismain(as suggested by the PR diff base), CI won’t run for pushes/PRs to the default branch. Consider switching this filter to the actual default branch name or removing the branch filter to ensure CI runs on all PRs.