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