PHPackages                             xsolve-pl/model-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. [HTTP &amp; Networking](/categories/http)
4. /
5. xsolve-pl/model-factory

ActiveLibrary[HTTP &amp; Networking](/categories/http)

xsolve-pl/model-factory
=======================

This library provides a versatile skeleton for organizing model factories.

v1.0.3(4y ago)316.6k↓50%1MITPHPPHP ^5.6|^7.0|^8.0CI failing

Since Nov 24Pushed 4y ago2 watchersCompare

[ Source](https://github.com/xsolve-pl/xsolve-model-factory)[ Packagist](https://packagist.org/packages/xsolve-pl/model-factory)[ Docs](http://github.com/xsolve-pl/xsolve-model-factory)[ RSS](/packages/xsolve-pl-model-factory/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (7)Used By (1)

[![Build Status](https://camo.githubusercontent.com/07d14ef257caa7bc56f64123a61c4646fbe62b5d648a4836f6c37ad2bdad5f3a/68747470733a2f2f7472617669732d63692e6f72672f78736f6c76652d706c2f78736f6c76652d6d6f64656c2d666163746f72792e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/xsolve-pl/xsolve-model-factory)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/93cd22abbdcf4fdca0ff442c71f2b3f0e95ac9433bdd6ce0854fbc1fcd402726/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f78736f6c76652d706c2f78736f6c76652d6d6f64656c2d666163746f72792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/xsolve-pl/xsolve-model-factory/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/9c541d8aca3825e262b2e1c8f738268220c57b4421417eecabfa39aa9d452bb2/68747470733a2f2f706f7365722e707567782e6f72672f78736f6c76652d706c2f6d6f64656c2d666163746f72792f762f737461626c65)](https://packagist.org/packages/xsolve-pl/model-factory)[![Total Downloads](https://camo.githubusercontent.com/6ff77bf7ab843a78a6022d63987c8bec5c6babe5da621258a86b51ad6554e25d/68747470733a2f2f706f7365722e707567782e6f72672f78736f6c76652d706c2f6d6f64656c2d666163746f72792f646f776e6c6f616473)](https://packagist.org/packages/xsolve-pl/model-factory)[![Monthly Downloads](https://camo.githubusercontent.com/aee5a9079e7e9750f6cc24f8b938b844032a2610e6633e6d22c3854f649e25c3/68747470733a2f2f706f7365722e707567782e6f72672f78736f6c76652d706c2f6d6f64656c2d666163746f72792f642f6d6f6e74686c79)](https://packagist.org/packages/xsolve-pl/model-factory)[![License](https://camo.githubusercontent.com/4ac99e4107753b76d3648abec520c079fbda66226b9d0bdec66a787c672f98b8/68747470733a2f2f706f7365722e707567782e6f72672f78736f6c76652d706c2f6d6f64656c2d666163746f72792f6c6963656e7365)](https://packagist.org/packages/xsolve-pl/model-factory)

Table of contents
=================

[](#table-of-contents)

- [Introduction](#introduction)
- [License](#license)
- [Getting started](#getting-started)
- [Usage examples](#usage-examples)
    - [Implementing model factory](#implementing-model-factory)
    - [Using external dependencies in model](#using-external-dependencies-in-model)
    - [Grouping model factories into collections](#grouping-model-factories-into-collections)
    - [Creating nested models](#creating-nested-models)

Introduction
============

[](#introduction)

This library provides a versatile skeleton for organizing model factories.

It can be used to provide objects that will be later on passed to some serializer and returned via API, or some adapters or facades for your objects before they are handed over to some other libraries or bundles.

It aims to empower models so that they can easily get access to some services or create nested models lazily without requiring much work upfront.

If you want to use this library in a Symfony application you may be interested in using the dedicated bundle available at [xsolve-pl/model-factory-bundle](https://packagist.org/packages/xsolve-pl/model-factory-bundle).

License
=======

[](#license)

This library is under the MIT license. See the complete license in `LICENSE` file.

Getting started
===============

[](#getting-started)

Include this library in your project using Composer as follows (assuming it is installed globally):

```
$ composer require xsolve-pl/model-factory
```

For more information on Composer see its [Introduction](https://getcomposer.org/doc/00-intro.md).

Usage examples
==============

[](#usage-examples)

Implementing model factory
--------------------------

[](#implementing-model-factory)

This library defines a simple interface for model factory that provides information about whether it supports given object (i.e. is able to produce model object appropriate for given object) and instantiate such model object. It also include convenient methods allowing to operate on multiple objects at once. See `Xsolve\ModelFactory\ModelFactory\ModelFactoryInterface` for more details.

One you are free to implement this interface with your own model factory classes, a basic abstract class for model factory is included as well in `Xsolve\ModelFactory\ModelFactory\ModelFactory`. It includes all the necessary logic and leaves out only a public `supportsObject` method and a protected `instantiateModel` method to be implemented. Using it as a base class creating a new model factory class becomes very easy:

```
