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