Inseriti metodi sperimentali per i callback su set delle variabili Dao testato IN LETTURA
137 lines
4.1 KiB
PHP
137 lines
4.1 KiB
PHP
<?php
|
|
/*
|
|
Author: Riccardo Di Dato
|
|
Creation Date: 07/gen/2016
|
|
*/
|
|
|
|
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 (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;
|
|
}
|
|
}
|
|
|
|
?>
|