1. Go to this page and download the library: Download spatie/typed 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/ */
spatie / typed example snippets
// This collection will only allow objects of type `Post` in it.
$postCollection = new Collection([new Post()]);
// This tuple will keep its signature of (Point, Point).
$vector = new Tuple(new Point(30, 5), new Point(120, 0));
// This struct's fields are autmoatically type checked.
$struct = [
'foo' => new Post(),
'bar' => function () {
// ...
},
];
$list = new Collection(T::bool());
$list[] = new Post(); // TypeError
$list = (new Collection(T::string()))->set(['a', 'b', 'c']);
$list = new IntegerList([1, 4]);
$list[] = 'a'; // TypeError
$postList = new Collection(T::generic(Post::class));
$postList[] = 1; // TypeError
$postList = new Collection(T::generic(Post::class));
$postList[] = new Post();
$postList[] = 1; // TypeError
use Spatie\Typed\Type;
use Spatie\Typed\Types\Nullable;
class PostType implements Type
{
use Nullable;
public function validate($post): Post
{
return $post;
}
}
$postList = new Collection(new PostType());
class T extends Spatie\Typed\T
{
public static function post(): PostType
{
return new PostType();
}
}
// ...
$postList = new Collection(T::post());
public function nullable(): NullType
{
return new NullType($this);
}
class Coordinates extends Tuple
{
public function __construct(int $x, int $y)
{
parent::__construct(T::int(), T::int());
$this[0] = $x;
$this[1] = $y;
}
}
$postList = new Collection<Post>();
// vs.
$postList[] = new Collection(T::generic(Post::class));
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.