<?php
namespace Project\Model\Entity;
/**
* Class EntityFactory
*
* A factory to DI around to build entities
*
* @package Project\Model\Entity
*/
class EntityFactory
{
/**
* Create your entity
*
* @param string $entityName This should be the concrete name of the entity, like 'User' or 'Test'
* @param array $args Single dimensional array of required entity arguments
*
* @return object The entity - you will have to typehint the object in your code for IDE auto-completion
*/
public function buildEntity($entityName, array $args = [])
{
if (!class_exists($className = sprintf('%s\%s', __NAMESPACE__, $entityName)))
{
throw new \InvalidArgumentException(sprintf(
'Invalid entity name provided, got: %s', $entityName
));
}
$reflector = new \ReflectionClass($className);
return $reflector->newInstanceArgs($args);
}
}