Source for file class.mparraymanager.php

Documentation is available at class.mparraymanager.php

  1. <?php
  2. /**
  3.  * Array access management classes.
  4.  * Basics to provide another way to access to arrays.
  5.  *
  6.  * Includes following classes:
  7.  *
  8.  * - class mpArrayManager_Exception
  9.  *   Exception handler
  10.  *
  11.  * - interface mpIArrayManager
  12.  *   Interface definition
  13.  *
  14.  * - class mpArrayManager
  15.  *   Abstract array management class
  16.  *
  17.  * @author      Murat Purc <murat@purc.de>
  18.  * @copyright   © Murat Purc 2008
  19.  * @date        07.10.2008
  20.  * @package     Development
  21.  * @subpackage  Array_Management
  22.  */
  23.  
  24.  
  25. defined('CON_FRAMEWORK') or die('Illegal call');
  26.  
  27.  
  28. /**
  29.  * mpArrayManager exception, extends Exception
  30.  *
  31.  * @author      Murat Purc <murat@purc.de>
  32.  * @package     Development
  33.  * @subpackage  Array_Management
  34.  */
  35. class mpArrayManager_Exception extends Exception {}
  36.  
  37.  
  38. /**
  39.  * Interface for array manager
  40.  *
  41.  * @author      Murat Purc <murat@purc.de>
  42.  * @package     Development
  43.  * @subpackage  Array_Management
  44.  */
  45. interface mpIArrayManager {
  46.  
  47.     /**
  48.      * Checks if an entry exists.
  49.      *
  50.      * @param   string  $path  The path (array structure) to the entry
  51.      * @return  bool    True if exists, otherwhise false
  52.      */
  53.     public function exists($path);
  54.  
  55.     /**
  56.      * Returns the value by passed path.
  57.      *
  58.      * @param   string  $path  Path (array structure) to the entry
  59.      * @param   string  $default  Default value to return, if no entry exists.
  60.      * @return  mixed   The value
  61.      */
  62.     public function get($path, $default=null);
  63.  
  64.     /**
  65.      * Sets the value by passed path.
  66.      *
  67.      * @param   string  $path  Path (array structure) to the entry
  68.      * @param   string  $value  New value to set.
  69.      */
  70.     public function set($path, $value);
  71.  
  72. }
  73.  
  74.  
  75. /**
  76.  * Abstract array manager class.
  77.  *
  78.  * Provides access to multidimensional arrays up to deepness of 4 levels.
  79.  *
  80.  * @author      Murat Purc <murat@purc.de>
  81.  * @package     Development
  82.  * @subpackage  Array_Management
  83.  * @todo        Comment code
  84.  */
  85. abstract class mpArrayManager implements mpIArrayManager {
  86.  
  87.     /**
  88.      * Contains the array to manage.
  89.      * @var  array 
  90.      */
  91.     protected $_aData;
  92.  
  93.     /**
  94.      * The delemiter beeing used to access to array structure.
  95.      * @var  string 
  96.      */
  97.     protected $_delem = '/';
  98.  
  99.     /**
  100.      * Instance of child class.
  101.      * @var  mpIArrayManager 
  102.      */
  103.     protected static $_instance;
  104.  
  105.  
  106.     public function exists($path) {
  107.         $a = $this->_getSections($path);
  108.  
  109.         $count = count($a);
  110.  
  111.         if ($count == 1 && is_array($this->_aData) && isset($this->_aData[$a[0]])) {
  112.             // 1. level
  113.             return true;
  114.         } elseif ($count == 2 && is_array($this->_aData[$a[0]]) && isset($this->_aData[$a[0]][$a[1]])) {
  115.             // 2. level
  116.             return true;
  117.         } elseif ($count == 3 && is_array($this->_aData[$a[0]][$a[1]]) && isset($this->_aData[$a[0]][$a[1]][$a[2]])) {
  118.             // 3. level
  119.             return true;
  120.         } elseif ($count == 4 && is_array($this->_aData[$a[0]][$a[1]][$a[2]]) && isset($this->_aData[$a[0]][$a[1]][$a[2]][$a[3]])) {
  121.             // 4. level
  122.             return true;
  123.         }
  124.  
  125.         return false;
  126.     }
  127.  
  128.  
  129.     public function get($path, $default=null) {
  130.         $a = $this->_getSections($path);
  131.  
  132.         $count = count($a);
  133.  
  134.         if ($count == 1 && is_array($this->_aData) && isset($this->_aData[$a[0]])) {
  135.             // 1. level
  136.             return $this->_aData[$a[0]];
  137.         } elseif ($count == 2 && is_array($this->_aData[$a[0]]) && isset($this->_aData[$a[0]][$a[1]])) {
  138.             // 2. level
  139.             return $this->_aData[$a[0]][$a[1]];
  140.         } elseif ($count == 3 && is_array($this->_aData[$a[0]][$a[1]]) && isset($this->_aData[$a[0]][$a[1]][$a[2]])) {
  141.             // 3. level
  142.             return $this->_aData[$a[0]][$a[1]][$a[2]];
  143.         } elseif ($count == 4 && is_array($this->_aData[$a[0]][$a[1]][$a[2]]) && isset($this->_aData[$a[0]][$a[1]][$a[2]][$a[3]])) {
  144.             // 4. level
  145.             return $this->_aData[$a[0]][$a[1]][$a[2]][$a[3]];
  146.         }
  147.  
  148.         // return default value
  149.         return $default;
  150.     }
  151.  
  152.  
  153.     public function set($path, $value) {
  154.         $a = $this->_getSections($path);
  155.  
  156.         // pre check of section depth
  157.         $count = count($a);
  158.  
  159.         if ($count > 1 && !isset($this->_aData[$a[0]])) {
  160.             $this->_aData[$a[0]] = array();
  161.         }
  162.  
  163.         if ($count > 2 && !isset($this->_aData[$a[0]][$a[1]])) {
  164.             $this->_aData[$a[0]][$a[1]] = array();
  165.         }
  166.  
  167.         if ($count > 3 && !isset($this->_aData[$a[0]][$a[1]][$a[2]])) {
  168.             $this->_aData[$a[0]][$a[1]][$a[2]] = array();
  169.         }
  170.  
  171.         if ($count == 1) {
  172.             // set at 1. level
  173.             $this->_aData[$a[0]] = $value;
  174.         } elseif ($count == 2) {
  175.             // set at 2. level
  176.             $this->_aData[$a[0]][$a[1]] = $value;
  177.         } elseif ($count == 3) {
  178.             // set at 3. level
  179.             $this->_aData[$a[0]][$a[1]][$a[2]] = $value;
  180.         } else {
  181.             // set at 4. level
  182.             $this->_aData[$a[0]][$a[1]][$a[2]][$a[3]] = $value;
  183.         }
  184.     }
  185.  
  186.  
  187.     private function _getSections($path) {
  188.         $aSections = explode($this->_delem, $path);
  189.         if (count($aSections) == 0) {
  190.             throw new mpArrayManager_Exception('Invalid section path.');
  191.         } elseif (count($aSections) > 4) {
  192.             throw new mpArrayManager_Exception('Maximum provided section depth (4) is overrun.');
  193.         }
  194.         foreach ($aSections as $pos => $value) {
  195.             if ((string) $value === '') {
  196.                 throw new mpArrayManager_Exception('Configuration section must be a non-empty string.');
  197.             }
  198.         }
  199.         return $aSections;
  200.     }
  201.  
  202. }

Documentation generated on Sun, 18 Jan 2009 22:21:20 +0100 by phpDocumentor 1.4.1