Inseriti metodi sperimentali per i callback su set delle variabili Dao testato IN LETTURA
116 lines
3.4 KiB
PHP
116 lines
3.4 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 22/dic/2015
|
|
*/
|
|
|
|
class GenericDao{
|
|
private $mongoObj;
|
|
private $database;
|
|
|
|
private static $defaultReturnClass = "DaoSaveableDefaultImplementation";
|
|
|
|
/**
|
|
* La connection string viene passata interamente a MongoClient, richiede formato 'mongo://server:port'.
|
|
* host1 e database sono obbligatori
|
|
*
|
|
* @param string $connectionString
|
|
*/
|
|
public function __construct($dbName, $connectionString = null){
|
|
if (is_null($connectionString)){
|
|
$this->mongoObj = new MongoClient();
|
|
}
|
|
else {
|
|
if (!preg_match("~^mongo[:][/][/]\w+[:]\w+$~",$connectionString)){
|
|
throw new CoreException("Invalid connectionString parameter passed to 'GenericDao::__construct()' ('".print_r($connectionString,true)."')");
|
|
}
|
|
$this->mongoObj = new MongoClient($connectionString);
|
|
}
|
|
|
|
$this->database = $this->mongoObj->$dbName;
|
|
}
|
|
|
|
/**
|
|
* 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();
|
|
foreach ($cursor as $element){
|
|
$rval[] = $retClass::buildFromSaveableObj($collectionName, $element);
|
|
}
|
|
|
|
return $rval;
|
|
}
|
|
|
|
|
|
public function save(DaoSaveable $model){
|
|
$collectionName = $model->getCollectionName();
|
|
$collection = $this->database->$collectionName;
|
|
|
|
$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 );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
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;
|
|
}
|
|
}
|
|
|
|
?>
|