| 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 |
* @type array |
| 16 |
*/ |
| 17 |
private $_storage = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* Registers a variable into the context. |
| 21 |
* @param string $name String name |
| 22 |
* @param mixed $ref Reference to variable to be registered |
| 23 |
*/ |
| 24 |
public function register($name, &$ref) |
| 25 |
{ |
| 26 |
if (array_key_exists($name, $this->_storage)) { |
| 27 |
throw new Exception("Name $name produces collision, cannot re-register"); |
| 28 |
} |
| 29 |
$this->_storage[$name] =& $ref; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Retrieves a variable reference from the context. |
| 34 |
* @param string $name String name |
| 35 |
* @param bool $ignore_error Boolean whether or not to ignore error |
| 36 |
* @return mixed |
| 37 |
*/ |
| 38 |
public function &get($name, $ignore_error = false) |
| 39 |
{ |
| 40 |
if (!array_key_exists($name, $this->_storage)) { |
| 41 |
if (!$ignore_error) { |
| 42 |
throw new Exception("Attempted to retrieve non-existent variable $name"); |
| 43 |
} |
| 44 |
$var = null; // so we can return by reference |
| 45 |
return $var; |
| 46 |
} |
| 47 |
return $this->_storage[$name]; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Destroys a variable in the context. |
| 52 |
* @param string $name String name |
| 53 |
*/ |
| 54 |
public function destroy($name) |
| 55 |
{ |
| 56 |
if (!array_key_exists($name, $this->_storage)) { |
| 57 |
throw new Exception("Attempted to destroy non-existent variable $name"); |
| 58 |
} |
| 59 |
unset($this->_storage[$name]); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Checks whether or not the variable exists. |
| 64 |
* @param string $name String name |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function exists($name) |
| 68 |
{ |
| 69 |
return array_key_exists($name, $this->_storage); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Loads a series of variables from an associative array |
| 74 |
* @param array $context_array Assoc array of variables to load |
| 75 |
*/ |
| 76 |
public function loadArray($context_array) |
| 77 |
{ |
| 78 |
foreach ($context_array as $key => $discard) { |
| 79 |
$this->register($key, $context_array[$key]); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// vim: et sw=4 sts=4 |