From f31af29bd45fedd344e126e59a764f127199be1d Mon Sep 17 00:00:00 2001 From: oscarotero Date: Thu, 3 Dec 2015 14:36:38 +0100 Subject: [PATCH] added more flexibility to configure the factories --- src/EntityFactory.php | 24 ++++++++++++++++++++++-- src/SimpleCrud.php | 27 ++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/EntityFactory.php b/src/EntityFactory.php index 5033d8d..2e45781 100644 --- a/src/EntityFactory.php +++ b/src/EntityFactory.php @@ -53,7 +53,7 @@ public function setNamespace($namespace) } /** - * Set the queryFactory instance used by the entities. + * Set the QueryFactory instance used by the entities. * * @param QueryFactory $queryFactory * @@ -67,7 +67,17 @@ public function setQueryFactory(QueryFactory $queryFactory) } /** - * Set the fieldFactory instance used by the entities. + * Returns the QueryFactory instance used by the entities. + * + * @return QueryFactory + */ + public function getQueryFactory() + { + return $this->queryFactory; + } + + /** + * Set the FieldFactory instance used by the entities. * * @param FieldFactory $fieldFactory * @@ -80,6 +90,16 @@ public function setFieldFactory(FieldFactory $fieldFactory) return $this; } + /** + * Returns the FieldFactory instance used by the entities. + * + * @return FieldFactory + */ + public function getFieldFactory(FieldFactory $fieldFactory) + { + return $this->fieldFactory; + } + /** * Set whether the entities are autocreated or not. * diff --git a/src/SimpleCrud.php b/src/SimpleCrud.php index 2305799..441bea5 100644 --- a/src/SimpleCrud.php +++ b/src/SimpleCrud.php @@ -21,15 +21,32 @@ class SimpleCrud */ public function __construct(PDO $connection, EntityFactory $entityFactory = null) { - if ($entityFactory === null) { - $entityFactory = (new EntityFactory())->setAutocreate(); - } + $this->connection = $connection; + $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $this->setEntityFactory($entityFactory ?: (new EntityFactory())->setAutocreate()); + } + /** + * Set the EntityFactory instance used to create all entities. + * + * @param EntityFactory $entityFactory + */ + public function setEntityFactory(EntityFactory $entityFactory) + { $entityFactory->setDb($this); + $this->entityFactory = $entityFactory; + } - $this->connection = $connection; - $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + /** + * Returns the EntityFactory instance used. + * + * @return EntityFactory + */ + public function getEntityFactory() + { + return $this->entityFactory; } /**