PHPackages                             lc5/from-array - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. lc5/from-array

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

lc5/from-array
==============

Create objects from arrays with type checks

1.0.0(4y ago)42.5kMITPHPPHP &gt;=7.4CI failing

Since Mar 4Pushed 3y ago1 watchersCompare

[ Source](https://github.com/Lc5/FromArray)[ Packagist](https://packagist.org/packages/lc5/from-array)[ Docs](https://github.com/Lc5/FromArray)[ RSS](/packages/lc5-from-array/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

FromArray
=========

[](#fromarray)

[![Build Status](https://github.com/Lc5/FromArray/workflows/Build/badge.svg)](https://github.com/Lc5/FromArray/actions)[![Latest Stable Version](https://camo.githubusercontent.com/b38c5a04285c623770723ecac5d2e7373fa8bb9c258d6f0cccccb06c9251431d/687474703a2f2f706f7365722e707567782e6f72672f6c63352f66726f6d2d61727261792f76)](https://packagist.org/packages/lc5/from-array)[![Total Downloads](https://camo.githubusercontent.com/667e88ae4df2a7d3cc9c90a8b7e8ab65e470bfb867b6e89753ac2d67a4225923/687474703a2f2f706f7365722e707567782e6f72672f6c63352f66726f6d2d61727261792f646f776e6c6f616473)](https://packagist.org/packages/lc5/from-array)[![PHP Version Require](https://camo.githubusercontent.com/3f4109257414e2172027a0120129fc0c517a5638d5a494d39536bfa3400d9a86/687474703a2f2f706f7365722e707567782e6f72672f6c63352f66726f6d2d61727261792f726571756972652f706870)](https://packagist.org/packages/lc5/from-array)[![License](https://camo.githubusercontent.com/c04fefc9221fd243d35172bb4805e9fada7fe74097564e467fad4721c93cbdef/687474703a2f2f706f7365722e707567782e6f72672f6c63352f66726f6d2d61727261792f6c6963656e7365)](https://packagist.org/packages/lc5/from-array)[![PHPStan Enabled](https://camo.githubusercontent.com/441b5874ce4df0a2defc892979c96c46889b69cb32119d04f0b48626349f8bc9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d656e61626c65642d627269676874677265656e2e7376673f7374796c653d666c6174)](https://phpstan.org/)

Create objects from arrays with type checks.

Installation
------------

[](#installation)

```
$ composer require lc5/from-array

```

Usage
-----

[](#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](#supported-annotations)

Aforementioned behaviour can be configured. See [Options](#options)

### Basic example

[](#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

[](#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
----

[](#docs)

### Options

[](#options)

The following options are available:

- `DEFAULT` - check for missing and redundant properties and check types
- `VALIDATE_MISSING` - check for missing properties
- `VALIDATE_REDUNDANT` - check for redundant properties
- `VALIDATE_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:

### Supported Annotations

[](#supported-annotations)

The following doc block annotations are supported:

- `callable` - standard PHP callable type
- `mixed` - represents PHP mixed typed, which basically means any type
- `T[]` - 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](https://www.php.net/manual/en/language.oop5.properties.php)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1572d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3e968739c39a6ff1eb266d168916f388ba3523eb247cd5820afd42919cc2385f?d=identicon)[Lc5](/maintainers/Lc5)

---

Top Contributors

[![Lc5](https://avatars.githubusercontent.com/u/3340775?v=4)](https://github.com/Lc5 "Lc5 (36 commits)")

---

Tags

arraytraitobjectarraystraitsobjects

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleECS

Type Coverage Yes

### Embed Badge

![Health badge](/badges/lc5-from-array/health.svg)

```
[![Health](https://phpackages.com/badges/lc5-from-array/health.svg)](https://phpackages.com/packages/lc5-from-array)
```

###  Alternatives

[symfony/property-access

Provides functions to read and write from/to an object or array using a simple string notation

2.8k309.5M3.0k](/packages/symfony-property-access)[cuyz/valinor

Dependency free PHP library that helps to map any input into a strongly-typed structure.

1.5k11.7M153](/packages/cuyz-valinor)[minwork/array

Pack of advanced array functions specifically tailored for: associative (assoc) array, multidimensional array, array of objects and handling nested array elements

63263.9k5](/packages/minwork-array)[lukascivil/treewalker

TreeWalker is a simple and small Library that will help you to work faster with manipulation of structures in PHP

731.1M7](/packages/lukascivil-treewalker)[sarhan/php-flatten

Flattens multidimensional arrays, traversables and vars into one dimensional array.

21182.2k1](/packages/sarhan-php-flatten)[jasny/dotkey

Dot notation access for objects and arrays

14222.3k7](/packages/jasny-dotkey)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
