Download the PHP package adlacruzes/php-factory without Composer
On this page you can find all versions of the php package adlacruzes/php-factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download adlacruzes/php-factory
More information about adlacruzes/php-factory
Files in adlacruzes/php-factory
Package php-factory
Short Description Factories to generate classes and arrays with default values
License MIT
Informations about the package php-factory
PHP Factory
PHP Factory allows generating classes and arrays with default values and reduce test bloatware.
The main purpose of this library is help with the creation of tests and fixtures.
Table of Contents
- Requirements
- Installation
- Factories
- Array factory
- Class factory
- Methods
- create
- createArray
- createNullable
- Exceptions
Requirements
PHP needs to be a minimum version of PHP 7.2.
Installation
The recommended way to install is through Composer.
Factories
Every factory has to extend Adlacruzes\Factory\Factory
to obtain the required methods.
Array factory
The array factory allows generating an array with predefined values.
You can call ArraysFactory::create()
and obtain the defined array:
If you want to change some information, you can override the values with a new partial array:
The returned array will be:
Class factory
The class factory allows generating a concrete class with default constructor parameters.
Create a class that extends from Factory.
You can call ValidClassFactory::create()
and obtain the defined class:
If you want to change some information, you can override default values with a new partial array:
The returned class will be:
Methods
There are three methods for every factory. They can be called with no arguments or can receive an array of values to override the defaults parameters.
create
Create returns an instance of the factory.
create()
Returns the predefined factory.
create(array)
Returns the predefined factory with the new specified values.
create array
CreateArray returns an array of instances of the factory. The number of instances can be specified as first argument. The default number is one.
createArray()
Returns an array of one predefined factory.
createArray(n)
Returns an array of n
predefined factories.
createArray(n, array)
Returns an array of n
predefined factories with the new specified values.
create nullable
CreateNullable returns an instance of the factory with all possibly null values.
createNullable()
Returns the predefined factory with nulls.
createNullable(array)
Returns the predefined factory with all possibly null values except the new specified ones.
Exceptions
An FactoryException
is thrown when the parameters to override the default values are not part of the original factory.
For example: