PHP code example of deetoo / essentials

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

    

deetoo / essentials example snippets




use DeeToo\Essentials\Laravel\Eloquent\Model;
use DeeToo\Essentials\Laravel\Eloquent\Types\ArrayType;
use DeeToo\Essentials\Laravel\Eloquent\Types\BooleanType;
use DeeToo\Essentials\Laravel\Eloquent\Types\DateTimeType;
use DeeToo\Essentials\Laravel\Eloquent\Types\DateType;
use DeeToo\Essentials\Laravel\Eloquent\Types\EmailType;
use DeeToo\Essentials\Laravel\Eloquent\Types\EnumerableType;
use DeeToo\Essentials\Laravel\Eloquent\Types\FloatType;
use DeeToo\Essentials\Laravel\Eloquent\Types\IntegerType;
use DeeToo\Essentials\Laravel\Eloquent\Types\ObjectType;
use DeeToo\Essentials\Laravel\Eloquent\Types\RelationType;
use DeeToo\Essentials\Laravel\Eloquent\Types\StringType;
use DeeToo\Essentials\Laravel\Eloquent\Types\TextType;
use DeeToo\Essentials\Laravel\Eloquent\Types\UrlType;
use DeeToo\Essentials\Laravel\Eloquent\Types\ValueObjectType;
use DeeToo\Essentials\ValueObjects\Country;
use DeeToo\Essentials\ValueObjects\Currency;
use DeeToo\Essentials\ValueObjects\Temperature;

class TestModel extends Model
{
    protected $readOnly = ['read_only'];

    public function types(): array
    {
        return [
            'integer'     => new IntegerType(),
            'float'       => new FloatType(),
            'string'      => new StringType(),
            'boolean'     => new BooleanType(),
            'datetime'    => new DateTimeType(),
            'date'        => new DateType(),
            'email'       => new EmailType(),
            'enumerable'  => new EnumerableType(['one', 'two']),
            'array'       => new ArrayType(),
            'object'      => new ObjectType(),
            'relation_id' => new RelationType(self::class),
            'text'        => new TextType(),
            'url'         => new UrlType(),
            'country'     => new ValueObjectType(Country::class),
            'vo'          => new ValueObjectType(Currency::class),
            'temp'        => new ValueObjectType(Temperature::class),
            'read_only'   => new StringType(),
        ];
    }
}


(new IntegerType())
    ->nullable()
    ->default(1)

(new IntegerType())
    ->nullable()
    ->default(1)
    ->unsigned()
    ->max(100)



use DeeToo\Essentials\Exceptions\Error;

class Humidity extends ValueObject
{
    public array $serialize = [
        'formatted',
    ];

    public function __construct($value)
    {
        parent::__construct(intval($value));

        if (!is_numeric($this->value)) {
            throw new Error('Invalid percentage value :value', ['value' => $this->value]);
        }

        if ($this->value > 10000) {
            throw new Error('Percentage value :value cannot be more than 10000', ['value' => $this->value]);
        }

        if ($this->value < 0) {
            throw new Error('Percentage value :value cannot be more negative', ['value' => $this->value]);
        }
    }

    public function formatted(): string
    {
        return format()->percent($this->value);
    }
}



use DeeToo\Essentials\Exceptions\Error;

/**
 * Class EmailType
 */
class EmailType extends Type
{
    public function validate($value)
    {
        if (filter_var($value, FILTER_VALIDATE_EMAIL) === false) {
            throw new Error('must be a valid email address');
        }
    }
}



use DeeToo\Essentials\Laravel\Eloquent\Model;
use DeeToo\Essentials\Laravel\Eloquent\Traits\Filterable;
use DeeToo\Essentials\Laravel\Eloquent\Types\StringType;
use DeeToo\Essentials\Laravel\Filters\Filters\Search;

class FilterableModel extends Model
{
    use Filterable;

    protected $readOnly = ['read_only'];

    public function types(): array
    {
        return [
            'name' => new StringType(),
        ];
    }
    
    public function filters(): array
    {
        return [
            'search' => new Search(['name']),
        ];
    }
}

## USAGE

$model = new FilterableModel();

$model->apply(['search' => 'search_string_to_fetch_records']);