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

Documentation generated on Sun, 07 Feb 2010 17:53:32 +0000 by phpDocumentor 1.4.0