PropertyList.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Generic property list implementation
  4. */
  5. class HTMLPurifier_PropertyList
  6. {
  7. /**
  8. * Internal data-structure for properties.
  9. * @type array
  10. */
  11. protected $data = array();
  12. /**
  13. * Parent plist.
  14. * @type HTMLPurifier_PropertyList
  15. */
  16. protected $parent;
  17. /**
  18. * Cache.
  19. * @type array
  20. */
  21. protected $cache;
  22. /**
  23. * @param HTMLPurifier_PropertyList $parent Parent plist
  24. */
  25. public function __construct($parent = null)
  26. {
  27. $this->parent = $parent;
  28. }
  29. /**
  30. * Recursively retrieves the value for a key
  31. * @param string $name
  32. * @throws HTMLPurifier_Exception
  33. */
  34. public function get($name)
  35. {
  36. if ($this->has($name)) {
  37. return $this->data[$name];
  38. }
  39. // possible performance bottleneck, convert to iterative if necessary
  40. if ($this->parent) {
  41. return $this->parent->get($name);
  42. }
  43. throw new HTMLPurifier_Exception("Key '$name' not found");
  44. }
  45. /**
  46. * Sets the value of a key, for this plist
  47. * @param string $name
  48. * @param mixed $value
  49. */
  50. public function set($name, $value)
  51. {
  52. $this->data[$name] = $value;
  53. }
  54. /**
  55. * Returns true if a given key exists
  56. * @param string $name
  57. * @return bool
  58. */
  59. public function has($name)
  60. {
  61. return array_key_exists($name, $this->data);
  62. }
  63. /**
  64. * Resets a value to the value of it's parent, usually the default. If
  65. * no value is specified, the entire plist is reset.
  66. * @param string $name
  67. */
  68. public function reset($name = null)
  69. {
  70. if ($name == null) {
  71. $this->data = array();
  72. } else {
  73. unset($this->data[$name]);
  74. }
  75. }
  76. /**
  77. * Squashes this property list and all of its property lists into a single
  78. * array, and returns the array. This value is cached by default.
  79. * @param bool $force If true, ignores the cache and regenerates the array.
  80. * @return array
  81. */
  82. public function squash($force = false)
  83. {
  84. if ($this->cache !== null && !$force) {
  85. return $this->cache;
  86. }
  87. if ($this->parent) {
  88. return $this->cache = array_merge($this->parent->squash($force), $this->data);
  89. } else {
  90. return $this->cache = $this->data;
  91. }
  92. }
  93. /**
  94. * Returns the parent plist.
  95. * @return HTMLPurifier_PropertyList
  96. */
  97. public function getParent()
  98. {
  99. return $this->parent;
  100. }
  101. /**
  102. * Sets the parent plist.
  103. * @param HTMLPurifier_PropertyList $plist Parent plist
  104. */
  105. public function setParent($plist)
  106. {
  107. $this->parent = $plist;
  108. }
  109. }
  110. // vim: et sw=4 sts=4