signpostmarv / daft-typed-object
Typed Object, a simplified version of signpostmarv/daft-object
v0.5.5
2020-04-21 15:08 UTC
Requires
- php: ^7.4
- ext-json: *
Requires (Dev)
- ext-xdebug: *
- infection/infection: ^0.16.2
- maglnet/composer-require-checker: ^2.0
- php-coveralls/php-coveralls: ^2.2
- phpunit/phpunit: ^8.5
- povils/phpmnd: ^2.2
- psalm/plugin-phpunit: ^0.10.0
- roave/security-advisories: dev-master
- sebastian/phpcpd: ^5.0
- signpostmarv/php-cs-fixer-config: ^0.17.2
- vimeo/psalm: ^3.11
Suggests
- vimeo/psalm: for static analysis & templated typehinting
Conflicts
- vimeo/psalm: <3.11.2
This package is auto-updated.
Last update: 2024-12-19 22:56:09 UTC
README
Copyright 2019 SignpostMarv
Daft-Typed-Object
Typed Object, a simplified version of signpostmarv/daft-object
Example
Immutable
use SignpostMarv\DaftTypedObject\Immutable as Base; use SignpostMarv\DaftTypedObject\Immutable as Base; /** * @psalm-type DATA = array{id:int, name:string} * * @template-extends Base<DATA, DATA> */ class Immutable extends Base { const TYPED_PROPERTIES = ['id', 'name']; /** * @readonly */ public int $id; /** * @readonly */ public string $name; }
Mutable
use SignpostMarv\DaftTypedObject\DaftTypedObject as Base; /** * @psalm-type DATA = array{id:int, name:string} * * @template-extends Base<DATA, DATA> */ class Mutable extends Base { const TYPED_PROPERTIES = ['id', 'name']; public int $id; public string $name; }
Mutable with DateTimeImmutable
& nullables
use DateTimeImmutable; use SignpostMarv\DaftTypedObject\DaftTypedObject as Base; /** * @template T as array{id:int, name:string, date:DateTimeImmutable|null} * @template S as array{id:int, name:string, date:string|null} * * @template-extends Base<T, S> * * @property-read int $id * @property-read string $name * @property-read DateTimeImmutable $date */ class MutableWithNullables extends Base { const TYPED_PROPERTIES = ['id', 'name', 'date']; const TYPED_NULLABLE_PROPERTIES = ['date']; /** * @var int */ protected $id; /** * @var string|null */ protected $name; /** * @var DateTimeImmutable|null */ protected $date; /** * @template K as key-of<T> * * @param K $property * @param T[K] $value * * @return S[K] */ public static function PropertyValueToScalarOrNull( string $property, $value ) { /** * @var T[K]|DateTimeImmutable */ $value = $value; if ($value instanceof DateTimeImmutable) { /** * @var S[K] */ return (string) $value->format('Y-m-d'); } /** * @var S[K] */ return parent::PropertyValueToScalarOrNull((string) $property, $value); } /** * @template K as key-of<S> * * @param K|'date' $property * @param S[K] $value * * @return T[K] */ public static function PropertyScalarOrNullToValue( string $property, $value ) { /** * @var S[K]|string */ $value = $value; if ('date' === $property && is_string($value)) { $out = new DateTimeImmutable($value); } else { /** * @var S[K] */ $value = $value; /** * @var T[K] */ $out = parent::PropertyScalarOrNullToValue( (string) $property, $value ); } /** * @var T[K] */ return $out; } }