PHP code example of adlacruzes / php-factory

1. Go to this page and download the library: Download adlacruzes/php-factory library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

adlacruzes / php-factory example snippets


use Adlacruzes\Factory\Factory;
use Adlacruzes\Factory\Factories\ArrayFactory;
use Adlacruzes\Factory\Factories\FactoryInterface;

class ArraysFactory extends Factory
{
    protected static function setFactory(): FactoryInterface
    {
        return new ArrayFactory(
            [
                'one' => 'one',
                'two' => 'two',
                'three' => 'three',
                'four' => 'four',
            ]
        );
    }
}

[
    'one' => 'one',
    'two' => 'two',
    'three' => 'three',
    'four' => 'four',
]

ArraysFactory::create(
    [
        'four' => 'another number'
    ]
);

[
    'one' => 'one',
    'two' => 'two',
    'three' => 'three',
    'four' => 'another number',
]

class ValidClass
{
    private $id;

    private $name;

    private $isEnabled;

    private $createdAt;

    public function __construct($id, $name, $isEnabled, \DateTime $createdAt)
    {
        $this->id = $id;
        $this->name = $name;
        $this->isEnabled = $isEnabled;
        $this->createdAt = $createdAt;
    }


use Adlacruzes\Factory\Factories\ClassFactory;
use Adlacruzes\Factory\Factories\FactoryInterface;
use Adlacruzes\Factory\Factory;

class ValidClassFactory extends Factory
{
    protected static function setFactory(): FactoryInterface
    {
        return new ClassFactory(
            ValidClass::class,
            [
                'id' => 1,
                'name' => 'name',
                'isEnabled' => true,
                'createdAt' => new \DateTime(),
            ]
        );
    }
}

class ValidClass (4) {
  private $id =>
  int(1)
  private $name =>
  string(4) "name"
  private $isEnabled =>
  bool(true)
  private $createdAt =>
  class DateTime (3) {
    public $date =>
    string(26) "2019-01-01 00:00:00.000000"
    public $timezone_type =>
    int(3)
    public $timezone =>
    string(3) "UTC"
  }
}


ValidClassFactory::create(
    [
        'name' => 'other',
        'isEnabled' => false
    ]
);

class ValidClass (4) {
  private $id =>
  int(1)
  private $name =>
  string(4) "other"
  private $isEnabled =>
  bool(false)
  private $createdAt =>
  class DateTime (3) {
    public $date =>
    string(26) "2019-01-01 00:00:00.000000"
    public $timezone_type =>
    int(3)
    public $timezone =>
    string(3) "UTC"
  }
}


SampleFactory::create();

SampleFactory::create(
    [
        'field' => 'value'
    ]
);

SampleFactory::createArray();

SampleFactory::createArray(n);

SampleFactory::create(
    n,
    [
        'field' => 'value'
    ]
);

SampleFactory::createNullable();

SampleFactory::createNullable(
    [
        'field' => 'value'
    ]
);

class ArraysFactory extends Factory
{
    protected static function setFactory(): FactoryInterface
    {
        return new ArrayFactory(
            [
                'one' => 'one',
                'two' => 'two',
                'three' => 'three',
                'four' => 'four',
            ]
        );
    }
}

ArraysFactory::create(
    [
        'five' => 'five'
    ]
);

Factory: invalid fields: five
bash
composer