PHP code example of fab2s / laravel-dt0

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

    

fab2s / laravel-dt0 example snippets


    #[Cast(in: new CollectionOfCaster(MyDt0::class))] // Dt0|UnitEnum|ScalarType|string
    public readonly Collection $prop;

#[Validate(Validator::class)] // same as #[Validate(new Validator)]
class MyDt0 extends Dt0 {
    // ...
}

    use fab2s\Dt0\Attribute\Rule;
    use fab2s\Dt0\Attribute\Rules;
    use fab2s\Dt0\Attribute\Validate;
    use fab2s\Dt0\Laravel\Dt0;
    use fab2s\Dt0\Laravel\Validator;
    
    #[Validate(
        Validator::class,
        new Rules(
            propName: new Rule('string|size:2'),
            // ...
        ),
    )]
    class MyDt0 extends Dt0 {
        public readonly string $propName;
    }
    

    use fab2s\Dt0\Attribute\Rule;
    use fab2s\Dt0\Attribute\Rules;
    use fab2s\Dt0\Attribute\Validate;
    use fab2s\Dt0\Laravel\Dt0;
    use fab2s\Dt0\Laravel\Validator;
    
    #[Validate(Validator::class)]
    #[Rules(
        propName: new Rule(['

    use fab2s\Dt0\Attribute\Rule;
    use fab2s\Dt0\Attribute\Rules;
    use fab2s\Dt0\Attribute\Validate;
    use fab2s\Dt0\Laravel\Dt0;
    use fab2s\Dt0\Laravel\Validator;
    use fab2s\Dt0\Laravel\Tests\Artifacts\Rules\Lowercase;
    
    #[Validate(Validator::class)]
    class MyDt0 extends Dt0 {
        #[Rule(new Lowercase)] // or any custom rule instance
        public readonly string $propName;
    }
    

// either get a Dt0 instance or a ValidationException
$dt0 = SomeValidatableDt0::withValidation(...Request::all());

use Illuminate\Database\Eloquent\Model;

class SomeModel extends Model
{
    protected $casts = [
        'some_dt0'          => SomeDt0::class,
        'some_nullable_dt0' => SomeNullableDt0::class.':nullable',
    ];
}

$model = new SomeModel;

$model->some_dt0 = '{"field":"value"}';
// or 
$model->some_dt0 = ['field' => 'value'];
// or 
$model->some_dt0 = SomeDt0::from(['field' => 'value']);

// then
$model->some_dt0->equals(SomeDt0::from('{"field":"value"}')); // true

$model->some_dt0 = null; // throws a NotNullableException
$model->some_nullable_dt0 = null; // works

// can thus be tried
$model->some_nullable_dt0 = SomeNullableDt0::tryFrom($anyInput);