6 * The DataObjectPool is a class to manage
7 * the DataObjects for different types.
9 * DataObjects are stored within groups. Every group
10 * has a validatator, with is applyed to
11 * every DataObject stored in the group.
12 * If no validatator is set, no validation will
15 * A DataObject is referenced by an identifier,
16 * which is uniqiue within a group.
18 * when creating a DataObjectPool instance,
19 * the wanted group is set. All following
20 * operations are done at this group.
23 class DataObjectPool {
26 * the actual group to operate on
29 private $strGroup = null;
32 * list of validators for each group. structure:
33 * array([group] => validator-callback,
34 * [group-n] => validator-callback-n, [..])
37 static private $arrValidatorList = array();
40 * list of DataObjects. stored in the following structure:
41 * array([group][uniqueue-identifier] => DataObject-reference,
42 * [group-n][uniqueue-identifier-n] => DataObject-reference-n, [..])
45 static private $arrDataObjects = array();
48 * @param $strGroup - the group of DataObjects to operate on
50 * @throws UnexpectedParameterTypeException - if given group is not a string
52 * create an instance of DataObjectPool and store the group
56 public function __construct($strGroup) {
58 if(!is_string($strGroup))
59 throw new UnexpectedParameterTypeException('string', $strGroup);
61 $this->strGroup = $strGroup;
63 if(!array_key_exists($this->strGroup, self::$arrValidatorList))
64 self::$arrValidatorList[$this->strGroup] = null;
66 if(!array_key_exists($this->strGroup, self::$arrDataObjects))
67 self::$arrDataObjects[$this->strGroup] = array();
72 * @param $cloValidator - the validator to set for the group
74 * @throws UnexpectedParameterTypeException - if given validator is not a callable
76 * set the validator for the active group. this validator
77 * is given to each newly created DataObject.
78 * if it is changed, the existing DataObjects are
82 public function setValidator($cloValidator) {
84 if(!is_callable($cloValidator))
85 throw new UnexpectedParameterTypeException('string', $cloValidator);
87 self::$arrValidatorList[$this->strGroup] = $cloValidator;
92 * @param $strIdentifier - the unique identifier of the DataObject
93 * @param $arrData - the data to store in the DataObject
95 * @see DataObject:add()
97 * @throws UnexpectedParameterTypeException - if identifier is not a string
98 * @throws \Exception - if given identifier is not unique
100 * @returns (DataObject) - reference to the created DataObject-instance
102 * create a new DataObject and store it in the pool. The given
103 * identifier is the key to retrieve the DataObject from the pool.
104 * The given data are stored within the DataObject.
106 * After creation and storage of the DataObject, a reference
107 * to the object is returned
110 public function add($strIdentifier, array $arrData) {
112 if(!is_string($strIdentifier))
113 throw new UnexpectedParameterTypeException('string', $strIdentifier);
115 if($this->exists($strIdentifier))
116 throw new \Exception ("identifier already in use: $strIdentifier");
118 $objDataObject = new DataObject(self::$arrValidatorList[$this->strGroup], $arrData);
120 self::$arrDataObjects[$this->strGroup][$strIdentifier] = $objDataObject;
122 return $objDataObject;
127 * @param $strIdentifier - the identifier of the object to delete
129 * @throws UnexpectedParameterTypeException - if given identifier is not a string
130 * @throws \Exception - if the given identifier is not known
132 * delete the stored DataObject from the DataObjectPool
135 public function delete($strIdentifier) {
137 if(!is_string($strIdentifier))
138 throw new UnexpectedParameterTypeException('string', $strIdentifier);
140 if(!$this->exists($strIdentifier))
141 throw new \Exception ("DataObject not found, identifier unknown: $strIdentifier");
143 unset(self::$arrDataObjects[$this->strGroup][$strIdentifier]);
148 * @param $strIdentifier - the identifier to check
150 * @throws UnexpectedParameterTypeException - if given identifier is not a string
152 * @returns (boolean) true, if the identifier exists
153 * @returns (boolean) false, if the identifier do not exists
156 public function exists($strIdentifier) {
158 if(!is_string($strIdentifier))
159 throw new UnexpectedParameterTypeException('string', $strIdentifier);
161 if(!isset(self::$arrDataObjects[$this->strGroup][$strIdentifier]))
169 * @param $strIdentifier - the identifier of the DataObject to retrieve
171 * @throws UnexpectedParameterTypeException - if given identifier is not a string
172 * @throws \Exception - if given identifier is unknown
174 * @returns (DataObject) - reference to the DataObject
176 * returns a reference to the DataObject stored under the identifer
179 public function get($strIdentifier) {
181 if(!is_string($strIdentifier))
182 throw new UnexpectedParameterTypeException('string', $strIdentifier);
184 if(!$this->exists($strIdentifier))
185 throw new \Exception ("DataObject not found, identifier unknown: $strIdentifier");
187 return self::$arrDataObjects[$this->strGroup][$strIdentifier];
192 * @returns (array) - list of all DataObjects of the active group
194 * returns an array of all stored DataObjects of the active group
195 * with the following structure:
197 * array([identifier] => DataObject-reference,
198 * [identifier-n] => DataObject-reference-n, [..])
201 public function getAll() {
203 return self::$arrDataObjects[$this->strGroup];