PHP code example of forrest79 / type-validator

1. Go to this page and download the library: Download forrest79/type-validator 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/ */

    

forrest79 / type-validator example snippets


assert(is_int($data)); // if we know, there will be always an int
if (!is_int($data)) throw new InvalidDataException(); // if we want to check this also in runtime

$arr = [3.14, 5, 10];
assert(is_type($arr, 'list<float|int>'));
assert(Forrest79\TypeValidator::isType($arr, 'list<float|int>'));
Forrest79\TypeValidator::checkType($arr, 'list<float|int>'));

/** @var array<string|int, list<Db\Row>> $arr
$arr = json_decode($data);

$arr = json_decode($data);
assert(is_type($arr, 'array<string|int, list<Db\Row>>'));

namespace App;

use App\Presenter; // this use is marked as unused

assert($presenter, 'class-string<Presenter>'); // even though it is correctly used here

namespace App;

assert($presenter, 'class-string<\App\Presenter>');