deetoo / essentials
Essentials By Jordan Dobrev
Installs: 157
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 6
Type:package
Requires
- doctrine/inflector: ~1.0
- guzzlehttp/guzzle: ~6.0
- laravel/framework: ^5.4
- umpirsky/country-list: ^2.0
Requires (Dev)
- fzaninotto/faker: 1.4.0
- laravel/laravel: ^5.4
- mockery/mockery: *
- phpspec/phpspec: ~2.1
- phpunit/phpunit: ~6.0
- squizlabs/php_codesniffer: 3.*
- theseer/phpdox: *
- dev-master
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/composer/symfony/http-kernel-5.4.20
- dev-dependabot/composer/guzzlehttp/guzzle-7.4.5
- dev-dependabot/composer/guzzlehttp/psr7-1.8.5
- dev-dependabot/composer/laravel/laravel-8.4.4
- dev-dependabot/composer/league/flysystem-1.1.4
- dev-dependabot/composer/laravel/framework-8.40.0
This package is auto-updated.
Last update: 2024-10-30 01:26:35 UTC
README
DeeToo Essentials provides tools that enhances your Laravel experience.
Models
The DeeToo Model provides an extra layer of validation to protect you against yourself. If validates the data before you save it to the database and verifies if a relation actually exists. It also provides nice additional features like read only properties and value objects.
<?php 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(), ]; } }
Each type can be set as nullable and have a default value:
(new IntegerType()) ->nullable() ->default(1)
Each different type can have his own options on top:
(new IntegerType()) ->nullable() ->default(1) ->unsigned() ->max(100)
You can extend and create new value object as well:
<?php 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); } }
Where the serialize
array is the list of values that will be serialized when the Model is converted to Array/Json.
You can also create your own types:
<?php 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'); } } }
You can add options that you can chain. All you need is to validate them accordingly in the validate
method.
Filters
The DeeToo Filters and easy way to provide human readable api filters your Eloquent Models.
<?php 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']);
You can use any of the predefined filters or create your own by implementing the FilterContract