|
- <?php
-
- namespace Lc\SovBundle\Factory;
-
- use Doctrine\ORM\EntityManagerInterface;
-
- abstract class AbstractFactory
- {
- protected $em ;
-
- public function __construct(EntityManagerInterface $em)
- {
- $this->em = $em ;
- }
-
- public function create($params = array())
- {
- $entityClass = $this->em->getEntityName($this->getEntityClass());
-
- if ($entityClass && strlen($entityClass) > 0) {
- $entity = new $entityClass();
- $this->populate($entity, $params);
-
- return $entity;
- }
- else {
- throw new \ErrorException('La classe de l\'entité n\'est pas définie.');
- }
- }
-
- public function populate($entity, $params = array())
- {
- $entityArray = (array)$entity;
-
- foreach ($entityArray as $name => $value) {
- $nameArray = explode("\x00", $name);
- $name = $nameArray[2];
-
- if ($name != 'id' && isset($params[$name])) {
- $setter = 'set' . ucfirst($name);
-
- if (method_exists($entity, $setter)) {
- $entity->$setter($params[$name]);
- }
- }
- }
- }
- }
|