From 749b1c8dc8bbbc843b44f586acfc367c041582aa Mon Sep 17 00:00:00 2001 From: Riccardo Di Dato Date: Wed, 23 Dec 2015 19:45:13 +0100 Subject: [PATCH] Dao Completato ma non testato --- .../dao/DaoSaveableDefaultImplementation.php | 40 +++++- private/lib/dao/DaoSaveableInterface.php | 21 +++- private/lib/dao/GenericDao.php | 116 ++++++++++++------ 3 files changed, 137 insertions(+), 40 deletions(-) diff --git a/private/lib/dao/DaoSaveableDefaultImplementation.php b/private/lib/dao/DaoSaveableDefaultImplementation.php index 51673e9..0044180 100644 --- a/private/lib/dao/DaoSaveableDefaultImplementation.php +++ b/private/lib/dao/DaoSaveableDefaultImplementation.php @@ -5,12 +5,44 @@ Creation Date: 23/dic/2015 */ class DaoSaveableDefaultImplementation implements DaoSaveable { - public function getSaveableObj(){ - + private $collectionName; + private $saveableObject; + + public function __construct($collectionName, $saveableObject){ + $this->collectionName = $collectionName; + $this->saveableObject = $saveableObject; } - public function buildFromSaveableObj($obj){ - + /** + * (non-PHPdoc) + * @see DaoSaveable::getSaveableObj() + */ + public function getSaveableObj(){ + return $this->saveableObject; + } + + /** + * (non-PHPdoc) + * @see DaoSaveable::getCollectionName() + */ + public function getCollectionName(){ + return $this->collectionName; + } + + /** + * (non-PHPdoc) + * @see DaoSaveable::buildFromSaveableObj() + */ + public function buildFromSaveableObj($collectionName, $saveableObject){ + return new static($collectionName, $saveableObject); + } + + /** + * (non-PHPdoc) + * @see DaoSaveable::updateFromSaveableObj() + */ + public function updateFromSaveableObj($obj){ + $this->saveableObject = $obj; } } diff --git a/private/lib/dao/DaoSaveableInterface.php b/private/lib/dao/DaoSaveableInterface.php index 4f7226d..eea4531 100644 --- a/private/lib/dao/DaoSaveableInterface.php +++ b/private/lib/dao/DaoSaveableInterface.php @@ -5,9 +5,28 @@ Creation Date: 23/dic/2015 */ interface DaoSaveable { + /** + * @return stdClass L'oggetto da passare alla set di mongo + */ public function getSaveableObj(); + + /** + * @return string La collection name dove salvare il model + */ + public function getCollectionName(); - public function buildFromSaveableObj($obj); + /** + * Costruisce l'oggetto a partire dai dati raw di mongo + * @param string $collectionName Nome della collezione da cui proviene l'oggetto + * @param stdClass $obj dati raw di mongo + */ + public function buildFromSaveableObj($collectionName, $obj); + + /** + * Aggiorna i dati dell'oggetto partendo da un saveable object + * @param stdClass $obj dati raw di mongo + */ + public function updateFromSaveableObj($obj); } ?> \ No newline at end of file diff --git a/private/lib/dao/GenericDao.php b/private/lib/dao/GenericDao.php index 832f230..db17910 100644 --- a/private/lib/dao/GenericDao.php +++ b/private/lib/dao/GenericDao.php @@ -5,65 +5,111 @@ Creation Date: 22/dic/2015 */ class GenericDao{ - private $mongo; + private $mongoObj; + private $database; private static $defaultReturnClass = "DaoSaveableDefaultImplementation"; - public static function setDefaultReturnClass($retClass){ - self::$defaultReturnClass = $retClass; - } - /** - * La connection string è nel seguente formato. - * [username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database] + * La connection string viene passata interamente a MongoClient, richiede formato 'mongo://server:port'. * host1 e database sono obbligatori * * @param string $connectionString */ - public function __construct($name, $connectionString){ - if (!preg_match("~^\w+/\w+$~",$connectionString)){ - throw new CoreException("'GenericDao' constructed with invalid connection string '$connectionString': "); + public function __construct($dbName, $connectionString = null){ + if (is_null($connectionString)){ + $this->mongoObj = new MongoClient(); } - $mongo = self::generateMongoObject($connectionString); + else { + if (!preg_match("~^mongo[:][/][/]\w+[:]\w+$~",$connectionString)){ + throw new CoreException("Invalid connectionString parameter passed to 'GenericDao::__construct()' ('$connectionString')"); + } + $this->mongoObj = new MongoClient($connectionString); + } + + $this->database = $mongo->$dbName; } - public function query($collectionName, array $filter = array(), array $options = array(), $retClass = null){ + /** + * Ritorna un array di model + * @param string $collectionName nome della collezione + * @param array $filter array associativo della select formato mongo + * @param array $options (al momento supporta limit e sort) + * @param string $customReturnClass + * @throws CoreException se la CustomReturnClass non è valida + * @return DaoSaveable[] + */ + public function query($collectionName, array $filter = array(), array $options = array(), $customReturnClass = null){ + $retClass = self::$defaultReturnClass; + if (!is_null($customReturnClass)){ + if (!is_subclass_of($customReturnClass, "DaoSaveable", true)){ + throw new CoreException("Invalid return class passed to GenericDao::query ($customReturnClass)"); + } + $retClass = $customReturnClass; + } + + $collection = $this->database->$collectionName; + + if (!array_key_exists("deleted",$filter)){ + $filter["deleted"] = false; + } + $cursor = $collection->find($filter); + + if (array_key_exists("sort",$options)){ + $cursor->sort($options["sort"]); + } + if (array_key_exists("limit",$options)){ + $cursor->limit($options["limit"]); + } + $rval = array(); - $table = $this->getTable($name); - - $query = new MongoDB\Driver\Query($filter, $options); - $cursor = $this->mongo->executeQuery($collectionName,$query); - - foreach ($cursor as $ele){ - $rval[] = new self::$defaultReturnClass($ele); + foreach ($cursor as $element){ + $rval[] = $retClass::buildFromSaveableObj($collectionName, $element); } - return $ele; + + return $rval; } + public function save(DaoSaveable $model){ - $bulk = new MongoDB\Driver\BulkWrite(['ordered' => false]); - $writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000); + $collectionName = $model->getCollectionName(); + $collection = $this->database->$collectionName; - try { - $result = $manager->executeBulkWrite('db.collection', $bulk, $writeConcern); - } catch (MongoDB\Driver\Exception\BulkWriteException $e) { - + $saveObj = $model->getSaveableObj(); + + if ( !is_null($saveObj->_id) ){ + $collection->update( array("_id"=>$id), $saveObj ); + } + else { + $saveObj->_id = new MongoId(); + $collection->insert($saveObj); + $model->updateFromSaveableObj($saveObj); } } public function delete(DaoSaveable $model){ - + $collectionName = $model->getCollectionName(); + $collection = $this->database->$collectionName; + + $saveObj = $model->getSaveableObj(); + + if ( !is_null($saveObj->_id) ){ + $saveObj["deleted"] = true; + $collection->update( array("_id"=>$id), $saveObj ); + } } - + /** - * Costruisce un oggetto mongo da passare al costruttore. - * - * @param string $connectionString - * @return \MongoDB\Driver\Manager + * Imposta il tipo di oggetto restituito dalle query; la classe deve implementare l'interfaccia DaoSaveable. + * Default DaoSaveableDefaultImplementation + * @param string $retClass nome della classe + * @throws CoreException se la classe non implementa l'interfaccia DaoSaveable */ - private static function generateMongoObject($connectionString){ - return new MongoClient("mongodb://$connectionString"); -// return new MongoDB\Driver\Manager("mongodb://$connectionString"); + public static function setDefaultReturnClass($retClass){ + if (!is_subclass_of($retClass, "DaoSaveable", true)){ + throw new CoreException("Invalid return class passed to GenericDao::setDefaultReturnClass ($retClass)"); + } + self::$defaultReturnClass = $retClass; } }