Source for file Map.class.php

Documentation is available at Map.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Map
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Map Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Map
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Map Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Map");
  34.  
  35. class Gumbo_Map implements Gumbo_Interface_Map {
  36.     
  37.     /** @var array $_keys associative array of keys */
  38.     private $_keys = array ();
  39.     
  40.     
  41.     
  42.     /** ACTION METHODS **/
  43.     /**
  44.      * Adds a foreign key reference to the source key
  45.      * @postcondition removes all non-alphanumeric (except underscores) characters from the source key
  46.      * @param string $source_key 
  47.      * @param string $foreign_key 
  48.      * @throws Gumbo_Exception
  49.      */
  50.     public function add ($source_key$foreign_key{
  51.         try {
  52.             // verify precondition
  53.             if (!is_string ($source_key)) {
  54.                 throw new Gumbo_Exception ("Invalid Argument 'source_key:str' => {$source_key}:gettype ($source_key));
  55.             }
  56.             if (!is_string ($foreign_key)) {
  57.                 throw new Gumbo_Exception ("Invalid Argument 'foreign_key:str' => {$foreign_key}:gettype ($foreign_key));
  58.             }
  59.             
  60.             $source_key ereg_replace ("[^A-Za-z0-9_]"""$source_key);
  61.             $this->_keys [$source_key$foreign_key;
  62.         catch (Gumbo_Exception $e{
  63.             $e->setFunction (__METHOD__);
  64.             gumbo_trigger ($e);
  65.         }
  66.     }
  67.     
  68.     /**
  69.      * Removes the foreign key reference to the source key
  70.      * @param string $source_key 
  71.      * @throws Gumbo_Exception
  72.      */
  73.     public function remove ($source_key{
  74.         try {
  75.             // verify precondition
  76.             if (!is_string ($source_key)) {
  77.                 throw new Gumbo_Exception ("Invalid Argument 'source_key:str' => {$source_key}:gettype ($source_key));
  78.             }
  79.             if (!isset ($this->_keys [$source_key])) {
  80.                 throw new Gumbo_Exception ("Source Key Undefined: {$source_key}");
  81.             }
  82.             
  83.             unset ($this->_keys [$source_key]);
  84.         catch (Gumbo_Exception $e{
  85.             $e->setFunction (__METHOD__);
  86.             gumbo_trigger ($e);
  87.         }
  88.     }
  89.     
  90.     /**
  91.      * Resets all keys
  92.      * @postcondition empty key references
  93.      */
  94.     public function reset ({
  95.         $this->_keys = array ();
  96.     }
  97.     
  98.     
  99.     
  100.     /** ACCESSOR METHODS **/
  101.     /**
  102.      * Returns the source key referenced by the foreign key
  103.      * @param string $foreign_key 
  104.      * @return string 
  105.      * @throws Gumbo_Exception
  106.      */
  107.     public function getKey ($foreign_key{
  108.         $source_key null;
  109.         try {
  110.             // verify precondition
  111.             if (!is_string ($source_key)) {
  112.                 throw new Gumbo_Exception ("Invalid Argument 'foreign_key:str' => {$foreign_key}:gettype ($foreign_key));
  113.             }
  114.             if (!$this->isForeignKey ($foreign_key)) {
  115.                 throw new Gumbo_Exception ("Foreign Key Undefined: {$foreign_key}");
  116.             }
  117.             
  118.             foreach ($this->getAll (as $key=>$val{
  119.                 if ($val === $foreign_key{
  120.                     $source_key $key;
  121.                     break;
  122.                 }
  123.             }
  124.         catch (Gumbo_Exception $e{
  125.             $e->setFunction (__METHOD__);
  126.             gumbo_trigger ($e);
  127.         }
  128.         return $source_key;
  129.     }
  130.     
  131.     /**
  132.      * Returns the foreign key reference to the source key
  133.      * @param string $source_key 
  134.      * @return string 
  135.      * @throws Gumbo_Exception
  136.      */
  137.     public function getForeignKey ($source_key{
  138.         try {
  139.             // verify precondition
  140.             if (!is_string ($source_key)) {
  141.                 throw new Gumbo_Exception ("Invalid Argument 'source_key:str' => {$source_key}:gettype ($source_key));
  142.             }
  143.             if (!$this->isKey ($source_key)) {
  144.                 throw new Gumbo_Exception ("Source Key Undefined: {$source_key}");
  145.             }
  146.             
  147.             return $this->_keys [$source_key];
  148.         catch (Gumbo_Exception $e{
  149.             $e->setFunction (__METHOD__);
  150.             gumbo_trigger ($e);
  151.         }
  152.         return null;
  153.     }
  154.     
  155.     /**
  156.      * Returns all the keys as an associative array where key=>source_key, val=>foreign_key
  157.      * @return array 
  158.      */
  159.     public function getAll ({
  160.         return $this->_keys;
  161.     }
  162.     
  163.     /**
  164.      * Returns if the source key exists
  165.      * @param string $source_key 
  166.      * @return bool 
  167.      * @throws Gumbo_Exception
  168.      */
  169.     public function isKey ($source_key{
  170.         try {
  171.             // verify precondition
  172.             if (!is_string ($source_key)) {
  173.                 throw new Gumbo_Exception ("Invalid Argument 'source_key:str' => {$source_key}:gettype ($source_key));
  174.             }
  175.             
  176.             return isset ($this->_keys [$source_key]);
  177.         catch (Gumbo_Exception $e{
  178.             $e->setFunction (__METHOD__);
  179.             gumbo_trigger ($e);
  180.         }
  181.         return false;
  182.     }
  183.     
  184.     /**
  185.      * Returns if the foreign key exists
  186.      * @param string $foreign_key 
  187.      * @return bool 
  188.      * @throws Gumbo_Exception
  189.      */
  190.     public function isForeignKey ($foreign_key{
  191.         $found false;
  192.         try {
  193.             // verify precondition
  194.             if (!is_string ($foreign_key)) {
  195.                 throw new Gumbo_Exception ("Invalid Argument 'foreign_key:str' => {$foreign_key}:gettype ($foreign_key));
  196.             }
  197.             
  198.             foreach ($this->getAll (as $val{
  199.                 if ($val === $foreign_key{
  200.                     $found true;
  201.                     break;
  202.                 }
  203.             }
  204.         catch (Gumbo_Exception $e{
  205.             $e->setFunction (__METHOD__);
  206.             gumbo_trigger ($e);
  207.         }
  208.         return $found;
  209.     }
  210.     
  211. }
  212.  
  213. ?>