From 24e90a0123e215b3e2a77068049d72899763dd6b Mon Sep 17 00:00:00 2001 From: Daniel Scherzer Date: Tue, 26 Aug 2025 18:06:29 +0300 Subject: [PATCH] [8.5] Add polyfill for FILTER_THROW_ON_FAILURE and exceptions See https://wiki.php.net/rfc/filter_throw_on_failure --- README.md | 2 ++ src/Php85/README.md | 2 ++ .../stubs/Filter/FilterException.php | 19 +++++++++++++++++++ .../stubs/Filter/FilterFailedException.php | 19 +++++++++++++++++++ src/Php85/bootstrap.php | 4 ++++ tests/Php85/Php85Test.php | 9 +++++++++ 6 files changed, 55 insertions(+) create mode 100644 src/Php85/Resources/stubs/Filter/FilterException.php create mode 100644 src/Php85/Resources/stubs/Filter/FilterFailedException.php diff --git a/README.md b/README.md index 60edd9a4..0e0897a0 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,8 @@ Polyfills are provided for: - the `NoDiscard` attribute introduced in PHP 8.5; - the `array_first` and `array_last` functions introduced in PHP 8.5; - the `DelayedTargetValidation` attribute introduced in PHP 8.5; +- the `FILTER_THROW_ON_FAILURE` constant introduced in PHP 8.5; +- the `\Filter\FilterException` and `\Filter\FilterFailedException` classes introduced in PHP 8.5; It is strongly recommended to upgrade your PHP version and/or install the missing extensions whenever possible. This polyfill should be used only when there is no diff --git a/src/Php85/README.md b/src/Php85/README.md index 6cac5666..fd06d557 100644 --- a/src/Php85/README.md +++ b/src/Php85/README.md @@ -7,6 +7,8 @@ This component provides features added to PHP 8.5 core: - [`NoDiscard`](https://wiki.php.net/rfc/marking_return_value_as_important) - [`array_first` and `array_last`](https://wiki.php.net/rfc/array_first_last) - [`DelayedTargetValidation`](https://wiki.php.net/rfc/delayedtargetvalidation_attribute) +- `FILTER_THROW_ON_FAILURE`, `\Filter\FilterException`, and +`\Filter\FilterFailedException` (see [RFC](https://wiki.php.net/rfc/filter_throw_on_failure)) More information can be found in the [main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md). diff --git a/src/Php85/Resources/stubs/Filter/FilterException.php b/src/Php85/Resources/stubs/Filter/FilterException.php new file mode 100644 index 00000000..dbc7f840 --- /dev/null +++ b/src/Php85/Resources/stubs/Filter/FilterException.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Filter; + +if (\PHP_VERSION_ID < 80500) { + // @author Daniel Scherzer + class FilterException extends \Exception + { + } +} diff --git a/src/Php85/Resources/stubs/Filter/FilterFailedException.php b/src/Php85/Resources/stubs/Filter/FilterFailedException.php new file mode 100644 index 00000000..968a4f02 --- /dev/null +++ b/src/Php85/Resources/stubs/Filter/FilterFailedException.php @@ -0,0 +1,19 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Filter; + +if (\PHP_VERSION_ID < 80500) { + // @author Daniel Scherzer + class FilterFailedException extends FilterException + { + } +} diff --git a/src/Php85/bootstrap.php b/src/Php85/bootstrap.php index 44e872b1..26e3c8e3 100644 --- a/src/Php85/bootstrap.php +++ b/src/Php85/bootstrap.php @@ -30,3 +30,7 @@ function array_first(array $array) { return p\Php85::array_first($array); } if (!function_exists('array_last')) { function array_last(array $array) { return p\Php85::array_last($array); } } + +if (!defined('FILTER_THROW_ON_FAILURE')) { + define('FILTER_THROW_ON_FAILURE', 0x10000000); +} diff --git a/tests/Php85/Php85Test.php b/tests/Php85/Php85Test.php index 1ff50bd8..cb238ec5 100644 --- a/tests/Php85/Php85Test.php +++ b/tests/Php85/Php85Test.php @@ -114,6 +114,15 @@ public function testArrayFirstArrayLast() $this->assertTrue(array_first([true, new \stdClass()])); $this->assertEquals(new \stdClass(), array_last([true, new \stdClass()])); } + + public function testFilterThrowFlagValue() + { + // Make sure that the polyfill matches the real value, as of the + // initial implementation in + // https://github.com/php/php-src/commit/0b326dcbabe11e227f911bbde967b74fcada9e5a + $this->assertTrue(defined('FILTER_THROW_ON_FAILURE')); + $this->assertSame(0x10000000, FILTER_THROW_ON_FAILURE); + } } class TestHandler