PropertyList.php 2.1 KB

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