PropertyAccess.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Part of XML_XRD
  4. *
  5. * PHP version 5
  6. *
  7. * @category XML
  8. * @package XML_XRD
  9. * @author Christian Weiske <cweiske@php.net>
  10. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  11. * @link http://pear.php.net/package/XML_XRD
  12. */
  13. require_once 'XML/XRD/LogicException.php';
  14. require_once 'XML/XRD/Element/Property.php';
  15. /**
  16. * Provides ArrayAccess to extending classes (XML_XRD and XML_XRD_Element_Link).
  17. *
  18. * By extending PropertyAccess, access to properties is possible with
  19. * "$object['propertyType']" array access notation.
  20. *
  21. * @category XML
  22. * @package XML_XRD
  23. * @author Christian Weiske <cweiske@php.net>
  24. * @license http://www.gnu.org/copyleft/lesser.html LGPL
  25. * @version Release: @package_version@
  26. * @link http://pear.php.net/package/XML_XRD
  27. */
  28. abstract class XML_XRD_PropertyAccess implements ArrayAccess
  29. {
  30. /**
  31. * Array of property objects
  32. *
  33. * @var array
  34. */
  35. public $properties = array();
  36. /**
  37. * Check if the property with the given type exists
  38. *
  39. * Part of the ArrayAccess interface
  40. *
  41. * @param string $type Property type to check for
  42. *
  43. * @return boolean True if it exists
  44. */
  45. public function offsetExists($type)
  46. {
  47. foreach ($this->properties as $prop) {
  48. if ($prop->type == $type) {
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. /**
  55. * Return the highest ranked property with the given type
  56. *
  57. * Part of the ArrayAccess interface
  58. *
  59. * @param string $type Property type to check for
  60. *
  61. * @return string Property value or NULL if empty
  62. */
  63. public function offsetGet($type)
  64. {
  65. foreach ($this->properties as $prop) {
  66. if ($prop->type == $type) {
  67. return $prop->value;
  68. }
  69. }
  70. return null;
  71. }
  72. /**
  73. * Not implemented.
  74. *
  75. * Part of the ArrayAccess interface
  76. *
  77. * @param string $type Property type to check for
  78. * @param string $value New property value
  79. *
  80. * @return void
  81. *
  82. * @throws XML_XRD_LogicException Always
  83. */
  84. public function offsetSet($type, $value)
  85. {
  86. throw new XML_XRD_LogicException('Changing properties not implemented');
  87. }
  88. /**
  89. * Not implemented.
  90. *
  91. * Part of the ArrayAccess interface
  92. *
  93. * @param string $type Property type to check for
  94. *
  95. * @return void
  96. *
  97. * @throws XML_XRD_LogicException Always
  98. */
  99. public function offsetUnset($type)
  100. {
  101. throw new XML_XRD_LogicException('Changing properties not implemented');
  102. }
  103. /**
  104. * Get all properties with the given type
  105. *
  106. * @param string $type Property type to filter by
  107. *
  108. * @return array Array of XML_XRD_Element_Property objects
  109. */
  110. public function getProperties($type = null)
  111. {
  112. if ($type === null) {
  113. return $this->properties;
  114. }
  115. $properties = array();
  116. foreach ($this->properties as $prop) {
  117. if ($prop->type == $type) {
  118. $properties[] = $prop;
  119. }
  120. }
  121. return $properties;
  122. }
  123. }
  124. ?>