DDDBL 2.0
 All Classes Namespaces Files Functions Variables
DataObject.class.php
Go to the documentation of this file.
1 <?php
2 
3 namespace DDDBL;
4 
17 class DataObject {
18 
22  private $arrData = array();
23 
27  private $cloValidator = null;
28 
43  public function __construct($cloValidator = null, array $arrData = array()) {
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  }
54 
73  public function add(array $arrData) {
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 
81  $cloValidator = $this->cloValidator;
82 
83  if(!$cloValidator($arrMergedData))
84  throw new \Exception("given data do not validate");
85 
86  $this->arrData = $arrMergedData;
87 
88  }
89 
105  public function update(array $arrData) {
106 
107  $arrMergedData = array_merge($this->arrData, $arrData);
108 
109  $cloValidator = $this->cloValidator;
110 
111  if(!$cloValidator($arrMergedData))
112  throw new \Exception("given data do not validate");
113 
114  $this->arrData = $arrMergedData;
115 
116  }
117 
127  public function delete($strKey) {
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  }
136 
148  public function exists($strKey) {
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  }
159 
171  public function get($strKey) {
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  }
182 
189  public function getAll() {
190 
191  return $this->arrData;
192 
193  }
194 
195 }