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

0001 <?php
0002 
0003 /**
0004  * Generic property list implementation
0005  */
0006 class HTMLPurifier_PropertyList
0007 {
0008     /**
0009      * Internal data-structure for properties.
0010      * @type array
0011      */
0012     protected $data = array();
0013 
0014     /**
0015      * Parent plist.
0016      * @type HTMLPurifier_PropertyList
0017      */
0018     protected $parent;
0019 
0020     /**
0021      * Cache.
0022      * @type array
0023      */
0024     protected $cache;
0025 
0026     /**
0027      * @param HTMLPurifier_PropertyList $parent Parent plist
0028      */
0029     public function __construct($parent = null)
0030     {
0031         $this->parent = $parent;
0032     }
0033 
0034     /**
0035      * Recursively retrieves the value for a key
0036      * @param string $name
0037      * @throws HTMLPurifier_Exception
0038      */
0039     public function get($name)
0040     {
0041         if ($this->has($name)) {
0042             return $this->data[$name];
0043         }
0044         // possible performance bottleneck, convert to iterative if necessary
0045         if ($this->parent) {
0046             return $this->parent->get($name);
0047         }
0048         throw new HTMLPurifier_Exception("Key '$name' not found");
0049     }
0050 
0051     /**
0052      * Sets the value of a key, for this plist
0053      * @param string $name
0054      * @param mixed $value
0055      */
0056     public function set($name, $value)
0057     {
0058         $this->data[$name] = $value;
0059     }
0060 
0061     /**
0062      * Returns true if a given key exists
0063      * @param string $name
0064      * @return bool
0065      */
0066     public function has($name)
0067     {
0068         return array_key_exists($name, $this->data);
0069     }
0070 
0071     /**
0072      * Resets a value to the value of it's parent, usually the default. If
0073      * no value is specified, the entire plist is reset.
0074      * @param string $name
0075      */
0076     public function reset($name = null)
0077     {
0078         if ($name == null) {
0079             $this->data = array();
0080         } else {
0081             unset($this->data[$name]);
0082         }
0083     }
0084 
0085     /**
0086      * Squashes this property list and all of its property lists into a single
0087      * array, and returns the array. This value is cached by default.
0088      * @param bool $force If true, ignores the cache and regenerates the array.
0089      * @return array
0090      */
0091     public function squash($force = false)
0092     {
0093         if ($this->cache !== null && !$force) {
0094             return $this->cache;
0095         }
0096         if ($this->parent) {
0097             return $this->cache = array_merge($this->parent->squash($force), $this->data);
0098         } else {
0099             return $this->cache = $this->data;
0100         }
0101     }
0102 
0103     /**
0104      * Returns the parent plist.
0105      * @return HTMLPurifier_PropertyList
0106      */
0107     public function getParent()
0108     {
0109         return $this->parent;
0110     }
0111 
0112     /**
0113      * Sets the parent plist.
0114      * @param HTMLPurifier_PropertyList $plist Parent plist
0115      */
0116     public function setParent($plist)
0117     {
0118         $this->parent = $plist;
0119     }
0120 }
0121 
0122 // vim: et sw=4 sts=4