]>
Commit | Line | Data |
---|---|---|
d4949327 NL |
1 | <?php\r |
2 | \r | |
3 | /**\r | |
4 | * Registry object that contains information about the current context.\r | |
5 | * @warning Is a bit buggy when variables are set to null: it thinks\r | |
6 | * they don't exist! So use false instead, please.\r | |
7 | * @note Since the variables Context deals with may not be objects,\r | |
8 | * references are very important here! Do not remove!\r | |
9 | */\r | |
10 | class HTMLPurifier_Context\r | |
11 | {\r | |
12 | \r | |
13 | /**\r | |
14 | * Private array that stores the references.\r | |
15 | * @type array\r | |
16 | */\r | |
17 | private $_storage = array();\r | |
18 | \r | |
19 | /**\r | |
20 | * Registers a variable into the context.\r | |
21 | * @param string $name String name\r | |
22 | * @param mixed $ref Reference to variable to be registered\r | |
23 | */\r | |
24 | public function register($name, &$ref)\r | |
25 | {\r | |
26 | if (array_key_exists($name, $this->_storage)) {\r | |
27 | trigger_error(\r | |
28 | "Name $name produces collision, cannot re-register",\r | |
29 | E_USER_ERROR\r | |
30 | );\r | |
31 | return;\r | |
32 | }\r | |
33 | $this->_storage[$name] =& $ref;\r | |
34 | }\r | |
35 | \r | |
36 | /**\r | |
37 | * Retrieves a variable reference from the context.\r | |
38 | * @param string $name String name\r | |
39 | * @param bool $ignore_error Boolean whether or not to ignore error\r | |
40 | * @return mixed\r | |
41 | */\r | |
42 | public function &get($name, $ignore_error = false)\r | |
43 | {\r | |
44 | if (!array_key_exists($name, $this->_storage)) {\r | |
45 | if (!$ignore_error) {\r | |
46 | trigger_error(\r | |
47 | "Attempted to retrieve non-existent variable $name",\r | |
48 | E_USER_ERROR\r | |
49 | );\r | |
50 | }\r | |
51 | $var = null; // so we can return by reference\r | |
52 | return $var;\r | |
53 | }\r | |
54 | return $this->_storage[$name];\r | |
55 | }\r | |
56 | \r | |
57 | /**\r | |
58 | * Destroys a variable in the context.\r | |
59 | * @param string $name String name\r | |
60 | */\r | |
61 | public function destroy($name)\r | |
62 | {\r | |
63 | if (!array_key_exists($name, $this->_storage)) {\r | |
64 | trigger_error(\r | |
65 | "Attempted to destroy non-existent variable $name",\r | |
66 | E_USER_ERROR\r | |
67 | );\r | |
68 | return;\r | |
69 | }\r | |
70 | unset($this->_storage[$name]);\r | |
71 | }\r | |
72 | \r | |
73 | /**\r | |
74 | * Checks whether or not the variable exists.\r | |
75 | * @param string $name String name\r | |
76 | * @return bool\r | |
77 | */\r | |
78 | public function exists($name)\r | |
79 | {\r | |
80 | return array_key_exists($name, $this->_storage);\r | |
81 | }\r | |
82 | \r | |
83 | /**\r | |
84 | * Loads a series of variables from an associative array\r | |
85 | * @param array $context_array Assoc array of variables to load\r | |
86 | */\r | |
87 | public function loadArray($context_array)\r | |
88 | {\r | |
89 | foreach ($context_array as $key => $discard) {\r | |
90 | $this->register($key, $context_array[$key]);\r | |
91 | }\r | |
92 | }\r | |
93 | }\r | |
94 | \r | |
95 | // vim: et sw=4 sts=4\r |