Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
DataObject.inc.php
1 <?php
2 
18 class DataObject {
20  var $_data = array();
21 
23  var $_hasLoadableAdapters = false;
24 
27 
29  var $_extractionAdaptersLoaded = false;
30 
33 
35  var $_injectionAdaptersLoaded = false;
36 
40  function DataObject() {
41  }
42 
43 
44  //
45  // Getters and Setters
46  //
53  function &getLocalizedData($key) {
54  $localePrecedence = AppLocale::getLocalePrecedence();
55  foreach ($localePrecedence as $locale) {
56  $value =& $this->getData($key, $locale);
57  if (!empty($value)) return $value;
58  unset($value);
59  }
60 
61  // Fallback: Get the first available piece of data.
62  $data =& $this->getData($key, null);
63  if (!empty($data)) {
64  // WARNING: Collapsing the following into a single line causes PHP 5.0.5 to die.
65  $locales = array_keys($data);
66  $firstLocale = array_shift($locales);
67  return $data[$firstLocale];
68  }
69 
70  // No data available; return null.
71  unset($data);
72  $data = null;
73  return $data;
74  }
75 
82  function &getData($key, $locale = null) {
83  if (is_null($locale)) {
84  if (isset($this->_data[$key])) {
85  return $this->_data[$key];
86  }
87  } else {
88  // see http://bugs.php.net/bug.php?id=29848
89  if (isset($this->_data[$key]) && is_array($this->_data[$key]) && isset($this->_data[$key][$locale])) {
90  return $this->_data[$key][$locale];
91  }
92  }
93  $nullVar = null;
94  return $nullVar;
95  }
96 
114  function setData($key, $value, $locale = null) {
115  if (is_null($locale)) {
116  // This is either a non-localized value or we're
117  // passing in all locales at once.
118  if (is_null($value)) {
119  if (isset($this->_data[$key])) unset($this->_data[$key]);
120  } else {
121  $this->_data[$key] = $value;
122  }
123  } else {
124  // (Un-)set a single localized value.
125  if (is_null($value)) {
126  // see http://bugs.php.net/bug.php?id=29848
127  if (isset($this->_data[$key])) {
128  if (is_array($this->_data[$key]) && isset($this->_data[$key][$locale])) unset($this->_data[$key][$locale]);
129  // Was this the last entry for the data variable?
130  if (empty($this->_data[$key])) unset($this->_data[$key]);
131  }
132  } else {
133  $this->_data[$key][$locale] = $value;
134  }
135  }
136  }
137 
144  function hasData($key, $locale = null) {
145  if (is_null($locale)) {
146  return isset($this->_data[$key]);
147  } else {
148  // see http://bugs.php.net/bug.php?id=29848
149  return isset($this->_data[$key]) && is_array($this->_data[$key]) && isset($this->_data[$key][$locale]);
150  }
151  }
152 
157  function &getAllData() {
158  return $this->_data;
159  }
160 
165  function setAllData(&$data) {
166  $this->_data =& $data;
167  }
168 
173  function getId() {
174  return $this->getData('id');
175  }
176 
181  function setId($id) {
182  return $this->setData('id', $id);
183  }
184 
185 
186  //
187  // Public helper methods
188  //
209  function upcastTo($targetObject) {
210  // Make sure that target is really inheriting
211  // from this class.
212  assert(is_a($targetObject, get_class($this)));
213 
214  // Copy data from the source to the target.
215  $targetObject->setAllData($this->getAllData());
216 
217  // Return the upcast object.
218  return $targetObject;
219  }
220 
221 
222  //
223  // MetadataProvider interface implementation
224  //
229  function setHasLoadableAdapters($hasLoadableAdapters) {
230  $this->_hasLoadableAdapters = $hasLoadableAdapters;
231  }
232 
237  function getHasLoadableAdapters() {
239  }
240 
247  function addSupportedMetadataAdapter($metadataAdapter) {
248  $metadataSchemaName = $metadataAdapter->getMetadataSchemaName();
249  assert(!empty($metadataSchemaName));
250 
251  // NB: Some adapters are injectors and extractors at the same time,
252  // notably the meta-data description dummy adapter that converts
253  // from/to a meta-data description. That's why we have to check
254  // input and output type separately.
255 
256  // Is this a meta-data extractor?
257  $inputType = $metadataAdapter->getInputType();
258  if ($inputType->checkType($this)) {
259  if (!isset($this->_metadataExtractionAdapters[$metadataSchemaName])) {
260  $this->_metadataExtractionAdapters[$metadataSchemaName] = $metadataAdapter;
261  }
262  }
263 
264  // Is this a meta-data injector?
265  $outputType = $metadataAdapter->getOutputType();
266  if ($outputType->checkType($this)) {
267  if (!isset($this->_metadataInjectionAdapters[$metadataSchemaName])) {
268  $this->_metadataInjectionAdapters[$metadataSchemaName] = $metadataAdapter;
269  }
270  }
271  }
272 
280  function removeSupportedMetadataAdapter($metadataSchemaName) {
281  $result = false;
282  if (isset($this->_metadataExtractionAdapters[$metadataSchemaName])) {
283  unset($this->_metadataExtractionAdapters[$metadataSchemaName]);
284  $result = true;
285  }
286  if (isset($this->_metadataInjectionAdapters[$metadataSchemaName])) {
287  unset($this->_metadataInjectionAdapters[$metadataSchemaName]);
288  $result = true;
289  }
290  return $result;
291  }
292 
299  function getSupportedExtractionAdapters() {
300  // Load meta-data adapters from the database.
301  if ($this->getHasLoadableAdapters() && !$this->_extractionAdaptersLoaded) {
302  $filterDao = DAORegistry::getDAO('FilterDAO'); /* @var $filterDao FilterDAO */
303  $loadedAdapters = $filterDao->getObjectsByTypeDescription('class::%', 'metadata::%', $this);
304  foreach($loadedAdapters as $loadedAdapter) {
305  $this->addSupportedMetadataAdapter($loadedAdapter);
306  }
307  $this->_extractionAdaptersLoaded = true;
308  }
309 
311  }
312 
319  function getSupportedInjectionAdapters() {
320  // Load meta-data adapters from the database.
321  if ($this->getHasLoadableAdapters() && !$this->_injectionAdaptersLoaded) {
322  $filterDao = DAORegistry::getDAO('FilterDAO'); /* @var $filterDao FilterDAO */
323  $loadedAdapters = $filterDao->getObjectsByTypeDescription('metadata::%', 'class::%', $this, false);
324  foreach($loadedAdapters as $loadedAdapter) {
325  $this->addSupportedMetadataAdapter($loadedAdapter);
326  }
327  $this->_injectionAdaptersLoaded = true;
328  }
329 
331  }
332 
338  function getSupportedMetadataSchemas() {
339  $supportedMetadataSchemas = array();
340  $extractionAdapters = $this->getSupportedExtractionAdapters();
341  foreach($extractionAdapters as $metadataAdapter) {
342  $supportedMetadataSchemas[] = $metadataAdapter->getMetadataSchema();
343  }
344  return $supportedMetadataSchemas;
345  }
346 
353  function getMetadataFieldNames($translated = true) {
354  // Create a list of all possible meta-data field names
355  $metadataFieldNames = array();
356  $extractionAdapters = $this->getSupportedExtractionAdapters();
357  foreach($extractionAdapters as $metadataAdapter) {
358  // Add the field names from the current adapter
359  $metadataFieldNames = array_merge(
360  $metadataFieldNames,
361  $metadataAdapter->getDataObjectMetadataFieldNames($translated)
362  );
363  }
364  return array_unique($metadataFieldNames);
365  }
366 
375  function getSetMetadataFieldNames($translated = true) {
376  // Retrieve a list of all possible meta-data field names
377  $metadataFieldNameCandidates = $this->getMetadataFieldNames($translated);
378 
379  // Only retain those fields that have data
380  $metadataFieldNames = array();
381  foreach($metadataFieldNameCandidates as $metadataFieldNameCandidate) {
382  if($this->hasData($metadataFieldNameCandidate)) {
383  $metadataFieldNames[] = $metadataFieldNameCandidate;
384  }
385  }
386  return $metadataFieldNames;
387  }
388 
394  function getLocaleMetadataFieldNames() {
395  return $this->getMetadataFieldNames(true);
396  }
397 
404  return $this->getMetadataFieldNames(false);
405  }
406 
414  function injectMetadata($metadataDescription) {
415  $dataObject = null;
416  $metadataSchemaName = $metadataDescription->getMetadataSchemaName();
417  $injectionAdapters = $this->getSupportedInjectionAdapters();
418  if(isset($injectionAdapters[$metadataSchemaName])) {
419  // Get the meta-data adapter that supports the
420  // given meta-data description's schema.
421  $metadataAdapter = $injectionAdapters[$metadataSchemaName]; /* @var $metadataAdapter MetadataDataObjectAdapter */
422 
423  // Pass in a reference to the data object which
424  // the filter will use to update the current instance
425  // of the data object.
426  $metadataAdapter->setTargetDataObject($this);
427 
428  // Use adapter filter to convert from a meta-data
429  // description to a data object.
430  $dataObject = $metadataAdapter->execute($metadataDescription);
431  }
432  return $dataObject;
433  }
434 
441  function extractMetadata($metadataSchema) {
442  $metadataDescription = null;
443  $metadataSchemaName = $metadataSchema->getClassName();
444  $extractionAdapters = $this->getSupportedExtractionAdapters();
445  if(isset($extractionAdapters[$metadataSchemaName])) {
446  // Get the meta-data adapter that supports the
447  // given meta-data description's schema.
448  $metadataAdapter = $extractionAdapters[$metadataSchemaName];
449 
450  // Use adapter filter to convert from a data object
451  // to a meta-data description.
452  $metadataDescription = $metadataAdapter->execute($this);
453  }
454  return $metadataDescription;
455  }
456 }
457 ?>
static & getDAO($name, $dbconn=null)
static getLocalePrecedence()
getMetadataFieldNames($translated=true)
getSupportedExtractionAdapters()
getSetMetadataFieldNames($translated=true)
& getLocalizedData($key)
setAllData(&$data)
upcastTo($targetObject)
getLocaleMetadataFieldNames()
& getData($key, $locale=null)
getSupportedInjectionAdapters()
extractMetadata($metadataSchema)
injectMetadata($metadataDescription)
addSupportedMetadataAdapter($metadataAdapter)
getSupportedMetadataSchemas()
removeSupportedMetadataAdapter($metadataSchemaName)
setHasLoadableAdapters($hasLoadableAdapters)
Any class with an associated DAO should extend this class.
$_metadataExtractionAdapters
setData($key, $value, $locale=null)
hasData($key, $locale=null)
getAdditionalMetadataFieldNames()