compwright / philter
A user-friendly interface for the PHP filter module
v1.0.1
2026-08-14 18:07 UTC
Requires
- php: ^8.5
- ext-filter: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.2
- phpunit/phpunit: ^13.3
This package is auto-updated.
Last update: 2026-08-14 18:07:14 UTC
README
A user-friendly interface for the PHP filter module
- Easy to use type-hinted API with IDE support
- Sticks to a reasonable subset of functionality (FILTER_VALIDATOR_*) and eschews deprecated and removed
filterfeatures - Values fall back to null by default
- Supports named parameters
- All objects are readonly, inspectable, and idempotent
- High code quality that passes PHPStan level 9 and a full suite of unit tests
Supported Validators:
- Boolean
- Integer
- Float
- Regexp
- Url
- Domain
- Ip
- Mac
- Callback
Each one utilizes the corresponding FILTER_VALIDATE_* constant, with its supported options and flags.
Prerequisites
- PHP 8.5+
- PHP ext-filter module
Installation
composer require --save compwright/philter
Example Usage
use Compwright\Philter\Input; use Compwright\Philter\Phactory; // Args shown with defaults for demo purposes; omit if defaults are desired. $ph = new Phactory(throwOnFailure: false, default: 'foo'); // Factory settings can be changed idempotently later $ph2 = $ph->throwOnFailure(false)->setDefault(null); assert($ph2 !== $ph1); // Validators can be executed directly or on standard inputs $boolean = $ph->boolean(); $featureFlag = $boolean('true'); $featureFlag = Input::get('feature-flag', $boolean); $featureFlag = Input::post('feature-flag', $boolean); $featureFlag = Input::cookie('feature-flag', $boolean); $featureFlag = Input::server('feature-flag', $boolean); $featureFlag = Input::env('feature-flag', $boolean); // Validator-specific configuration $integer = $ph->integer(min: -100, max: 100, allowOctal: true, allowHex: true); assert($integer('42') === 42); assert($integer('-27') === -27); assert($integer('0b1') === 1); // binary assert($integer('0o1') === 1); // octal assert($integer('0x1') === 1); // hexadecimal assert($integer(27.5) === null); // wrong type assert($integer('142') === null); // out of range
License
MIT License