PHPackages                             soyhuce/array-factory - 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. soyhuce/array-factory

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

soyhuce/array-factory
=====================

This is my package array-factory

2.2.0(2mo ago)19.0k[4 PRs](https://github.com/Soyhuce/array-factory/pulls)MITPHPPHP ^8.3CI passing

Since Sep 21Pushed 1mo ago3 watchersCompare

[ Source](https://github.com/Soyhuce/array-factory)[ Packagist](https://packagist.org/packages/soyhuce/array-factory)[ Docs](https://github.com/soyhuce/array-factory)[ RSS](/packages/soyhuce-array-factory/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (22)Versions (21)Used By (0)

This is array-factory package
=============================

[](#this-is-array-factory-package)

[![Latest Version on Packagist](https://camo.githubusercontent.com/a370dfee338b3d418ce6fe00bf5da0f4c4c15bd70bd3ed072d0133e003e787d2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f79687563652f61727261792d666163746f72792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/soyhuce/array-factory)[![GitHub Tests Action Status](https://camo.githubusercontent.com/be415534449955b89ea211287c01515e897b59cc54f7611f3a23257ebba7f5f0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f79687563652f61727261792d666163746f72792f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/soyhuce/array-factory/actions/workflows/run-tests.yml)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/2612d14db5c864b2f15ebcbe73fa6534569604700cad56f8e5bfd2bbff917e97/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f79687563652f61727261792d666163746f72792f7068707374616e2e796d6c3f6272616e63683d6d61696e266c6162656c3d7068707374616e267374796c653d666c61742d737175617265)](https://github.com/soyhuce/array-factory/actions/workflows/phpstan.yml)[![GitHub PHPStan Action Status](https://camo.githubusercontent.com/b5b0ad89cca2d59126b3631fcef687dcb1c1ea1b52e7e2389ff17b9f1eba9b75/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736f79687563652f61727261792d666163746f72792f7068702d63732d66697865722e796d6c3f6272616e63683d6d61696e266c6162656c3d7068702d63732d6669786572267374796c653d666c61742d737175617265)](https://github.com/soyhuce/array-factory/actions/workflows/php-cs-fixer.yml)[![Total Downloads](https://camo.githubusercontent.com/b9c0e2f9a6e280a9f3046d8259498dcddd46fff40b49f9c79bf8f4297a6abda2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f79687563652f61727261792d666163746f72792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/soyhuce/array-factory)

When testing your application, you may need to write the same data structures (lists, collections, Etc.) to populate models or something else. These often duplicated in multiple test files.

A bit like Laravel's Factory classes, ArrayFactory permits to generate and reuse custom data structures and usable among your tests.

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

[](#installation)

You can install the package via composer:

```
composer require --dev soyhuce/array-factory
```

Usage
-----

[](#usage)

#### Create a factory

[](#create-a-factory)

```
use Soyhuce\ArrayFactory;

$factory = new ArrayFactory(['foo' => 'bar']); // from an array
$factory = ArrayFactory::new(['foo' => 'bar']); // from static method new
$factory = ArrayFactory::new(fn () => ['foo' => 'bar']); // with a callable
$factory = ArrayFactory::new(fn () => ['foo' => faker()->word() ]); // using \Faker\Generator generation
```

#### Use it

[](#use-it)

```
$factory->createOne();
// ['foo' => 'bar']
$factory->createMany(['foo' => 'qux'], ['foo' => 'bar'])
// [['foo' => 'qux'], ['foo' => 'bar']]
$factory->count(2)->asCollection();
// Illuminate\Support\Collection([ ['foo' => 'bar'], ['foo' => 'bar'] ])
```

#### Add states

[](#add-states)

You may need to define states and use it in a specific context.

```
$factory = new ArrayFactory(
    definition: ['foo' => 'bar'],
    states: [
        'qux' => ['foo' => 'qux'],
    ]
);

$factory->createOne();
// ['foo' => 'bar']
$factory->state('qux')->createOne();
// ['foo' => 'qux']
```

#### Add custom Collection

[](#add-custom-collection)

You can transform the array into a custom Collection.

```
$factory = new ArrayFactory(
    collection: FooCollection::class,
);

$foo = FooFactory::new()->asCollection();
```

You can also transform the array into a custom Data object by defining the Spatie\\LaravelData\\Data class.

#### Add custom \\Spatie\\LaravelData\\Data

[](#add-custom-spatielaraveldatadata)

```
$factory = new ArrayFactory(
    data: FooData::class,
);

$factory = FooFactory::new()->asData();
```

It allows get many results as collection of datas

```
$datas = $factory->manyAsDataCollection(['id' => 2], ['id' => 3]);
// Collection([FooData(id: 2), FooData(id: 3)])
```

### Create an extended class

[](#create-an-extended-class)

```
class FooFactory extends ArrayFactory
{
    protected string $collection = FooCollection::class;

    protected string $data = FooData::class;

    public function definition(): array
    {
        return [
            'id' => 1,
            'activated' => true,
            'message' => faker()->sentence(),
            'value' => 0,
        ];
    }

    public function id(int $id): static
    {
        return $this->state(['id' => $id]);
    }

    public function activated(bool $activated = true): static
    {
        return $this->state(['activated' => $activated]);
    }
}
```

Then, you can simply use it in your tests.

```
    $foo1 = FooFactory::new()->createOne();
    // ['id' => 1, 'activated' => true, 'value' => 0]
    $foo1 = FooFactory::new()->createOne(['value' => 1]]);
     // ['id' => 1, 'activated' => true, 'value' => 1]
    $foo2 = FooFactory::new()->id(2)->createOne();
     // ['id' => 2, 'activated' => true, 'value' => 0]
    $foo3 = FooFactory::new()->activated(false)->createOne();
     // ['id' => 1, 'activated' => false, 'value' => 0]
```

Testing
-------

[](#testing)

Runs tests.

```
composer test
```

Runs all pre-commit checks.

```
composer all
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Bastien Philippe](https://github.com/bastien-phi)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance88

Actively maintained with recent releases

Popularity25

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 53.8% 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

Every ~89 days

Recently: every ~211 days

Total

15

Last Release

78d ago

Major Versions

0.7.0 → 1.0.02023-11-07

1.1.0 → 2.0.02024-12-30

PHP version history (3 changes)0.1.0PHP ^8.1

1.0.0PHP ^8.2

2.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/206cfbf866a463f7e7d1e86946d59b82f4191b9c89f9981fb03eeb264d77af79?d=identicon)[SoyHuCe](/maintainers/SoyHuCe)

---

Top Contributors

[![bastien-phi](https://avatars.githubusercontent.com/u/10199039?v=4)](https://github.com/bastien-phi "bastien-phi (43 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (15 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (10 commits)")[![qanvo](https://avatars.githubusercontent.com/u/15672030?v=4)](https://github.com/qanvo "qanvo (8 commits)")[![EdenMl](https://avatars.githubusercontent.com/u/70885551?v=4)](https://github.com/EdenMl "EdenMl (3 commits)")[![tnajah59](https://avatars.githubusercontent.com/u/91531929?v=4)](https://github.com/tnajah59 "tnajah59 (1 commits)")

---

Tags

hacktoberfestlaravelsoyhucearray-factory

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/soyhuce-array-factory/health.svg)

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

###  Alternatives

[barryvdh/laravel-ide-helper

Laravel IDE Helper, generates correct PHPDocs for all Facade classes, to improve auto-completion.

14.9k123.0M687](/packages/barryvdh-laravel-ide-helper)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[matt-daneshvar/laravel-survey

Create surveys inside your Laravel app

28770.3k](/packages/matt-daneshvar-laravel-survey)[frittenkeez/laravel-vouchers

Voucher system for Laravel 9+

5819.9k2](/packages/frittenkeez-laravel-vouchers)[glhd/special

1929.4k](/packages/glhd-special)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k2](/packages/bjuppa-laravel-blog)

PHPackages © 2026

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