Files
fdn2/app/private/lib/constrained/ConstrainedObject.php
2022-08-29 23:20:16 +02:00

220 lines
6.4 KiB
PHP

<?php
/*
Author: Riccardo Di Dato
Creation Date: 07/gen/2016
*/
class ConstrainedObject extends Constrained {
private $base;
public function __construct($base){
parent::__construct();
$this->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;
// }
// }
?>