PHP code example of spaceonfire / type

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

    

spaceonfire / type example snippets


use spaceonfire\Type\BuiltinType;
use Webmozart\Assert\Assert;

$int = new BuiltinType(BuiltinType::INT);
Assert::true($int->check(1));
Assert::false($int->check('1'));

$intNonStrict = new BuiltinType(BuiltinType::INT, false);
Assert::true($intNonStrict->check('1'));
Assert::same(1, $intNonStrict->cast('1'));

use spaceonfire\Type\Factory\CompositeTypeFactory;
use spaceonfire\Type\Factory\MemoizedTypeFactory;

$factory = new MemoizedTypeFactory(CompositeTypeFactory::makeWithDefaultFactories());
$factory->make('int');
$factory->make('string[]');
$factory->make('array<string,object>');
$factory->make('int|null');
$factory->make('Traversable|iterable|null');
$factory->make('Traversable&JsonSerializable');