2017 © Pedro PelĂĄez
 

library object-factory

Create plain PHP objects using Laravel model factories

image

stephanecoinon/object-factory

Create plain PHP objects using Laravel model factories

  • Monday, October 2, 2017
  • by stephanecoinon
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel Object Factory for Plain Old PHP Objects

Would you like to build plain PHP objects using Laravel 5 model factories?, (*1)

BuiltByFactory trait makes it a breeze..., (*2)

Step 1: Install this package

composer require stephanecoinon/object-factory, (*3)

Step 2: define your plain model using the trait:

<?php

// /app/Popo.php

namespace App;

use StephaneCoinon\ObjectFactory\BuiltByFactory;

/**
 * This is a plain old PHP class, we do not extend Eloquent.
 */
class Popo
{
    use BuiltByFactory;

    /**
     * Objects built by model factory are constructed from an array of
     * attributes.
     *
     * Here's an example of implementation.
     *
     * @param  array  $attributes
     * @return static
     */
    public function __construct($attributes = [])
    {
        foreach ($attributes as $key => $value) {
            $this->$key = $value;
        }
    }
}

Step 3: define the factory:

<?php

// /database/factories/PopoFactory.php

use Faker\Generator as Faker;

$factory->define(App\Popo::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
    ];
});

Step 4: build your objects!

// A single instance
$object = factory(App\Popo::class)->make();

// Or a collection
$objects = factory(App\Popo::class, 3)->make();

The Versions

02/10 2017

dev-master

9999999-dev

Create plain PHP objects using Laravel model factories

  Sources   Download

MIT

The Requires

 

The Development Requires

by Stéphane Coinon

02/10 2017

v0.0.1

0.0.1.0

Create plain PHP objects using Laravel model factories

  Sources   Download

MIT

The Requires

 

The Development Requires

by Stéphane Coinon