From b8a9e5e599bf84ea46f86766adcd9f12fb4cf041 Mon Sep 17 00:00:00 2001 From: Riccardo Di Dato Date: Fri, 8 Jan 2016 22:21:17 +0100 Subject: [PATCH 1/2] Correzioni al dao (save Tested) Modifiche ai model Rimossa la constrained interface in quanto inefficace e troppo pesante --- private/lib/ConstrainedObject.php | 137 ----------- private/lib/ConstraintException.php | 11 - private/lib/constrained/Constrained.php | 191 +++++++++++++++ .../constrained/ConstrainedArrayObject.php | 67 ++++++ .../ConstrainedBehaviour.php} | 2 +- .../lib/constrained/ConstrainedFactory.php | 37 +++ .../lib/constrained/ConstrainedInterface.php | 32 +++ .../constrained/ConstrainedMemoryObject.php | 11 + private/lib/constrained/ConstrainedObject.php | 220 ++++++++++++++++++ .../lib/constrained/ConstraintException.php | 32 +++ private/lib/constrained/lib.inclusion.php | 18 ++ .../dao/DaoSaveableDefaultImplementation.php | 70 ++++-- private/lib/dao/GenericDao.php | 8 +- private/lib/dao/models/NotiziaModel.php | 21 +- private/lib/lib.inclusions.php | 5 +- public/index.php | 18 +- 16 files changed, 682 insertions(+), 198 deletions(-) delete mode 100644 private/lib/ConstrainedObject.php delete mode 100644 private/lib/ConstraintException.php create mode 100644 private/lib/constrained/Constrained.php create mode 100644 private/lib/constrained/ConstrainedArrayObject.php rename private/lib/{ConstrainedObjectBehaviourCallable.php => constrained/ConstrainedBehaviour.php} (97%) create mode 100644 private/lib/constrained/ConstrainedFactory.php create mode 100644 private/lib/constrained/ConstrainedInterface.php create mode 100644 private/lib/constrained/ConstrainedMemoryObject.php create mode 100644 private/lib/constrained/ConstrainedObject.php create mode 100644 private/lib/constrained/ConstraintException.php create mode 100644 private/lib/constrained/lib.inclusion.php diff --git a/private/lib/ConstrainedObject.php b/private/lib/ConstrainedObject.php deleted file mode 100644 index c99cec6..0000000 --- a/private/lib/ConstrainedObject.php +++ /dev/null @@ -1,137 +0,0 @@ -original = $original; - } - - public function __set($key, $value){ - if (array_key_exists($key, $this->constraints)){ - $constraint = $this->constraints[$key]->set; - switch ($constraint->behaviour){ - case self::SET_BEHAVIOUR_NORMAL: - $this->original->$key = $value; - break; - case self::SET_BEHAVIOUR_THROW: - throw new ConstraintException("Constraint violation: impossible to set field '$key'"); - break; - case self::SET_BEHAVIOUR_IGNORE: - default: - break; - } - if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ - $this->original = $constraint->callbackClass->executeSetCallback($this->original, $key, $value); - } - } - else { - $this->original->$key = $value; - } - } - - public function __get($key){ - $rval = $this->original->$key; - if (array_key_exists($key, $this->constraints)){ - $constraint = $this->constraints[$key]->get; - switch ($constraint->behaviour){ - case self::GET_BEHAVIOUR_EMPTY: - $rval = null; - break; - case self::GET_BEHAVIOUR_THROW: - throw new ConstraintException("Constraint violation: impossible to get field '$key'"); - break; - case self::GET_BEHAVIOUR_NORMAL: - default: - break; - } - if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ - $rval = $constraint->callbackClass->executeGetCallback($this->original, $key); - } - } - return $rval; - } - - public function __unset($key){ - if (array_key_exists($key, $this->constraints)){ - $constraint = $this->constraints[$key]->unset; - switch ($constraint->behaviour){ - case self::UNSET_BEHAVIOUR_NORMAL: - unset($this->original->$key); - break; - case self::UNSET_BEHAVIOUR_THROW: - throw new ConstraintException("Constraint violation: impossible to unset field '$key'"); - break; - case self::UNSET_BEHAVIOUR_IGNORE: - default: - break; - } - if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ - $this->original = $constraint->callbackClass->executeUnsetCallback($this->original, $key); - } - } - else { - unset($this->original->$key); - } - } - - public function createSetBehaviour($key, $behaviour, ConstraintObjectBehaviourCallable $callbackClass = null){ - $this->constraints[$key]->set = new stdClass(); - if ($behaviour < self::SET_BEHAVIOUR_NORMAL){ - $behaviour = self::SET_BEHAVIOUR_NORMAL; - } - else if ($behaviour > self::SET_BEHAVIOUR_THROW){ - $behaviour = self::SET_BEHAVIOUR_THROW; - } - $this->constraints[$key]->set->behaviour = $behaviour; - $this->constraints[$key]->set->callbackClass = $callbackClass; - } - - public function createGetBehaviour($key, $behaviour, ConstraintObjectBehaviourCallable $callbackClass = null){ - $this->constraints[$key]->get = new stdClass(); - if ($behaviour < self::GET_BEHAVIOUR_NORMAL){ - $behaviour = self::GET_BEHAVIOUR_NORMAL; - } - else if ($behaviour > self::GET_BEHAVIOUR_THROW){ - $behaviour = self::GET_BEHAVIOUR_THROW; - } - $this->constraints[$key]->get->behaviour = $behaviour; - $this->constraints[$key]->get->callbackClass = $callbackClass; - } - - public function createUnsetBehaviour($key, $behaviour, ConstraintObjectBehaviourCallable $callbackClass = null){ - $this->constraints[$key]->unset = new stdClass(); - if ($behaviour < self::UNSET_BEHAVIOUR_NORMAL){ - $behaviour = self::UNSET_BEHAVIOUR_NORMAL; - } - else if ($behaviour > self::UNSET_BEHAVIOUR_THROW){ - $behaviour = self::UNSET_BEHAVIOUR_THROW; - } - $this->constraints[$key]->unset->behaviour = $behaviour; - $this->constraints[$key]->unset->callbackClass = $callbackClass; - } - - public function get(){ - return $this->original; - } -} - -?> \ No newline at end of file diff --git a/private/lib/ConstraintException.php b/private/lib/ConstraintException.php deleted file mode 100644 index 61f3fd8..0000000 --- a/private/lib/ConstraintException.php +++ /dev/null @@ -1,11 +0,0 @@ - \ No newline at end of file diff --git a/private/lib/constrained/Constrained.php b/private/lib/constrained/Constrained.php new file mode 100644 index 0000000..a8c69eb --- /dev/null +++ b/private/lib/constrained/Constrained.php @@ -0,0 +1,191 @@ + array("label"=>"Normal"), + self::SET_BEHAVIOUR_IGNORE => array("label"=>"Ignore"), + self::SET_BEHAVIOUR_THROW => array("label"=>"Throw Exception") + ); + + public static $UNSET_BEHAVIOURS = array( + self::UNSET_BEHAVIOUR_NORMAL => array("label"=>"Normal"), + self::UNSET_BEHAVIOUR_IGNORE => array("label"=>"Ignore"), + self::UNSET_BEHAVIOUR_THROW => array("label"=>"Throw Exception") + ); + + public static $GET_BEHAVIOURS = array( + self::GET_BEHAVIOUR_NORMAL => array("label"=>"Normal"), + self::GET_BEHAVIOUR_EMPTY => array("label"=>"Empty"), + self::GET_BEHAVIOUR_THROW => array("label"=>"Throw Exception") + ); + + + private $constraints = array(); + + public function __construct(){ + $this->createSetBehaviour("*", self::SET_BEHAVIOUR_NORMAL); + $this->createGetBehaviour("*", self::GET_BEHAVIOUR_NORMAL); + $this->createUnsetBehaviour("*", self::UNSET_BEHAVIOUR_NORMAL); + } + + public function createSetBehaviour($key, $behaviour, ConstrainedBehaviour $callbackClass = null){ +// if (strpos($key, ".")!== false){ +// $matches = array(); +// if (!preg_match("/^([^.]+)\.(.+)$/", $key, $matches)){ +// throw new CoreException("Invalid key format passed to ".get_class($this)."::createSetBehaviour ($key)"); +// } +// $myKey = $matches[1]; +// $childKey = $matches[2]; + +// $child = $this->getInternal($myKey); +// if (is_null($child)){ +// $this->setInternal($myKey, new ConstrainedMemoryObject()); +// } +// if ($child instanceof Constrained){ +// $child->createSetBehaviour($childKey, $behaviour, $callbackClass); +// } +// else { +// throw new CoreException("Invalid key passed to ".get_class($this)."::createSetBehaviour. Setting a child constraint on a non constrained element."); +// } +// } +// else { + if (!array_key_exists($behaviour, self::$SET_BEHAVIOURS)){ + throw new CoreException("Invalid behaviour parameter passed to ".get_class($this)."::createSetBehaviour ($behaviour)"); + } + + if (!array_key_exists($key,$this->constraints)){ + $this->constraints[$key] = new stdClass(); + } + $this->constraints[$key]->set = new stdClass(); + $this->constraints[$key]->set->behaviour = $behaviour; + $this->constraints[$key]->set->callbackClass = $callbackClass; +// } + } + + public function createGetBehaviour($key, $behaviour, ConstrainedBehaviour $callbackClass = null){ + if (!array_key_exists($behaviour, self::$GET_BEHAVIOURS)){ + throw new CoreException("Invalid behaviour parameter passed to ".get_class($this)."::createGetBehaviour ($behaviour)"); + } + if (!array_key_exists($key,$this->constraints)){ + $this->constraints[$key] = new stdClass(); + } + $this->constraints[$key]->get = new stdClass(); + $this->constraints[$key]->get->behaviour = $behaviour; + $this->constraints[$key]->get->callbackClass = $callbackClass; + } + + public function createUnsetBehaviour($key, $behaviour, ConstrainedBehaviour $callbackClass = null){ + if (!array_key_exists($behaviour, self::$UNSET_BEHAVIOURS)){ + throw new CoreException("Invalid behaviour parameter passed to ".get_class($this)."::createUnsetBehaviour ($behaviour)"); + } + if (!array_key_exists($key,$this->constraints)){ + $this->constraints[$key] = new stdClass(); + } + $this->constraints[$key]->unset = new stdClass(); + $this->constraints[$key]->unset->behaviour = $behaviour; + $this->constraints[$key]->unset->callbackClass = $callbackClass; + } + + public function setConstrained($key, $value){ + try { + $constraint = $this->constraints["*"]->set; + if (array_key_exists($key, $this->constraints) && property_exists($this->constraints[$key], "set")){ + $constraint = $this->constraints[$key]->set; + } + $constraint = $this->constraints[$key]->set; + switch ($constraint->behaviour){ + case self::SET_BEHAVIOUR_NORMAL: + $this->setInternal($key, $value); + break; + case self::SET_BEHAVIOUR_THROW: + throw new ConstraintException("set",$key,$value); + break; + case self::SET_BEHAVIOUR_IGNORE: + default: + break; + } + if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ + $this->setBase($constraint->callbackClass->executeSetCallback($this->getBase(), $key, $value)); + } + } + catch (ConstraintException $cex){ + throw $this->addPathToChildException($cex); + } + } + + public function getConstrained($key){ + try { + $rval = null; + if (array_key_exists($key, $this->constraints) && property_exists($this->constraints[$key], "get")){ + $constraint = $this->constraints[$key]->get; + switch ($constraint->behaviour){ + case self::GET_BEHAVIOUR_THROW: + throw new ConstraintException("get",$key); + break; + case self::GET_BEHAVIOUR_NORMAL: + $rval = $this->getInternal($key); + break; + case self::GET_BEHAVIOUR_EMPTY: + default: + break; + } + if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ + $rval = $constraint->callbackClass->executeGetCallback($this->getBase(), $key); + } + } + else { + $rval = $this->getInternal($key); + } + return $rval; + } + catch (ConstraintException $cex){ + throw $this->addPathToChildException($cex); + } + } + + public function unsetConstrained($key){ + try { + if (array_key_exists($key, $this->constraints) && property_exists($this->constraints[$key], "unset")){ + $constraint = $this->constraints[$key]->unset; + switch ($constraint->behaviour){ + case self::UNSET_BEHAVIOUR_NORMAL: + $this->unsetInternal($key); + break; + case self::UNSET_BEHAVIOUR_THROW: + throw new ConstraintException("unset",$key); + break; + case self::UNSET_BEHAVIOUR_IGNORE: + default: + break; + } + if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ + $this->setBase($constraint->callbackClass->executeUnsetCallback($this->getBase(), $key)); + } + } + else { + $this->unsetInternal($key); + } + } + catch (ConstraintException $cex){ + throw $this->addPathToChildException($cex); + } + } + + protected function replicateConstraints(Constrained $oth){ + $this->constraints = clone $oth->constraints; + } + + protected abstract function addPathToChildException($myKey, ConstraintException $ex); + protected abstract function setInternal($key,$value); + protected abstract function getInternal($key); + protected abstract function unsetInternal($key); + + protected abstract function setBase($base); + +} + +?> \ No newline at end of file diff --git a/private/lib/constrained/ConstrainedArrayObject.php b/private/lib/constrained/ConstrainedArrayObject.php new file mode 100644 index 0000000..8e3b29b --- /dev/null +++ b/private/lib/constrained/ConstrainedArrayObject.php @@ -0,0 +1,67 @@ +setBase($base); + } + + protected function setBase($base){ + foreach ($base as $key=>$value){ + $this->base[$key] = ConstrainedFactory::build($value); + } + } + + public function getBase(){ + return $this->base; + } + + protected function addPathToChildException($myKey, ConstraintException $ex){ + $newKey = $myKey."[".$ex->getRelatedKey()."]"; + return new ConstraintException($ex->getRelatedOperation(), $newKey, $ex->getRelatedValue()); + } + + protected function setInternal($key,$value){ + if (is_null($key)) { + $this->base[] = $value; + } + else { + $this->base[$key] = $value; + } + } + + protected function getInternal($key){ + return $this->offsetExists($key) ? $this->base[$key] : null; + } + + protected function unsetInternal($key){ + unset($this->base[$key]); + } + + + // ArrayAccess methods + public function offsetExists($key) { + return array_key_exists($key, $this->base); + } + + public function offsetSet($key, $value) { + $this->setConstrained($key, $value); + } + + public function offsetUnset($key) { + $this->unsetConstrained($key); + } + + public function offsetGet($key) { + $this->getConstrained($key); + } + +} + +?> \ No newline at end of file diff --git a/private/lib/ConstrainedObjectBehaviourCallable.php b/private/lib/constrained/ConstrainedBehaviour.php similarity index 97% rename from private/lib/ConstrainedObjectBehaviourCallable.php rename to private/lib/constrained/ConstrainedBehaviour.php index 9fd9613..3b32986 100644 --- a/private/lib/ConstrainedObjectBehaviourCallable.php +++ b/private/lib/constrained/ConstrainedBehaviour.php @@ -4,7 +4,7 @@ Author: Riccardo Di Dato Creation Date: 07/gen/2016 */ -interface ConstraintObjectBehaviourCallable { +interface ConstrainedBehaviour { /** * Esegue il callback per le funzioni di set, restituisce il valore finale di constrainedOriginal. * ATTENZIONE!! LA funzione รจ in grado di modificare anche parametri diversi da diff --git a/private/lib/constrained/ConstrainedFactory.php b/private/lib/constrained/ConstrainedFactory.php new file mode 100644 index 0000000..565e142 --- /dev/null +++ b/private/lib/constrained/ConstrainedFactory.php @@ -0,0 +1,37 @@ +getBase(); + foreach ($tmp as $key=>$val){ + $rval->$key = self::debuild($val); + } + } + return $rval; + } + +} + +?> \ No newline at end of file diff --git a/private/lib/constrained/ConstrainedInterface.php b/private/lib/constrained/ConstrainedInterface.php new file mode 100644 index 0000000..6c8552b --- /dev/null +++ b/private/lib/constrained/ConstrainedInterface.php @@ -0,0 +1,32 @@ + \ No newline at end of file diff --git a/private/lib/constrained/ConstrainedMemoryObject.php b/private/lib/constrained/ConstrainedMemoryObject.php new file mode 100644 index 0000000..3eb38e4 --- /dev/null +++ b/private/lib/constrained/ConstrainedMemoryObject.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/private/lib/constrained/ConstrainedObject.php b/private/lib/constrained/ConstrainedObject.php new file mode 100644 index 0000000..6b46a16 --- /dev/null +++ b/private/lib/constrained/ConstrainedObject.php @@ -0,0 +1,220 @@ +setBase($base); + } + + protected function setBase($base){ + $this->base = new stdClass(); + foreach ($base as $key=>$value){ + $this->base->$key = ConstrainedFactory::build($value); + } + } + + public function getBase(){ + return $this->base; + } + + protected function addPathToChildException($myKey, ConstraintException $ex){ + $newKey = $myKey."->".$ex->getRelatedKey(); + return new ConstraintException($ex->getRelatedOperation(), $newKey, $ex->getRelatedValue()); + } + + protected function setInternal($key,$value){ + return $this->base->$key = $value; + } + + protected function getInternal($key){ + return $this->base->$key; + } + + protected function unsetInternal($key){ + unset($this->base->$key); + } + + public function __set($key,$value){ + return $this->setConstrained($key, $value); + } + + public function __get($key){ + return $this->getConstrained($key); + } + + public function __unset($key){ + return $this->unsetConstrained($key); + } +} + +// class ConstrainedObject { +// const SET_BEHAVIOUR_NORMAL = 1; +// const SET_BEHAVIOUR_IGNORE = 2; +// const SET_BEHAVIOUR_THROW = 100; + +// const UNSET_BEHAVIOUR_NORMAL = 1; +// const UNSET_BEHAVIOUR_IGNORE = 2; +// const UNSET_BEHAVIOUR_THROW = 100; + +// const GET_BEHAVIOUR_NORMAL = 1; +// const GET_BEHAVIOUR_EMPTY = 2; +// const GET_BEHAVIOUR_THROW = 100; + +// private $original; + +// private $constraints = array(); + +// public function __construct($original){ +// $this->original = $original; +// } + +// public function __set($key, $value){ +// if ($value instanceof ArrayObject){ +// $value = $value->getArrayCopy(); +// } +// if (array_key_exists($key, $this->constraints)){ +// $constraint = $this->constraints[$key]->set; +// switch ($constraint->behaviour){ +// case self::SET_BEHAVIOUR_NORMAL: +// $this->original->$key = $value; +// break; +// case self::SET_BEHAVIOUR_THROW: +// throw new ConstraintException("Constraint violation: impossible to set field '$key'"); +// break; +// case self::SET_BEHAVIOUR_IGNORE: +// default: +// break; +// } +// if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ +// $this->original = $constraint->callbackClass->executeSetCallback($this->original, $key, $value); +// } +// } +// else { +// $this->original->$key = $value; +// } +// } + +// public function __get($key){ +// $rval = null; +// if (array_key_exists($key, $this->constraints)){ +// $constraint = $this->constraints[$key]->get; +// switch ($constraint->behaviour){ +// case self::GET_BEHAVIOUR_THROW: +// throw new ConstraintException("Constraint violation: impossible to get field '$key'"); +// break; +// case self::GET_BEHAVIOUR_NORMAL: +// $rval = $this->original->$key; +// break; +// case self::GET_BEHAVIOUR_EMPTY: +// default: +// break; +// } +// if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ +// $rval = $constraint->callbackClass->executeGetCallback($this->original, $key); +// } +// } +// else { +// $rval = $this->original->$key; +// } +// if (is_array($rval)){ +// $rval = new ArrayObject($rval); +// } +// return $rval; +// } + +// public function __unset($key){ +// if (array_key_exists($key, $this->constraints)){ +// $constraint = $this->constraints[$key]->unset; +// switch ($constraint->behaviour){ +// case self::UNSET_BEHAVIOUR_NORMAL: +// unset($this->original->$key); +// break; +// case self::UNSET_BEHAVIOUR_THROW: +// throw new ConstraintException("Constraint violation: impossible to unset field '$key'"); +// break; +// case self::UNSET_BEHAVIOUR_IGNORE: +// default: +// break; +// } +// if (property_exists($constraint, "callbackClass") && !is_null($constraint->callbackClass)){ +// $this->original = $constraint->callbackClass->executeUnsetCallback($this->original, $key); +// } +// } +// else { +// unset($this->original->$key); +// } +// } + +// public function createSetBehaviour($key, $behaviour, ConstraintObjectBehaviourCallable $callbackClass = null){ +// if (!array_key_exists($key,$this->constraints)){ +// $this->constraints[$key] = new stdClass(); +// } +// $this->constraints[$key]->set = new stdClass(); +// if ($behaviour < self::SET_BEHAVIOUR_NORMAL){ +// $behaviour = self::SET_BEHAVIOUR_NORMAL; +// } +// else if ($behaviour > self::SET_BEHAVIOUR_THROW){ +// $behaviour = self::SET_BEHAVIOUR_THROW; +// } +// $this->constraints[$key]->set->behaviour = $behaviour; +// $this->constraints[$key]->set->callbackClass = $callbackClass; +// } + +// public function createGetBehaviour($key, $behaviour, ConstraintObjectBehaviourCallable $callbackClass = null){ +// if (!array_key_exists($key,$this->constraints)){ +// $this->constraints[$key] = new stdClass(); +// } +// $this->constraints[$key]->get = new stdClass(); +// if ($behaviour < self::GET_BEHAVIOUR_NORMAL){ +// $behaviour = self::GET_BEHAVIOUR_NORMAL; +// } +// else if ($behaviour > self::GET_BEHAVIOUR_THROW){ +// $behaviour = self::GET_BEHAVIOUR_THROW; +// } +// $this->constraints[$key]->get->behaviour = $behaviour; +// $this->constraints[$key]->get->callbackClass = $callbackClass; +// } + +// public function createUnsetBehaviour($key, $behaviour, ConstraintObjectBehaviourCallable $callbackClass = null){ +// if (!array_key_exists($key,$this->constraints)){ +// $this->constraints[$key] = new stdClass(); +// } +// $this->constraints[$key]->unset = new stdClass(); +// if ($behaviour < self::UNSET_BEHAVIOUR_NORMAL){ +// $behaviour = self::UNSET_BEHAVIOUR_NORMAL; +// } +// else if ($behaviour > self::UNSET_BEHAVIOUR_THROW){ +// $behaviour = self::UNSET_BEHAVIOUR_THROW; +// } +// $this->constraints[$key]->unset->behaviour = $behaviour; +// $this->constraints[$key]->unset->callbackClass = $callbackClass; +// } + +// public function get(){ +// return $this->original; +// } + + +// public function debugInfo(){ +// $rval = array(); +// foreach ($this->original as $key=>$val){ +// $rval[$key] = $this->$key; +// } +// foreach ($this->constraints as $key=>$constr){ +// if (property_exists($constr, "get")){ +// if ($constr->get->behaviour != self::GET_BEHAVIOUR_THROW){ +// $rval[$key] = $this->$key; +// } +// } +// } +// return $rval; +// } +// } + +?> \ No newline at end of file diff --git a/private/lib/constrained/ConstraintException.php b/private/lib/constrained/ConstraintException.php new file mode 100644 index 0000000..5112375 --- /dev/null +++ b/private/lib/constrained/ConstraintException.php @@ -0,0 +1,32 @@ +operation = $operation; + $this->key = $key; + $this->value = $value; + parent::__construct("Impossible to execute operation '$operation' on attribute '$key'".(!is_null($value)?" with value '$value'":"")); + } + + public function getRelatedOperation(){ + return $this->operation; + } + + public function getRelatedKey(){ + return $this->key; + } + + public function getRelatedValue(){ + return $this->value; + } +} + +?> \ No newline at end of file diff --git a/private/lib/constrained/lib.inclusion.php b/private/lib/constrained/lib.inclusion.php new file mode 100644 index 0000000..9b929f4 --- /dev/null +++ b/private/lib/constrained/lib.inclusion.php @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/private/lib/dao/DaoSaveableDefaultImplementation.php b/private/lib/dao/DaoSaveableDefaultImplementation.php index 30a969e..e25395e 100644 --- a/private/lib/dao/DaoSaveableDefaultImplementation.php +++ b/private/lib/dao/DaoSaveableDefaultImplementation.php @@ -6,29 +6,25 @@ Creation Date: 23/dic/2015 class DaoSaveableDefaultImplementation implements DaoSaveable { private $collectionName; -// private $saveableObject; - private $constrainedObject; + private $saveableObject; public function __construct($collectionName, $saveableObject){ $this->collectionName = $collectionName; -// $this->saveableObject = $saveableObject; - $this->constrainedObject = new ConstrainedObject($saveableObject); + $this->updateFromSaveableObj($saveableObject); } public function __set($attr,$value){ -// $this->saveableObject->$attr = $value; - $this->constrainedObject->$attr = $value; + $this->saveableObject->$attr = $value; } - public function __get($attr){ -// return $this->saveableObject->$attr; - return $this->constrainedObject->$attr; + public function &__get($attr){ + $rval = &$this->saveableObject->$attr; + return $rval; } public function __unset($attr){ -// unset($this->saveableObject->$attr); - unset($this->constrainedObject->$attr); + unset($this->saveableObject->$attr); } /** @@ -36,10 +32,29 @@ class DaoSaveableDefaultImplementation implements DaoSaveable { * @see DaoSaveable::getSaveableObj() */ public function getSaveableObj(){ -// return $this->saveableObject->get(); - return $this->constrainedObject->get(); +// 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() @@ -60,15 +75,32 @@ class DaoSaveableDefaultImplementation implements DaoSaveable { * (non-PHPdoc) * @see DaoSaveable::updateFromSaveableObj() */ - public function updateFromSaveableObj($obj){ -// $this->saveableObject = $obj; - $this->constrainedObject = new ConstrainedObject($saveableObject); + public function updateFromSaveableObj($saveableObject){ + $this->saveableObject = $this->toObject($saveableObject); +// $this->saveableObject = $saveableObject; } - protected function getConstrainedObject(){ - return $this->constrainedObject; + 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; } - } ?> \ No newline at end of file diff --git a/private/lib/dao/GenericDao.php b/private/lib/dao/GenericDao.php index 4b133d8..4499af7 100644 --- a/private/lib/dao/GenericDao.php +++ b/private/lib/dao/GenericDao.php @@ -64,6 +64,7 @@ class GenericDao{ $rval = array(); foreach ($cursor as $element){ + var_dump($element); $rval[] = $retClass::buildFromSaveableObj($collectionName, $element); } @@ -77,11 +78,11 @@ class GenericDao{ $saveObj = $model->getSaveableObj(); - if ( !is_null($saveObj->_id) ){ - $collection->update( array("_id"=>$id), $saveObj ); + if ( !is_null($saveObj["_id"]) ){ + $collection->update( array("_id"=>$saveObj["_id"] ), $saveObj ); } else { - $saveObj->_id = new MongoId(); + $saveObj["_id"] = new MongoId(); $collection->insert($saveObj); $model->updateFromSaveableObj($saveObj); } @@ -111,6 +112,7 @@ class GenericDao{ } self::$defaultReturnClass = $retClass; } + } ?> \ No newline at end of file diff --git a/private/lib/dao/models/NotiziaModel.php b/private/lib/dao/models/NotiziaModel.php index 3ed8f3d..7a750b8 100644 --- a/private/lib/dao/models/NotiziaModel.php +++ b/private/lib/dao/models/NotiziaModel.php @@ -4,29 +4,10 @@ Author: Riccardo Di Dato Creation Date: 07/gen/2016 */ -class NotiziaModel extends Model implements ConstraintObjectBehaviourCallable{ +class NotiziaModel extends Model{ public function __construct($saveableObject){ parent::__construct("notizia", $saveableObject); - $this->getConstrainedObject()->createSetBehaviour("numero", ConstrainedObject::SET_BEHAVIOUR_IGNORE, $this); - } - - - public function executeGetCallback($constrainedOriginal, $key){ - - } - - public function executeSetCallback($constrainedOriginal, $key, $value){ - if (strcmp("numero", $key)==0){ - $constrainedOriginal->$key = $value+100; - } - return $constrainedOriginal; - } - - public function executeUnsetCallback($constrainedOriginal, $key){ - - } - } ?> \ No newline at end of file diff --git a/private/lib/lib.inclusions.php b/private/lib/lib.inclusions.php index 7d822d4..88230a7 100644 --- a/private/lib/lib.inclusions.php +++ b/private/lib/lib.inclusions.php @@ -8,9 +8,6 @@ Creation Date: 14/nov/2013 require_once(dirname(__FILE__)."/CoreException.php"); // Constrained Objects -require_once(dirname(__FILE__)."/ConstraintException.php"); -require_once(dirname(__FILE__)."/ConstrainedObjectBehaviourCallable.php"); -require_once(dirname(__FILE__)."/ConstrainedObject.php"); // My instances require_once(dirname(__FILE__)."/lib.instances.php"); @@ -18,6 +15,8 @@ require_once(dirname(__FILE__)."/lib.instances.php"); // Include other modules +// require_once(dirname(__FILE__)."/constrained/lib.inclusion.php"); + require_once(dirname(__FILE__)."/gui/lib.inclusion.php"); require_once(dirname(__FILE__)."/logger/lib.inclusion.php"); diff --git a/public/index.php b/public/index.php index 30ff49b..069b7c9 100644 --- a/public/index.php +++ b/public/index.php @@ -9,12 +9,22 @@ require_once(dirname(__FILE__)."/load.php"); try { // var_dump($dao->query("notizia")); - $prova = new stdClass(); - $prova->numero = 0; - $model = new NotiziaModel($prova); - $model->numero = 7; + $models = $dao->query("notizia"); + $model = reset($models); var_dump($model); + echo "

"; + + $model->immagini = array(); + + $model->immagini[] = "1.jpg"; + $model->about->giorno = "boh"; + + var_dump($model); + echo "

"; + + $dao->save($model); + } catch (Exception $ex){ echo $ex->getMessage(); From 1140fd6d9d4ac4dd9b472270eeb36f3f5fa751fc Mon Sep 17 00:00:00 2001 From: Riccardo Di Dato Date: Tue, 12 Jan 2016 15:33:50 +0100 Subject: [PATCH 2/2] Aggiunto costruttore di default a DaoSaveableDefaultImplementation --- private/lib/dao/DaoSaveableDefaultImplementation.php | 5 ++++- private/lib/dao/models/NotiziaModel.php | 2 +- public/index.php | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/private/lib/dao/DaoSaveableDefaultImplementation.php b/private/lib/dao/DaoSaveableDefaultImplementation.php index e25395e..2c5f348 100644 --- a/private/lib/dao/DaoSaveableDefaultImplementation.php +++ b/private/lib/dao/DaoSaveableDefaultImplementation.php @@ -9,7 +9,10 @@ class DaoSaveableDefaultImplementation implements DaoSaveable { private $saveableObject; - public function __construct($collectionName, $saveableObject){ + public function __construct($collectionName, $saveableObject = null){ + if (is_null($saveableObject)){ + $saveableObject = new stdClass(); + } $this->collectionName = $collectionName; $this->updateFromSaveableObj($saveableObject); } diff --git a/private/lib/dao/models/NotiziaModel.php b/private/lib/dao/models/NotiziaModel.php index 7a750b8..babe05b 100644 --- a/private/lib/dao/models/NotiziaModel.php +++ b/private/lib/dao/models/NotiziaModel.php @@ -5,7 +5,7 @@ Creation Date: 07/gen/2016 */ class NotiziaModel extends Model{ - public function __construct($saveableObject){ + public function __construct($saveableObject = null){ parent::__construct("notizia", $saveableObject); } } diff --git a/public/index.php b/public/index.php index 069b7c9..50d6445 100644 --- a/public/index.php +++ b/public/index.php @@ -15,7 +15,7 @@ try { var_dump($model); echo "

"; - $model->immagini = array(); +// $model->immagini = array(); $model->immagini[] = "1.jpg"; $model->about->giorno = "boh";