PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/metadata/MetadataSchema.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 179 lines | 65 code | 25 blank | 89 comment | 4 complexity | 096035e4216f557d4c28b482e4f9eda6 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @defgroup metadata
  4. */
  5. /**
  6. * @file classes/metadata/MetadataSchema.inc.php
  7. *
  8. * Copyright (c) 2000-2012 John Willinsky
  9. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  10. *
  11. * @class MetadataSchema
  12. * @ingroup metadata
  13. * @see MetadataProperty
  14. * @see MetadataRecord
  15. *
  16. * @brief Class that represents a meta-data schema (e.g. NLM element-citation,
  17. * OpenURL, dc(terms), etc.
  18. *
  19. * NB: We currently provide meta-data schemas as classes for better performance
  20. * and code readability. It might, however, be necessary to maintain meta-data
  21. * schemas in the database for higher flexibility and easier run-time configuration/
  22. * installation of new schemas.
  23. */
  24. // $Id$
  25. import('metadata.MetadataProperty');
  26. class MetadataSchema {
  27. /** @var string */
  28. var $_name;
  29. /** @var string */
  30. var $_namespace;
  31. /**
  32. * @var array meta-data properties (predicates)
  33. * supported for this meta-data schema.
  34. */
  35. var $_properties = array();
  36. //
  37. // Get/set methods
  38. //
  39. /**
  40. * Get the name of the schema
  41. * @return string
  42. */
  43. function getName() {
  44. return $this->_name;
  45. }
  46. /**
  47. * Set the name of the schema
  48. * @param $name string
  49. */
  50. function setName($name) {
  51. $this->_name = $name;
  52. }
  53. /**
  54. * Get the internal namespace qualifier of the schema
  55. * @return string
  56. */
  57. function getNamespace() {
  58. return $this->_namespace;
  59. }
  60. /**
  61. * Set the internal namespace qualifier of the schema
  62. * @param $namespace string
  63. */
  64. function setNamespace($namespace) {
  65. $this->_namespace = $namespace;
  66. }
  67. /**
  68. * Get the properties of the meta-data schema.
  69. * @return array an array of MetadataProperties
  70. */
  71. function &getProperties() {
  72. return $this->_properties;
  73. }
  74. /**
  75. * Get a property. Returns null if the property
  76. * doesn't exist.
  77. * @return MetadataProperty
  78. */
  79. function &getProperty($propertyName) {
  80. assert(is_string($propertyName));
  81. if ($this->hasProperty($propertyName)) {
  82. $property =& $this->_properties[$propertyName];
  83. } else {
  84. $property = null;
  85. }
  86. return $property;
  87. }
  88. /**
  89. * Returns the property id with prefixed name space
  90. * for use in an external context (e.g. Forms, Templates).
  91. * @param $propertyName string
  92. * @return string
  93. */
  94. function getNamespacedPropertyId($propertyName) {
  95. $property =& $this->getProperty($propertyName);
  96. assert(is_a($property, 'MetadataProperty'));
  97. return $this->getNamespace().ucfirst($property->getId());
  98. }
  99. /**
  100. * (Re-)set all properties of this meta-data schema.
  101. * @param $properties array an array of MetadataProperties
  102. */
  103. function setProperties(&$properties) {
  104. // Remove the existing properties
  105. $this->_properties = array();
  106. // Insert the new properties
  107. foreach($properties as $property) {
  108. $this->addProperty($property);
  109. }
  110. }
  111. /**
  112. * Add a property to this meta-data schema
  113. * @param $element MetadataElement
  114. */
  115. function addProperty(&$property) {
  116. assert(is_a($property, 'MetadataProperty'));
  117. $propertyName = $property->getName();
  118. // Make sure that this property has not been added before
  119. assert(!is_null($propertyName) && !isset($this->_properties[$propertyName]));
  120. // Add the property
  121. $this->_properties[$propertyName] =& $property;
  122. }
  123. /**
  124. * Get the property names defined for this meta-data schema
  125. * @return array an array of string values representing valid property names
  126. */
  127. function getPropertyNames() {
  128. return array_keys($this->_properties);
  129. }
  130. /**
  131. * Get the names of properties with a given data type.
  132. * @param $propertyType mixed a valid property type description
  133. * @return array an array of string values representing valid property names
  134. */
  135. function getPropertyNamesByType($propertyType) {
  136. assert(in_array($propertyType, MetadataProperty::getSupportedTypes()));
  137. $propertyNames = array();
  138. foreach($this->_properties as $property) {
  139. if (in_array($propertyType, $property->getTypes())) {
  140. $propertyNames[] = $property->getName();
  141. }
  142. }
  143. return $propertyNames;
  144. }
  145. /**
  146. * Checks whether a property exists in the meta-data schema
  147. * @param $propertyName string
  148. * @return boolean
  149. */
  150. function hasProperty($propertyName) {
  151. return isset($this->_properties[$propertyName]);
  152. }
  153. }
  154. ?>