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. [Utility &amp; Helpers](/categories/utility)
4. /
5. anteris-dev/data-transfer-object-factory

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

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

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

v1.0.0(5y 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 1mo ago

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 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community10

Small or concentrated contributor base

Maturity64

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

1844d 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

[mbezhanov/faker-provider-collection

A collection of custom providers for the Faker library

2138.6M24](/packages/mbezhanov-faker-provider-collection)[verbb/formie

The most user-friendly forms plugin for Craft.

101372.9k40](/packages/verbb-formie)[blair2004/nexopos

The Free Modern Point Of Sale System build with Laravel, TailwindCSS and Vue.js.

1.2k2.3k](/packages/blair2004-nexopos)[pelmered/fake-car

Fake-Car is a Faker provider that generates fake car data for you.

1271.2M2](/packages/pelmered-fake-car)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

52664.9k12](/packages/solspace-craft-freeform)[jzonta/faker-restaurant

Food and Beverage names generate using fakerphp/faker

96317.2k](/packages/jzonta-faker-restaurant)

PHPackages © 2026

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