PHPackages                             anteris-dev/data-transfer-object-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. anteris-dev/data-transfer-object-factory

ActiveLibrary

anteris-dev/data-transfer-object-factory
========================================

Gives an easy-to-use API for mocking Data Transfer Objects.

v1.0.0(4y ago)4087.4k—0%3[1 issues](https://github.com/Anteris-Dev/data-transfer-object-factory/issues)[2 PRs](https://github.com/Anteris-Dev/data-transfer-object-factory/pulls)MITPHPPHP ^8.0

Since Sep 5Pushed 3y ago2 watchersCompare

[ Source](https://github.com/Anteris-Dev/data-transfer-object-factory)[ Packagist](https://packagist.org/packages/anteris-dev/data-transfer-object-factory)[ RSS](/packages/anteris-dev-data-transfer-object-factory/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (6)Versions (7)Used By (0)

Making it easy to mock your Data Transfer Objects
=================================================

[](#making-it-easy-to-mock-your-data-transfer-objects)

[![Tests](https://github.com/Anteris-Dev/data-transfer-object-factory/workflows/Tests/badge.svg)](https://github.com/Anteris-Dev/data-transfer-object-factory/actions?query=workflow%3ATests)[![Style](https://github.com/Anteris-Dev/data-transfer-object-factory/workflows/Style/badge.svg)](https://github.com/Anteris-Dev/data-transfer-object-factory/actions?query=workflow%3AStyle)

This package provides a fluent factory for Data Transfer Objects. Natively supported are POPOs (Plain Old PHP Objects), Getter / Setter Objects, and [Spatie's Data Transfer Objects](https://github.com/spatie/data-transfer-object). If there is a DTO not represented, you can easily add an adapter to support it.

One thing that makes this package so powerful is that it integrates with Faker in an attempt to intelligently generate the correct content for your data based on its name. For example, a DTO with the property "$firstName" will get a Faker first name.

By default only built-in PHP types are supported, but this factory can easily be extended to support other types (e.g., Carbon) as well.

To Install
==========

[](#to-install)

Run `composer require anteris-dev/data-transfer-object-factory`.

Getting Started
===============

[](#getting-started)

If you are simply using PHP default types in your DTOs, you can get started right away. Just pass your DTO FQDN to the static `new()` method. You can then use any of the following helper methods.

- `count()` - *Allows you to specify how many DTOs to be generated. They will be returned in an array.*
- `make()` - *Called when you are ready to generate the DTO. Returns the generated DTO\[s\].*
- `random()` - *Generates a random number of DTOs*
- `sequence()` - *Alternates a specific state. (See below)*
- `state()` - *Manually sets properties based on the array of values passed.*

Examples of these methods can be found below.

```
use Anteris\DataTransferObjectFactory\Factory;

// Creates one DTO
Factory::new(PersonData::class)->make();

// Creates two DTOs in an array
Factory::new(PersonData::class)->count(2)->make();

// Sets the first name of every person to "Jim"
Factory::new(PersonData::class)
    ->random()
    ->state([
        'firstName' => 'Jim',
    ])
    ->make();

// Also sets the first name of every person to "Jim"
Factory::dto(PersonData::class)
    ->random()
    ->make([
        'firstName' => 'Jim',
    ]);

// Alternates the names of each person between "Jim" and "Susie"
Factory::dto(PersonData::class)
    ->random()
    ->sequence(
        [ 'firstName' => 'Jim' ],
        [ 'firstName' => 'Susie' ]
    )
    ->make();
```

Extending
---------

[](#extending)

### Adapters

[](#adapters)

Adapters instruct the factory on how to retrieve properties for a specific type of class. Adapters must implement the `Anteris\DataTransferObjectFactory\Adapter\AdapterInterface` which requires the following methods.

- `handles(ReflectionClass $class)` - *Returns a bool if the adapter can handle the referenced reflection class.*
- `getProperties(ReflectionClass $class)` - *Returns a collection of properties found on the referenced reflection class.*
- `createClass(ReflectionClass $class, PropertyCollection $collection)` - *Creates and returns and instance of the reflection class using the properties passed.*

To register an adapter on the factory, call its static `registerAdapter()` method. For example:

```
use Anteris\DataTransferObjectFactory\Factory;

Factory::registerAdapter(new MyCustomAdapter);
```

For more information check out the `Adapter` directory in the source code.

### Property Types

[](#property-types)

It used to be that you had to extend the factory class to utilize custom types. You can now do so through the static `registerProvider()` method on the `PropertyFactory` class. This method takes two arguments. The first should be the FQDN of the class you are providing (e.g. `Carbon\Carbon`) OR the built-in type (e.g. `string`). The second should be a callback that returns the generated value. This callback is passed two properties when called to assist in generating the value. The first is an instance of `Anteris\FakerMap\FakerMap` which can be used to help generate fake data. The second is the name of the property being generated or null if not provided.

For example, to support Carbon:

```
use Anteris\DataTransferObjectFactory\PropertyFactory;

use Anteris\FakerMap\FakerMap;

PropertyFactory::registerProvider('Carbon\Carbon', fn(FakerMap $fakerMap) => Carbon::parse(
    $fakerMap->closest('dateTime')->fake()
));
```

###  Health Score

38

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community10

Small or concentrated contributor base

Maturity63

Established project with proven stability

 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

Every ~76 days

Total

4

Last Release

1794d ago

Major Versions

v0.1.2 → v1.0.02021-04-22

PHP version history (2 changes)v0.1.0PHP ^7.4

v1.0.0PHP ^8.0

### Community

Maintainers

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

---

Top Contributors

[![aidan-casey](https://avatars.githubusercontent.com/u/6686277?v=4)](https://github.com/aidan-casey "aidan-casey (5 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/anteris-dev-data-transfer-object-factory/health.svg)

```
[![Health](https://phpackages.com/badges/anteris-dev-data-transfer-object-factory/health.svg)](https://phpackages.com/packages/anteris-dev-data-transfer-object-factory)
```

###  Alternatives

[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.4k5.5M596](/packages/sylius-sylius)[nelmio/alice

Expressive fixtures generator

2.5k42.8M126](/packages/nelmio-alice)[orchestra/testbench

Laravel Testing Helper for Packages Development

2.2k38.0M27.1k](/packages/orchestra-testbench)[knuckleswtf/scribe

Generate API documentation for humans from your Laravel codebase.✍

2.3k11.6M36](/packages/knuckleswtf-scribe)[jgrossi/corcel

Use WordPress backend with Laravel or any PHP framework

4.8k1.8M26](/packages/jgrossi-corcel)[zenstruck/foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.

78011.3M79](/packages/zenstruck-foundry)

PHPackages © 2026

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