Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / library / HTMLPurifier / Context.php
bloba78a6fb6f6329286325590b4b4791360bb3cfe60
1 <?php
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 */
8 class HTMLPurifier_Context
11 /**
12 * Private array that stores the references.
13 * @private
15 var $_storage = array();
17 /**
18 * Registers a variable into the context.
19 * @param $name String name
20 * @param $ref Variable to be registered
22 function register($name, &$ref) {
23 if (isset($this->_storage[$name])) {
24 trigger_error("Name $name produces collision, cannot re-register",
25 E_USER_ERROR);
26 return;
28 $this->_storage[$name] =& $ref;
31 /**
32 * Retrieves a variable reference from the context.
33 * @param $name String name
34 * @param $ignore_error Boolean whether or not to ignore error
36 function &get($name, $ignore_error = false) {
37 if (!isset($this->_storage[$name])) {
38 if (!$ignore_error) {
39 trigger_error("Attempted to retrieve non-existent variable $name",
40 E_USER_ERROR);
42 $var = null; // so we can return by reference
43 return $var;
45 return $this->_storage[$name];
48 /**
49 * Destorys a variable in the context.
50 * @param $name String name
52 function destroy($name) {
53 if (!isset($this->_storage[$name])) {
54 trigger_error("Attempted to destroy non-existent variable $name",
55 E_USER_ERROR);
56 return;
58 unset($this->_storage[$name]);
61 /**
62 * Checks whether or not the variable exists.
63 * @param $name String name
65 function exists($name) {
66 return isset($this->_storage[$name]);
69 /**
70 * Loads a series of variables from an associative array
71 * @param $context_array Assoc array of variables to load
73 function loadArray(&$context_array) {
74 foreach ($context_array as $key => $discard) {
75 $this->register($key, $context_array[$key]);