2017 © Pedro Peláez
 

library reflection-factory

The tiny Factory class which loads parameters and injects them into constructors by reflection.

image

nikolas-lada/reflection-factory

The tiny Factory class which loads parameters and injects them into constructors by reflection.

  • Wednesday, March 21, 2018
  • by nikolas-lada
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 50 % Grown

The README.md

Reflection factory - repeat your code less!

The factory class which loads parameters and injects them into constructors by reflection., (*1)

Installation

composer require nikolas-lada/reflection-factory

It requires PHP version 7.1+., (*2)

Usage

For \stdClass:, (*3)

use \NikolasLada\ReflectionFactory\ReflectionFactory;

$data = new \stdClass;

/**
 * no matter of property order
 */
$data->created = '2018-03-20 12:00:00';
$data->id = 1;
$data->updated = \null;

$beforeCreate = function() use ($data) {
  $data->created = new \DateTime($data->created);

  if (!\is_null($data->updated)) {
    $data->updated = new \DateTime($data->updated);
  }

  return $data;
};

$article = ReflectionFactory::createFromStdClass(
    \NikolasLada\ReflectionFactory\Tests\Domain\Article::class,
    $data,
    $beforeCreate
);

// or

$article = ReflectionFactory::create(
    \NikolasLada\ReflectionFactory\Tests\Domain\Article::class,
    $data,
    $beforeCreate
);

For array:, (*4)

use \NikolasLada\ReflectionFactory\ReflectionFactory;

$data = [];

/**
 * The createFromArray method depends on order items in array!
 */
$data['id'] = 1;
$data['created'] = '2018-03-20 12:00:00';
$data['updated'] = '2018-03-20 12:01:13';

$data['created'] = new \DateTime($data['created']);

if (!\is_null($data['updated'])) {
  $data['updated'] = new \DateTime($data['updated']);
}

$article = ReflectionFactory::createFromArray(
    \NikolasLada\ReflectionFactory\Tests\Domain\Article::class,
    $data
);

// or

$beforeCreate = function() use ($data) {
  $data['created'] = new \DateTime($data['created']);

  return $data;
};

$article = ReflectionFactory::create(
    \NikolasLada\ReflectionFactory\Tests\Domain\Article::class,
    $data,
    $beforeCreate
);

License

The Reflection factory library is open-sourced software licensed under: - GNU GPL 2.0, - GNU GPL 3.0, - GNU LGPL 3.0 and - MIT license., (*5)

The Versions

21/03 2018

dev-master

9999999-dev

The tiny Factory class which loads parameters and injects them into constructors by reflection.

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0 LGPL-3.0

The Requires

  • php >= 7.1

 

The Development Requires

reflection factory

21/03 2018

0.0.0

0.0.0.0

The tiny Factory class which loads parameters and injects them into constructors by reflection.

  Sources   Download

GPL-3.0 BSD-3-Clause GPL-2.0 LGPL-3.0

The Requires

  • php >= 7.1

 

The Development Requires

reflection factory