1. Go to this page and download the library: Download xtompie/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/ */
xtompie / typed example snippets
use Xtompie\Typed\Max;
use Xtompie\Typed\Min;
use Xtompie\Typed\NotBlank;
use Xtompie\Typed\Typed;
Class PetPayload
{
public function __construct(
#[NotBlank]
protected string $name,
#[NotBlank]
#[Min(0)]
#[Max(30)]
protected int $age,
) {}
public function name(): string
{
return $this->name;
}
public function age(): int
{
return $this->age;
}
}
$pet = Typed::typed(PetPayload::class, $_POST);
Class Typed
{
/**
* @template T of object
* @param class-string<T> $type
* @param mixed $input
* @return T|ErrorCollection
*/
public static function object(string $type, mixed $input): object
{
// ...
}
// ...
}
use Xtompie\Typed\NotBlank;
use Xtompie\Typed\Typed;
class Author
{
public function __construct(
#[NotBlank]
protected string $name,
) {
}
}
class Article
{
public function __construct(
protected Author $author,
) {
}
}
$article = Typed::typed(Article::class, ['author' => ['name' => 'John']]);
var_dump($article);
/* Output:
object(Article)#4 (1) {
["author":protected] => object(Author)#9 (1) {
["name":protected] => string(4) "John"
}
}
*/
use Xtompie\Typed\ArrayOf;
use Xtompie\Typed\NotBlank;
use Xtompie\Typed\Typed;
class Comment
{
public function __construct(
#[NotBlank]
protected string $text,
) {
}
}
class Article
{
public function __construct(
#[ArrayOf(Comment::class)]
protected array $comments,
) {
}
}
$article = Typed::typed(Article::class, ['comments' => [['text' => 'A'], ['text' => 'B']]]);
var_dump($article);
/* Output:
object(Article)#6 (1) {
["comments":protected] => array(2) {
[0] => object(Comment)#12 (1) {
["text":protected] => string(1) "A"
}
[1] => object(Comment)#13 (1) {
["text":protected] => string(1) "B"
}
}
}
*/
use Xtompie\Typed\Source;
use Xtompie\Typed\Typed;
class ArticleQuery
{
public function __construct(
#[Source('id:qt')]
protected int $idGt,
) {
}
}
$query = Typed::typed(ArticleQuery::class, ['id:qt' => 1234]);
var_dump($query);
/* Output:
object(ArticleQuery)#4 (1) {
["idGt":protected] => int(1234)
}
*/
use Xtompie\Result\ErrorCollection;
use Xtompie\Typed\Callback;
use Xtompie\Typed\NotBlank;
use Xtompie\Typed\Typed;
#[Callback('typed')]
class Password
{
public function __construct(
#[NotBlank]
protected string $new_password,
protected string $new_password_confirm,
) {
}
protected function passwordIdentical(): bool
{
return $this->new_password === $this->new_password_confirm;
}
public function typed(): static|ErrorCollection
{
if (!$this->passwordIdentical()) {
return ErrorCollection::ofErrorMsg('Passwords must be indentical', 'identical', 'new_password_confirm');
}
return $this;
}
}
$password = Typed::typed(Password::class, ['new_password' => '1234', 'new_password_confirm' => '123']);
var_dump($password);
/* Output:
object(Xtompie\Result\ErrorCollection)#7 (1) {
["collection":protected] => array(1) {
[0] => object(Xtompie\Result\Error)#4 (3) {
["message":protected] => string(28) "Passwords must be indentical"
["key":protected] => string(9) "identical"
["space":protected] => string(20) "new_password_confirm"
}
}
}
*/
use Xtompie\Result\ErrorCollection;
use Xtompie\Typed\Factory;
use Xtompie\Typed\Typed;
#[Factory(class: Time::class, method: 'typed')]
class Time
{
public static function typed(mixed $input): static|ErrorCollection
{
$input = (int)$input;
if ($input < 0) {
return ErrorCollection::ofErrorMsg('Time must be positive', 'time');
}
return new Time($input);
}
public function __construct(
protected int $time,
) {
}
}
class Article
{
public function __construct(
protected Time $time,
) {
}
}
$article = Typed::typed(Article::class, ['time' => time()]);
use Attribute;
use Xtompie\Result\ErrorCollection;
use Xtompie\Typed\Assert;
#[Attribute(Attribute::TARGET_PARAMETER)]
class Positive implements Assert
{
public function assert(mixed $input, string $type): mixed
{
$input = (int)$input;
if ($input < 0) {
return ErrorCollection::ofErrorMsg(
message: 'Value must be positive',
key: 'positive',
);
}
return $input;
}
}
class Pet
{
public function __construct(
#[Positive]
protected int $age,
) {
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.