PHP code example of nikolas-lada / reflection-factory

1. Go to this page and download the library: Download nikolas-lada/reflection-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/ */

    

nikolas-lada / reflection-factory example snippets


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
);

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
);