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

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

skollro/factory
===============

Higher order factories for PHP.

v1.0.1(8y ago)4201MITPHP

Since Apr 5Pushed 8y ago2 watchersCompare

[ Source](https://github.com/skollro/factory)[ Packagist](https://packagist.org/packages/skollro/factory)[ RSS](/packages/skollro-factory/feed)WikiDiscussions master Synced 2w ago

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

Higher order factories
======================

[](#higher-order-factories)

[![Latest Version](https://camo.githubusercontent.com/3185de4c516431ef4ce35722634b1ed62288d326ab55ccea206e4a85030bfcd9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f736b6f6c6c726f2f666163746f72792e7376673f7374796c653d666c61742d737175617265)](https://github.com/skollro/factory/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/561b773e50bd8afa161492ebc9902710186ba2df3b3cb9b4a2c8dda577cf80b4/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736b6f6c6c726f2f666163746f72792f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/skollro/factory)[![StyleCI](https://camo.githubusercontent.com/e8d737a9c464bbba08fc510157d5eb1d71f0e016faa7857eadde23acaba3b1b2/68747470733a2f2f7374796c6563692e696f2f7265706f732f3132373839313433362f736869656c64)](https://styleci.io/repos/127891436)

This package provides a declarative higher order syntax for implementing the factory pattern.

```
use function Skollro\Factory\make;

class VehicleFactory
{
    public static function make($type)
    {
        return make($type)
            ->resolve('car', Car::class)
            ->resolve('bike', Bike::class)
            ->resolve([
                'truck' => Truck::class,
                'bus' => Bus::class,
            ])
            ->otherwiseThrow(InvalidArgumentException::class);
    }
}

$car = VehicleFactory::make('car'); // returns instance of Car
$truck = VehicleFactory::make('truck'); // returns instance of Truck
$motorcycle = VehicleFactory::make('motorcycle') // throws InvalidArgumentException
```

Install
-------

[](#install)

You can install this package via composer:

```
composer require skollro/factory
```

Usage
-----

[](#usage)

Every `make` consists out of one or multiple `resolve` and one `otherwise` to provide values for each path.

#### `make($type): Factory`

[](#maketype-factory)

This package provides a helper function `make`.

```
use Skollro\Factory\Factory;
use function Skollro\Factory\make;

$factory = make($type);
$factory = Factory::make($type);
```

#### `resolve($type, $resolvable = null): Factory`

[](#resolvetype-resolvable--null-factory)

`$type` is the identifier of an instance. `$type` also accepts an associative array which maps identifiers to class names. `$resolvable` can be some class name or a callable that returns an instance.

```
$object = make('bike')
    ->resolve('car', Car::class)
    ->resolve('bike', function () {
        return new Bike;
    })
    ->resolve('bike', MountainBike::class) // this is not the first match and thus not the result
    ->resolve([
        'truck' => Truck::class,
        'bus' => Bus::class,
    ])
    ->otherwiseThrow(InvalidArgumentException::class);

// $object is an instance of Bike
```

#### `otherwise($resolvable)`

[](#otherwiseresolvable)

`$resolvable` is some class name or a callable that returns an instance. Supplies the default instance if no `resolve` has matched before.

```
$object = make('motorcycle')
    ->resolve([
        'car' => Car::class,
        'bike' => Bike::class,
    ])
    ->otherwise(Vehicle::class);

// $object is an instance of Vehicle

$object = make('motorcycle')
    ->resolve([
        'car' => Car::class,
        'bike' => Bike::class,
    ])
    ->otherwise(function () {
        return new Vehicle;
    });

// $object is an instance of Vehicle
```

#### `otherwiseThrow($resolvable)`

[](#otherwisethrowresolvable)

`$resolvable` is some exception class name or a callable that returns an exception. Throws an exception if no `resolve` has matched before.

```
$object = make('motorcycle')
    ->resolve([
        'car' => Car::class,
        'bike' => Bike::class,
    ])
    ->otherwiseThrow(InvalidArgumentException::class);

// throws an InvalidArgumentException
```

#### `Factory::resolveUsing($resolver)`

[](#factoryresolveusingresolver)

`resolveUsing` is a static method of `Factory`. You can use a custom resolver to resolve an instance from a class name, e.g. a dependency injection container. If no resolver is set, an instance of the requested class is created with `new`.

##### Integration with Laravel

[](#integration-with-laravel)

```
// in your AppServiceProvider
Factory::resolveUsing(function ($className) {
    return app($className);
});

// in your code
$object = make('car')
    ->resolve('car', Car::class)
    ->otherwiseThrow(InvalidArgumentException::class);

// $object is an instance of Car, resolved by Laravel container
```

License
-------

[](#license)

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

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community9

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 ~0 days

Total

2

Last Release

3008d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8849605?v=4)[Simon Kollross](/maintainers/skollro)[@skollro](https://github.com/skollro)

---

Top Contributors

[![skollro](https://avatars.githubusercontent.com/u/8849605?v=4)](https://github.com/skollro "skollro (6 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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