PHP code example of smpita / typeas

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

    

smpita / typeas example snippets


use Smpita\TypeAs\TypeAs;

$typed = TypeAs::string($mixed);

use Smpita\TypeAs\TypeAs;

$typed = TypeAs::string($mixed, '');

use Smpita\TypeAs\TypeAs;

$typed = TypeAs::class(Target::class, $mixed);

use Smpita\TypeAs\TypeAs;

$typed = TypeAs::class(Target::class, $mixed, new \StdClass);

use Smpita\TypeAs\TypeAs;

$typed = TypeAs::class($mixed, Target::class, $default);

use Smpita\TypeAs\TypeAs;

TypeAs::array('example') === ['example'];

use Smpita\TypeAs\TypeAs;

$typed = TypeAs::array($mixed, false);

use Smpita\TypeAs\TypeAs;

$typed = TypeAs::array($mixed, []);

use Smpita\TypeAs\TypeAs;

TypeAs::nullableString(new \stdClass) === null

use Smpita\TypeAs\Contracts\StringResolver;

class CustomStringResolver implements StringResolver
{
    /**
     * @throws \UnexpectedValueException
     */
    public function resolve(mixed $value, string $default = null): string
    {
        // Your logic here
    }
}

TypeAs::setStringResolver(new CustomStringResolver);

$typed = Smpita\TypeAs::string($mixed, null, new CustomStringResolver);

TypeAs::setStringResolver(null);

TypeAs::useDefaultResolvers();

$typed = Smpita\TypeAs::string($mixed, null, new \Smpita\TypeAs\Resolvers\AsString);

use function Smpita\TypeAs\asString;

$typed = asString($mixed);