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;
// Throws \Smpita\TypeAs\TypeAsResolutionException if $mixed can't resolve to the type.
$array = TypeAs::array($mixed);
$bool = TypeAs::bool($mixed);
$class = TypeAs::class(Expected::class, $mixed);
$float = TypeAs::float($mixed);
$int = TypeAs::int($mixed);
$string = TypeAs::string($mixed);
// Returns null if $mixed can't resolve to the type.
$nullableArray = TypeAs::nullableArray($mixed);
$nullableBool = TypeAs::nullableBool($mixed);
$nullableClass = TypeAs::nullableClass(Expected::class, $mixed);
$nullableFloat = TypeAs::nullableFloat($mixed);
$nullableInt = TypeAs::nullableInt($mixed);
$nullableString = TypeAs::nullableString($mixed);
use Smpita\TypeAs\TypeAs;
// Returns the default if passed null, or if $mixed can't resolve to the type.
$array = TypeAs::array($mixed, []);
$bool = TypeAs::bool($mixed, false);
$class = TypeAs::class(Expected::class, $mixed, new StdClass());
$float = TypeAs::float($mixed, 0.0);
$int = TypeAs::int($mixed, 0);
$string = TypeAs::string($mixed, '');
// Nullable types can specify defaults.
$nullableArray = TypeAs::nullableArray($mixed, []);
$nullableBool = TypeAs::nullableBool($mixed, false);
$nullableClass = TypeAs::nullableClass(Expected::class, $mixed, new StdClass());
$nullableFloat = TypeAs::nullableFloat($mixed, 0.0);
$nullableInt = TypeAs::nullableInt($mixed, 0);
$nullableString = TypeAs::nullableString($mixed, '');
// As a named parameter
$array = TypeAs::array(value: $mixed, default: []);
use Smpita\TypeAs\TypeAs;
TypeAs::array('example'); // returns ['example']
TypeAs::array(['example']); // returns ['example']
/**
* Disable array wrapping to get exceptions.
* These throw \Smpita\TypeAs\TypeAsResolutionException
*/
TypeAs::array('example', wrap: false);
TypeAs::array('example', null, null, false);
use Smpita\TypeAs\TypeAs;
$instance = TypeAs::type($mixed);
$assignment = $instance; // $assignment mutates when $instance changes.
$copy = $instance->copy(); // $copy is unaffected by changes to $instance.
$clone = clone $instance; // $clone is unaffected by changes to $instance.
use Smpita\TypeAs\Fluent\Nullable;
use Smpita\TypeAs\Fluent\NonNullable;
// Returns \Smpita\TypeAs\Fluent\TypeConfig
$config = NonNullable::make()->config();
$config = Nullable::make()->config();
use Smpita\TypeAs\Fluent\Nullable;
use Smpita\TypeAs\Fluent\NonNullable;
use Smpita\TypeAs\Fluent\TypeConfig;
$config = new TypeConfig(
fromValue: $mixed,
defaultTo: $default,
resolveUsing: $resolver,
arrayWrap: null,
);
$nonNullable = NonNullable::make($config);
$nonNullable = (new NonNullable())->import($config);
$nullable = Nullable::make($config);
$nullable = (new Nullable())->import($config);
use Smpita\TypeAs\TypeAs;
// Static API
TypeAs::onError('Expected iterable, received %s', CustomResolutionException::class)
->array($mixed);
// Fluent API
TypeAs::type($mixed)
->onError('Expected iterable, received %s', CustomResolutionException::class)
->asArray();
// Custom message only
onError('Expected iterable, received %s')
// Custom Exception only
onError(exception: CustomResolutionException::class)
onError(null, CustomResolutionException::class)
use Smpita\TypeAs\Contracts\StringResolver;
class CustomStringResolver implements StringResolver
{
/**
* Return null when unresolvable for default error handling.
*/
public function resolve(mixed $value, string $default = null): ?string
{
/**
* Your logic here
*
* Note:
* The resolver is responsible for returning $default when appropriate.
* See \Smpita\TypeAs\Resolvers for examples.
*/
}
}
use Smpita\TypeAs\TypeAs;
TypeAs::setArrayResolver(null);
TypeAs::setBoolResolver(null);
TypeAs::setClassResolver(null);
TypeAs::setFloatResolver(null);
TypeAs::setIntResolver(null);
TypeAs::setStringResolver(null);
// Return all resolvers to default
TypeAs::useDefaultResolvers();
use Smpita\TypeAs\TypeAs;
// Non-nullable methods
$array = TypeAs::array($mixed, resolver: new CustomArrayResolver());
$bool = TypeAs::bool($mixed, resolver: new CustomBoolResolver());
$class = TypeAs::class(Expected::class, $mixed, resolver: new CustomClassResolver());
$float = TypeAs::float($mixed, resolver: new CustomFloatResolver());
$int = TypeAs::int($mixed, resolver: new CustomIntResolver());
$string = TypeAs::string($mixed, resolver: new CustomStringResolver());
// Nullable methods use same resolver interface
$nullableArray = TypeAs::nullableArray($mixed, resolver: new CustomArrayResolver());
$nullableBool = TypeAs::nullableBool($mixed, resolver: new CustomBoolResolver());
$nullableClass = TypeAs::nullableClass(Expected::class, $mixed, resolver: new CustomClassResolver());
$nullableFloat = TypeAs::nullableFloat($mixed, resolver: new CustomFloatResolver());
$nullableInt = TypeAs::nullableInt($mixed, resolver: new CustomIntResolver());
$nullableString = TypeAs::nullableString($mixed, resolver: new CustomStringResolver());
// Or with positional params
$array = TypeAs::array($mixed, null, new CustomArrayResolver());
$string = TypeAs::string($mixed, null, new CustomStringResolver());
$nullableArray = TypeAs::nullableArray($mixed, null, new CustomArrayResolver());
$nullableString = TypeAs::nullableString($mixed, null, new CustomStringResolver());
use Smpita\TypeAs\TypeAs;
$array = TypeAs::array($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsArray());
$bool = TypeAs::bool($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsBool());
$class = TypeAs::class(Expected::class, $mixed, resolver: \Smpita\TypeAs\Resolvers\AsClass());
$float = TypeAs::float($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsFloat());
$int = TypeAs::int($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsInt());
$string = TypeAs::string($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsString());
// Nullable methods use same resolver classes
$nullableArray = TypeAs::nullableArray($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsArray());
$nullableBool = TypeAs::nullableBool($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsBool());
$nullableClass = TypeAs::nullableClass(Expected::class, $mixed, resolver: new \Smpita\TypeAs\Resolvers\AsClass());
$nullableFloat = TypeAs::nullableFloat($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsFloat());
$nullableInt = TypeAs::nullableInt($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsInt());
$nullableString = TypeAs::nullableString($mixed, resolver: new \Smpita\TypeAs\Resolvers\AsString());
use function Smpita\TypeAs\asArray as TypeAsArray;
use function Smpita\TypeAs\asBool as TypeAsBool;
use function Smpita\TypeAs\asFilterBool as TypeAsFilterBool;
use function Smpita\TypeAs\asClass as TypeAsClass;
use function Smpita\TypeAs\asFloat as TypeAsFloat;
use function Smpita\TypeAs\asInt as TypeAsInt;
use function Smpita\TypeAs\asString as TypeAsString;
use function Smpita\TypeAs\asNullableArray as TypeAsNullableArray;
use function Smpita\TypeAs\asNullableBool as TypeAsNullableBool;
use function Smpita\TypeAs\asNullableFilterBool as TypeAsNullableFilterBool;
use function Smpita\TypeAs\asNullableClass as TypeAsNullableClass;
use function Smpita\TypeAs\asNullableFloat as TypeAsNullableFloat;
use function Smpita\TypeAs\asNullableInt as TypeAsNullableInt;
use function Smpita\TypeAs\asNullableString as TypeAsNullableString;
use function Smpita\TypeAs\type as TypeAsType;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.