Download the PHP package zero-to-prod/factory without Composer

On this page you can find all versions of the php package zero-to-prod/factory. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package factory

Zerotoprod\Factory

Repo GitHub Actions Workflow Status GitHub Actions Workflow Status Packagist Downloads php Packagist Version License wakatime Hits-of-Code

Contents

Introduction

A Generic Factory Pattern for Creating Arrays.

Requirements

Installation

Install Zerotoprod\Factory via Composer:

This will add the package to your project's dependencies and create an autoloader entry for it.

Documentation Publishing

You can publish this README to your local documentation directory.

This can be useful for providing documentation for AI agents.

This can be done using the included script:

Automatic Documentation Publishing

You can automatically publish documentation by adding the following to your composer.json:

Usage

Basic Factory Pattern

The simplest factory implementation requires only the Factory trait and a definition() method:

Model-Based Factory Pattern

You can add a static factory() method to your model classes for more convenient usage:

Custom Factory Methods

Create custom methods in your factory to provide a fluent, expressive interface for setting specific values. These methods use the state() method internally to modify the factory context.

Advanced Factory Features

For more complex scenarios, you can add state manipulation with closures and other advanced patterns:

Define a factory in this way:

Callable State (Closures)

Use closures to create dynamic state based on the current context. This is powerful for creating values that depend on other values in the factory:

The closure receives the current context as its parameter, allowing you to create dynamic relationships between fields.

The set() Method

The set() method provides a flexible way to modify factory state without creating custom methods. It supports four different syntaxes to accommodate various use cases:

1. Key-Value Syntax

Set a single field by passing the key and value as separate parameters:

2. Array Syntax

Set multiple fields or use associative arrays:

3. Closure Syntax

Use closures for dynamic values based on current context:

4. Dot Notation Syntax

Access and modify nested array values:

Complete Example

Here's how all four syntaxes work together:

The merge() Method

The merge() method allows you to merge new values directly into the factory's current context. This is useful for bulk updates or overriding multiple values at once.

Important: merge() updates the context after set() methods have been evaluated, so closures will use the original values.

Use merge() when you want to:

The context() Method

Use the context() method to get the current context of the factory without creating the final result. This is useful for inspecting or debugging the factory state.

Context Priority

Context can be provided at different stages, with later contexts taking priority:

Priority order (highest to lowest):

  1. Context passed to make() - Final override at creation time
  2. Values from merge() method - Direct context updates
  3. Context from state() methods - Fluent method calls
  4. Context passed to factory() - Initial context setup
  5. Default values from definition() - Base factory defaults

Note: Closures in state() methods are evaluated when the method is called, so they use the context available at that time, before any subsequent merge() operations.

The make() Method

The make() method is the final step that creates your data structure. It can accept optional context that will override any previously set values:

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page if you want to contribute.

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit changes (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.

All versions of factory with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
zero-to-prod/arr Version ^1.1
zero-to-prod/package-helper Version ^1.1.3
ext-json Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zero-to-prod/factory contains the following files

Loading the files please wait ...