182 lines
4.0 KiB
PHP
182 lines
4.0 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 23/dic/2015
|
|
*/
|
|
|
|
abstract class DaoSaveableDefaultImplementation implements DaoSaveable, Serializable {
|
|
protected $saveableObject;
|
|
|
|
|
|
public function __construct($saveableObject = null){
|
|
if (is_null($saveableObject)){
|
|
$saveableObject = new stdClass();
|
|
}
|
|
else {
|
|
$this->updateFromSaveableObj($saveableObject);
|
|
}
|
|
}
|
|
|
|
public function __set($attr,$value){
|
|
if (strcmp($attr,"id")==0 || strcmp($attr,"_id")==0){
|
|
if (strcmp($attr,"id")==0){
|
|
$attr = "_id";
|
|
}
|
|
if (!($value instanceof MongoDB\BSON\ObjectID)){
|
|
$value = new MongoDB\BSON\ObjectID($value);
|
|
}
|
|
}
|
|
$this->saveableObject->$attr = $value;
|
|
}
|
|
|
|
public function &__get($attr){
|
|
$rval = null;
|
|
if (strcmp($attr,"id")==0){
|
|
$attr = '_id';
|
|
}
|
|
if ($this->hasProperty($attr)){
|
|
$rval = &$this->saveableObject->$attr;
|
|
}
|
|
if (strcmp($attr,"_id")==0){
|
|
$rval = strval($rval);
|
|
}
|
|
|
|
return $rval;
|
|
}
|
|
|
|
public function __unset($attr){
|
|
unset($this->saveableObject->$attr);
|
|
}
|
|
|
|
public function hasProperty($attr){
|
|
if (strcmp($attr,"id")==0){
|
|
$attr = "_id";
|
|
}
|
|
return property_exists($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 instanceof MongoDate) ){
|
|
if (!($rval instanceof MongoDB\BSON\ObjectID) && !($rval instanceof MongoDB\BSON\UTCDateTime) ){
|
|
$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);
|
|
if (!property_exists($this->saveableObject, "deleted")){
|
|
$this->saveableObject->deleted = false;
|
|
}
|
|
// $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) && !($rval instanceof MongoDate)){
|
|
if (!($rval instanceof MongoDB\BSON\ObjectID) && !($rval instanceof MongoDB\BSON\UTCDateTime) ){
|
|
foreach ( $rval as $key=>$val){
|
|
$rval->$key = $this->toObject($val);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $rval;
|
|
}
|
|
|
|
private function toArrayAndScalars($obj){
|
|
$rval = $obj;
|
|
|
|
if ( ( $rval instanceof MongoDB\BSON\ObjectID ) || ( $rval instanceof MongoDB\BSON\UTCDateTime ) ){
|
|
$rval = [
|
|
'__fetch' => get_class($rval),
|
|
'value' => strval($rval)
|
|
];
|
|
}
|
|
else if ( is_object( $rval ) ){
|
|
$rval = (array)($rval);
|
|
}
|
|
|
|
if ( is_array($rval) ) {
|
|
foreach ( $rval as $key=>$val ) {
|
|
if (!is_scalar($val)){
|
|
$rval[$key] = $this->toArrayAndScalars($val);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $rval;
|
|
}
|
|
|
|
private function fromArrayAndScalars($obj){
|
|
$rval = $obj;
|
|
|
|
if (is_array($obj)){
|
|
if (array_key_exists("__fetch", $rval) && array_key_exists("value", $rval) ){
|
|
$class = $rval["__fetch"];
|
|
$val = $rval["value"];
|
|
$rval = new $class($val);
|
|
}
|
|
else {
|
|
foreach ($rval as $key=>$value){
|
|
$rval[$key] = $this->fromArrayAndScalars($value);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $rval;
|
|
}
|
|
|
|
public function serialize(){
|
|
return json_encode( $this->toArrayAndScalars( $this->saveableObject ) );
|
|
}
|
|
|
|
public function unserialize($serialized){
|
|
$this->updateFromSaveableObj( $this->fromArrayAndScalars( json_decode( $serialized, true ) ) );
|
|
}
|
|
|
|
}
|
|
|
|
?>
|