Files
fdn2/private/lib/dao/DaoSaveableDefaultImplementation.php
T

109 lines
2.2 KiB
PHP

<?php
/*
Author: Riccardo Di Dato
Creation Date: 23/dic/2015
*/
class DaoSaveableDefaultImplementation implements DaoSaveable {
private $collectionName;
private $saveableObject;
public function __construct($collectionName, $saveableObject = null){
if (is_null($saveableObject)){
$saveableObject = new stdClass();
}
$this->collectionName = $collectionName;
$this->updateFromSaveableObj($saveableObject);
}
public function __set($attr,$value){
$this->saveableObject->$attr = $value;
}
public function &__get($attr){
$rval = &$this->saveableObject->$attr;
return $rval;
}
public function __unset($attr){
unset($this->saveableObject->$attr);
}
/**
* (non-PHPdoc)
* @see DaoSaveable::getSaveableObj()
*/
public function getSaveableObj(){
// return $this->saveableObject;
return $this->toArray($this->saveableObject);
}
private function toArray($input){
$rval = $input;
if (is_object($rval) ){
if (!($rval instanceof MongoId)){
$rval = (array)$input;
}
// $rval = (array)$input;
}
if (is_array($rval)) {
foreach ( $rval as $key=>$val){
$rval[$key] = $this->toArray($val);
}
}
return $rval;
}
/**
* (non-PHPdoc)
* @see DaoSaveable::getCollectionName()
*/
public function getCollectionName(){
return $this->collectionName;
}
/**
* (non-PHPdoc)
* @see DaoSaveable::buildFromSaveableObj()
*/
public static function buildFromSaveableObj($collectionName, $saveableObject){
return new static($collectionName, $saveableObject);
}
/**
* (non-PHPdoc)
* @see DaoSaveable::updateFromSaveableObj()
*/
public function updateFromSaveableObj($saveableObject){
$this->saveableObject = $this->toObject($saveableObject);
// $this->saveableObject = $saveableObject;
}
private function toObject($input){
$rval = $input;
if (is_array($input) && sizeof(array_diff_key($input,array_keys($input)))>0 ){
$rval = (object)$input;
}
if (is_array($rval)){
foreach ( $rval as $key=>$val){
$rval[$key] = $this->toObject($val);
}
}
else if (is_object($rval)) {
if (!($rval instanceof MongoId)){
foreach ( $rval as $key=>$val){
$rval->$key = $this->toObject($val);
}
}
}
return $rval;
}
}
?>