PHP code example of compwright / philter

1. Go to this page and download the library: Download compwright/philter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

compwright / philter example snippets


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