lc5 / from-array
Create objects from arrays with type checks
Installs: 1 257
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
Requires (Dev)
- phpstan/phpstan: ^1.1
- phpunit/phpunit: ^9.5
- symplify/easy-coding-standard: ^9.4
This package is not auto-updated.
Last update: 2025-04-13 11:46:10 UTC
README
Create objects from arrays with type checks.
Installation
$ composer require lc5/from-array
Usage
Add FromArrayTrait
to the class you wish to be instantiated with the values from the array. It provides
fromArray
method, which will validate the data and create the object if the data is valid. Otherwise, either PHP
TypeError
or Lc5\FromArray\Exception\InvalidArgumentException
will be thrown.
The validation consists of the following steps:
- check if all the required properties are present
- check if there are no redundant properties
- check if all the properties have correct types according to the doc blocks. See Supported Annotations
Aforementioned behaviour can be configured. See Options
Basic example
use Lc5\FromArray\FromArrayTrait; class ExampleClass { use FromArrayTrait; private bool $bool; private int $int; private float $float; private string $string; private array $array; private object $object; } $properties = [ 'bool' => true, 'int' => 2, 'float' => 3.5, 'string' => 'example string', 'array' => ['example array'], 'object' => new stdClass() ]; $exampleObject = ExampleClass::fromArray($properties);
Advanced example
use Lc5\FromArray\FromArrayTrait; class ExampleClass { use FromArrayTrait; /** @var callable */ private $callable; /** @var mixed[] */ private iterable $iterable; /** @var stdClass[] */ private array $typedArray; /** @var stdClass[] */ private iterable $typedIterable; /** @var mixed */ private $mixed; /** @var int|float */ public $intOrFloat; } $properties = [ 'callable' => function (): void {}, 'iterable' => new ArrayObject(), 'typedArray' => [new stdClass(), new stdClass()], 'typedIterable' => new ArrayObject([new stdClass(), new stdClass()]), 'mixed' => 'mixed', 'intOrFloat' => 1.5 ]; $exampleObject = ExampleClass::fromArray($properties);
Docs
Options
The following options are available:
DEFAULT
- check for missing and redundant properties and check typesVALIDATE_MISSING
- check for missing propertiesVALIDATE_REDUNDANT
- check for redundant propertiesVALIDATE_TYPES
- check types of properties
Options can be combined using bitwise operators. To disable validation of redundant properties in order to be able to pass an array with more properties you can use the following code:
ExampleClass::fromArray($properties, Options::DEFAULT & ~Options::VALIDATE_REDUNDANT);
More info: https://www.php.net/manual/en/language.operators.bitwise.php
Supported Annotations
The following doc block annotations are supported:
callable
- standard PHP callable typemixed
- represents PHP mixed typed, which basically means any typeT[]
- represents typed iterable of items of a given type e.g.int[]
,stdClass[]
etc.- union types - e.g.
int|float
- representing union of types
Standard PHP types are supported by native Typed Properties