File indexing completed on 2024-12-22 05:36:21

0001 <?php
0002 
0003 /**
0004  * Registry object that contains information about the current context.
0005  * @warning Is a bit buggy when variables are set to null: it thinks
0006  *          they don't exist! So use false instead, please.
0007  * @note Since the variables Context deals with may not be objects,
0008  *       references are very important here! Do not remove!
0009  */
0010 class HTMLPurifier_Context
0011 {
0012 
0013     /**
0014      * Private array that stores the references.
0015      * @type array
0016      */
0017     private $_storage = array();
0018 
0019     /**
0020      * Registers a variable into the context.
0021      * @param string $name String name
0022      * @param mixed $ref Reference to variable to be registered
0023      */
0024     public function register($name, &$ref)
0025     {
0026         if (array_key_exists($name, $this->_storage)) {
0027             trigger_error(
0028                 "Name $name produces collision, cannot re-register",
0029                 E_USER_ERROR
0030             );
0031             return;
0032         }
0033         $this->_storage[$name] =& $ref;
0034     }
0035 
0036     /**
0037      * Retrieves a variable reference from the context.
0038      * @param string $name String name
0039      * @param bool $ignore_error Boolean whether or not to ignore error
0040      * @return mixed
0041      */
0042     public function &get($name, $ignore_error = false)
0043     {
0044         if (!array_key_exists($name, $this->_storage)) {
0045             if (!$ignore_error) {
0046                 trigger_error(
0047                     "Attempted to retrieve non-existent variable $name",
0048                     E_USER_ERROR
0049                 );
0050             }
0051             $var = null; // so we can return by reference
0052             return $var;
0053         }
0054         return $this->_storage[$name];
0055     }
0056 
0057     /**
0058      * Destroys a variable in the context.
0059      * @param string $name String name
0060      */
0061     public function destroy($name)
0062     {
0063         if (!array_key_exists($name, $this->_storage)) {
0064             trigger_error(
0065                 "Attempted to destroy non-existent variable $name",
0066                 E_USER_ERROR
0067             );
0068             return;
0069         }
0070         unset($this->_storage[$name]);
0071     }
0072 
0073     /**
0074      * Checks whether or not the variable exists.
0075      * @param string $name String name
0076      * @return bool
0077      */
0078     public function exists($name)
0079     {
0080         return array_key_exists($name, $this->_storage);
0081     }
0082 
0083     /**
0084      * Loads a series of variables from an associative array
0085      * @param array $context_array Assoc array of variables to load
0086      */
0087     public function loadArray($context_array)
0088     {
0089         foreach ($context_array as $key => $discard) {
0090             $this->register($key, $context_array[$key]);
0091         }
0092     }
0093 }
0094 
0095 // vim: et sw=4 sts=4