DDDBL 2.0
 All Classes Namespaces Files Functions Variables
Public Member Functions | Private Attributes | List of all members
DDDBL\DataObject Class Reference

Public Member Functions

 __construct ($cloValidator=null, array $arrData=array())
 
 add (array $arrData)
 
 delete ($strKey)
 
 exists ($strKey)
 
 get ($strKey)
 
 getAll ()
 
 update (array $arrData)
 

Private Attributes

 $arrData = array()
 
 $cloValidator = null
 

Detailed Description

a DataObject is a generic object to store data under given keys.

it allows getting, adding, updating and deleting data.

a validation callback can be provided to ensure, that the stored data validate correctly.

Definition at line 17 of file DataObject.class.php.

Constructor & Destructor Documentation

DDDBL\DataObject::__construct (   $cloValidator = null,
array  $arrData = array() 
)
Parameters
$cloValidator- optional validator callback to validate stored data
$arrData- optional list of data to store in object
Exceptions
UnexpectedParameterTypeException- if validator callback is not a callable

initiates the data-object and stores the validator callback. if no callback is given, a default callback is stored, which validates against everything.

if optional data are given, they are passed to DataObject::add(), to be stored immediatley

Definition at line 43 of file DataObject.class.php.

43  {
44 
45  if(!is_null($cloValidator) && !is_callable($cloValidator))
46  throw new UnexpectedParameterTypeException('callable', $cloValidator);
47 
48  $this->cloValidator = (!is_null($cloValidator)) ? $cloValidator : function() {return true; };
49 
50  if(!empty($arrData))
51  $this->add($arrData);
52 
53  }

Member Function Documentation

DDDBL\DataObject::add ( array  $arrData)
Parameters
$arrData- list of data to store in object
Exceptions
\Exception- if a key is already in use
\Exception- if the final data-set do not validate

add the list of data to the existing ones. the given data must have the following format: array([key] => data [key] => data, [..])

if a key in the given data is already used in stored data the addition is aborted and an exception is thrown.

the stored data are only modified on success

Definition at line 73 of file DataObject.class.php.

73  {
74 
75  $arrMergedData = array_merge($this->arrData, $arrData);
76 
77  foreach($arrData AS $strKey => $mixData)
78  if(array_key_exists($strKey, $this->arrData))
79  throw new \Exception("could not store data, key is already in use: $strKey");
80 
82 
83  if(!$cloValidator($arrMergedData))
84  throw new \Exception("given data do not validate");
85 
86  $this->arrData = $arrMergedData;
87 
88  }
DDDBL\DataObject::delete (   $strKey)
Parameters
$strKey- the key of the value to delete
Exceptions
UnexpectedParameterTypeException- if given key is not a string

delete the value stored under the given key. if given key do not exists, nothing is done!

Definition at line 127 of file DataObject.class.php.

127  {
128 
129  if(!is_string($strKey))
130  throw new UnexpectedParameterTypeException('string', $strKey);
131 
132  if($this->exists($strKey))
133  unset($this->arrData[$strKey]);
134 
135  }
DDDBL\DataObject::exists (   $strKey)
Parameters
$strKey- the key to check
Exceptions
UnexpectedParameterTypeException- if given key is not a string
Returns
(boolean) true, if key exists
(boolean) false, if key do not exists

check if the given key exists

Definition at line 148 of file DataObject.class.php.

148  {
149 
150  if(!is_string($strKey))
151  throw new UnexpectedParameterTypeException('string', $strKey);
152 
153  if(!array_key_exists($strKey, $this->arrData))
154  return false;
155 
156  return true;
157 
158  }
DDDBL\DataObject::get (   $strKey)
Parameters
$strKey- the key to get the value from
Exceptions
UnexpectedParameterTypeException- if given key is not a string
\Exception- if given key is unknown
Returns
(mixed) the value stored under the given key

return the value stored under the given key

Definition at line 171 of file DataObject.class.php.

171  {
172 
173  if(!is_string($strKey))
174  throw new UnexpectedParameterTypeException('string', $strKey);
175 
176  if(!$this->exists($strKey))
177  throw new \Exception("unknown key: $strKey");
178 
179  return $this->arrData[$strKey];
180 
181  }
DDDBL\DataObject::getAll ( )

return all stored data in the structure of: array([key] => data [key] => data, [..])

Definition at line 189 of file DataObject.class.php.

189  {
190 
191  return $this->arrData;
192 
193  }
DDDBL\DataObject::update ( array  $arrData)
Parameters
$arrData- list of data to update
Exceptions
\Exception- if the final data-set do not validate

update the stored data with the given data-set. for the structure of $arrData have a look at DataObject:add()

existing keys are overwritten with new values. new keys are added to the data-set.

if validation of final set fails, an exception is thrown. no data are modified on failure.

Definition at line 105 of file DataObject.class.php.

105  {
106 
107  $arrMergedData = array_merge($this->arrData, $arrData);
108 
110 
111  if(!$cloValidator($arrMergedData))
112  throw new \Exception("given data do not validate");
113 
114  $this->arrData = $arrMergedData;
115 
116  }

Member Data Documentation

DDDBL\DataObject::$arrData = array()
private

list of stored data

Definition at line 22 of file DataObject.class.php.

DDDBL\DataObject::$cloValidator = null
private

callback to validate all stored data

Definition at line 27 of file DataObject.class.php.


The documentation for this class was generated from the following file: