Source for file Data.php

Documentation is available at Data.php

  1. <?php
  2.  
  3. /**
  4.  * dwoo data object, use it for complex data assignments or if you want to easily pass it
  5.  * around multiple functions to avoid passing an array by reference
  6.  *
  7.  * This software is provided 'as-is', without any express or implied warranty.
  8.  * In no event will the authors be held liable for any damages arising from the use of this software.
  9.  *
  10.  * This file is released under the LGPL
  11.  * "GNU Lesser General Public License"
  12.  * More information can be found here:
  13.  * {@link http://www.gnu.org/copyleft/lesser.html}
  14.  *
  15.  * @author     Jordi Boggiano <[email protected]>
  16.  * @copyright  Copyright (c) 2008, Jordi Boggiano
  17.  * @license    http://www.gnu.org/copyleft/lesser.html  GNU Lesser General Public License
  18.  * @link       http://dwoo.org/
  19.  * @version    0.9.1
  20.  * @date       2008-05-30
  21.  * @package    Dwoo
  22.  */
  23. class Dwoo_Data implements Dwoo_IDataProvider
  24. {
  25.     /**
  26.      * data array
  27.      *
  28.      * @var array 
  29.      */
  30.     protected $data = array();
  31.  
  32.     /**
  33.      * returns the data array
  34.      *
  35.      * @return array 
  36.      */
  37.     public function getData()
  38.     {
  39.         return $this->data;
  40.     }
  41.  
  42.     /**
  43.      * clears a the entire data or only the given key
  44.      *
  45.      * @param array|string$name clears only one value if you give a name, multiple values if
  46.      *                                you give an array of names, or the entire data if left null
  47.      */
  48.     public function clear($name null)
  49.     {
  50.         if ($name === null{
  51.             $this->data = array();
  52.         elseif (is_array($name)) {
  53.             foreach ($name as $index)
  54.                 unset($this->data[$index]);
  55.         else {
  56.             unset($this->data[$name]);
  57.         }
  58.     }
  59.  
  60.     /**
  61.      * overwrites the entire data with the given array
  62.      *
  63.      * @param array $data the new data array to use
  64.      */
  65.     public function setData(array $data)
  66.     {
  67.         $this->data $data;
  68.     }
  69.  
  70.     /**
  71. /**
  72.      * merges the given array(s) with the current data with array_merge
  73.      *
  74.      * @param array $data the array to merge
  75.      * @param array $data2 $data3 ... other arrays to merge, optional, etc.
  76.      */
  77.     public function mergeData(array $data)
  78.     {
  79.         $args func_get_args();
  80.         while (list(,$veach($args)) {
  81.             if (is_array($v)) {
  82.                 $this->data array_merge($this->data$v);
  83.             }
  84.         }
  85.     }
  86.  
  87.     /**
  88.      * assigns a value or an array of values to the data object
  89.      *
  90.      * @param array|string$name an associative array of multiple (index=>value) or a string
  91.      *                         that is the index to use, i.e. a value assigned to "foo" will be
  92.      *                         accessible in the template through {$foo}
  93.      * @param mixed $val the value to assign, or null if $name was an array
  94.      */
  95.     public function assign($name$val null)
  96.     {
  97.         if (is_array($name)) {
  98.             reset($name);
  99.             while (list($k,$veach($name))
  100.                 $this->data[$k$v;
  101.         else {
  102.             $this->data[$name$val;
  103.         }
  104.     }
  105.        
  106.        /**
  107.         * allows to assign variables using the object syntax
  108.         * 
  109.         * @param string $name the variable name
  110.         * @param string $value the value to assign to it
  111.         */
  112.        public function __set($name$value)
  113.        {
  114.            $this->assign($name$value);
  115.        }
  116.  
  117.     /**
  118.      * assigns a value by reference to the data object
  119.      *
  120.      * @param string $name the index to use, i.e. a value assigned to "foo" will be
  121.      *                         accessible in the template through {$foo}
  122.      * @param mixed $val the value to assign by reference
  123.      */
  124.     public function assignByRef($name&$val)
  125.     {
  126.         $this->data[$name=$val;
  127.     }
  128.  
  129.     /**
  130.      * appends values or an array of values to the data object
  131.      *
  132.      * @param array|string$name an associative array of multiple (index=>value) or a string
  133.      *                         that is the index to use, i.e. a value assigned to "foo" will be
  134.      *                         accessible in the template through {$foo}
  135.      * @param mixed $val the value to assign, or null if $name was an array
  136.      * @param bool $merge true to merge data or false to append, defaults to false
  137.      */
  138.        public function append($name$val null$merge false)
  139.        {
  140.            if (is_array($name)) {
  141.             foreach ($name as $key=>$val{
  142.                 if (isset($this->data[$key]&& !is_array($this->data[$key])) {
  143.                     settype($this->data[$key]'array');
  144.                 }
  145.  
  146.                 if ($merge === true && is_array($val)) {
  147.                     $this->data[$key$val $this->data[$key];
  148.                 else {
  149.                     $this->data[$key][$val;
  150.                 }
  151.             }
  152.            elseif ($val !== null{
  153.             if (isset($this->data[$name]&& !is_array($this->data[$name])) {
  154.                 settype($this->data[$name]'array');
  155.             }
  156.  
  157.             if ($merge === true && is_array($val)) {
  158.                 $this->data[$name$val $this->data[$name];
  159.             else {
  160.                 $this->data[$name][$val;
  161.             }
  162.            }
  163.        }
  164.  
  165.     /**
  166.      * appends a value by reference to the data object
  167.      *
  168.      * @param string $name the index to use, i.e. a value assigned to "foo" will be
  169.      *                         accessible in the template through {$foo}
  170.      * @param mixed $val the value to append by reference
  171.      * @param bool $merge true to merge data or false to append, defaults to false
  172.      */
  173.        public function appendByRef($name&$val$merge false)
  174.        {
  175.            if (isset($this->data[$name]&& !is_array($this->data[$name])) {
  176.             settype($this->data[$name]'array');
  177.         }
  178.  
  179.            if ($merge === true && is_array($val)) {
  180.                foreach ($val as $key => &$val{
  181.                    $this->data[$name][$key=$val;
  182.                }
  183.            else {
  184.                $this->data[$name][=$val;
  185.            }
  186.        }
  187.        
  188.        /**
  189.         * returns true if the variable has been assigned already, false otherwise
  190.         * 
  191.         * @param string $name the variable name
  192.         * @return bool 
  193.         */
  194.        public function isAssigned($name)
  195.        {
  196.            return isset($this->data[$name]);
  197.        }
  198.        
  199.        /**
  200.         * supports calls to isset($dwooData->var)
  201.         * 
  202.         * @param string $name the variable name
  203.         */
  204.        public function __isset($name)
  205.        {
  206.            return isset($this->data[$name]);
  207.        }
  208.        
  209.        /**
  210.         * unassigns/removes a variable
  211.         * 
  212.         * @param string $name the variable name
  213.         */
  214.        public function unassign($name)
  215.        {
  216.            unset($this->data[$name]);
  217.        }
  218.        
  219.        /**
  220.         * supports unsetting variables using the object syntax
  221.         * 
  222.         * @param string $name the variable name
  223.         */
  224.        public function __unset($name)
  225.        {
  226.            unset($this->data[$name]);
  227.        }
  228.        
  229.        /**
  230.         * returns a variable if it was assigned
  231.         * 
  232.         * @param string $name the variable name
  233.         * @return mixed 
  234.         */
  235.        public function get($name)
  236.        {
  237.            return $this->__get($name);
  238.        }
  239.  
  240.        /**
  241.         * allows to read variables using the object syntax
  242.         * 
  243.         * @param string $name the variable name
  244.         * @return mixed 
  245.         */
  246.        public function __get($name)
  247.        {
  248.            if (isset($this->data[$name])) {
  249.                return $this->data[$name];
  250.            else {
  251.                throw new Dwoo_Exception('Tried to read a value that was not assigned yet : "'.$name.'"');
  252.            }
  253.        }
  254. }

Documentation generated on Sun, 03 Aug 2008 15:12:27 +0200 by phpDocumentor 1.4.0