GithubHelp home page GithubHelp logo

object-creator's Introduction

Object Creator

The Object Creator can be used to instantiate and initialize objects from classes which represent a data structure. It is possible to add instantiators which know how to instantiate objects of a specific class. In this way it can be ensured that all objects are getting instantiated properly.

Usage

use Drieschel\ObjectCreator\ObjectCreator;
use Drieschel\ObjectCreator\Instantiator\DateTimeInstantiator;

// Example data from various source
$data = [
    'int' => 42,
    'float' => 0.7,
    'string' => 'yeeeehaaa',
    'bool' => true,
    'datetime' => 'Sat Jan 30 1988 12:22:22 GMT+0100'
];

// Example data structure class
class Entity {
    protected int $int;
    
    protected float $float;
    
    protected string $string;
    
    protected bool $bool;
    
    protected \DateTimeInterface $datetime;
    
    public function __construct(int $int, string $string) {
        $this->int = $int;
        $this->string = $string;
    }
    
    public function setFloat(float $float): self
    {
        $this->float = $float;
        return $this;
    }
        
    public function setBool(bool $bool): self
    {
        $this->bool = $bool;
        return $this;
    }
        
    public function setDatetime(\DateTimeInterface $datetime): self
    {
        $this->datetime = $datetime;
        return $this;
    }
}

// Instantiate the creator
$creator = new ObjectCreator();

// Add a class mapping for arguments from type DateTimeInterface.
// All arguments from type DateTimeInterface will be instantiated as DateTimeImmutable 
$creator->setClassMapping(\DateTimeInterface::class, \DateTimeImmutable::class);

// Register a DateTime instantiator which knows how to
// instantiate objects that implements the DateTimeInterface 
$creator->registerInstantiator(new DateTimeInstantiator());

// Instantiate an object
$entity = $creator->instantiate(Entity::class, $data);

// Initialize an object
$creator->initialize($entity, $data);

// Or instantiate and initialize an object together 
$entity = $creator->instantiateAndInitialize(Entity::class, $data);

object-creator's People

Contributors

drieschel avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.