98 lines
1.9 KiB
PHP
98 lines
1.9 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 23/dic/2015
|
|
*/
|
|
|
|
abstract class DaoSaveableDefaultImplementation implements DaoSaveable {
|
|
private $saveableObject;
|
|
|
|
|
|
public function __construct($saveableObject = null){
|
|
if (is_null($saveableObject)){
|
|
$saveableObject = new stdClass();
|
|
}
|
|
$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::buildFromSaveableObj()
|
|
*/
|
|
public static function buildFromSaveableObj($saveableObject){
|
|
return new static($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;
|
|
}
|
|
}
|
|
|
|
?>
|