PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_MyMap.php

Documentation is available at PMO_MyMap.php

  1. <?php
  2. /**
  3.  * This file contains the PMO_MyMap class which implements
  4.  * datastructure to stock, retrieve and filter a PMO_Object.
  5.  *
  6.  * This file is part of the PhpMyObject project.
  7.  * 
  8.  * For questions, help, comments, discussion, etc., please join our
  9.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  10.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  11.  *
  12.  * PhpMyObject is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * @package        PhpMyObject
  26.  * @subpackage PMO_Core
  27.  * @author        Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  28.  * @link            http://pmo.developpez.com/
  29.  * @since        PhpMyObject v0.1x
  30.  * @version        $Revision: $
  31.  * @copyright    Copyright (C) 2007-2008 Nicolas Boiteux
  32.  * @license        GPLv3 {@link http://www.gnu.org/licenses/gpl}
  33.  * @filesource
  34.  *
  35.  */ 
  36.  
  37. /** requires the interface */
  38. require_once(dirname(__FILE__).'/PMO_Map.php');
  39.  
  40. /**
  41.  * This class is a datastructure to stock, retrieve and filter
  42.  * the reference of PMO_Object.
  43.  *
  44.  * Several methods are build to
  45.  * search for an object with criterians. This map is specific
  46.  * to PMO_Object as it use the interface PMO_Object
  47.  *
  48.  * @package        PhpMyObject
  49.  * @subpackage PMO_Core
  50.  */ 
  51. class PMO_MyMap implements PMO_Map{
  52.  
  53.     /**
  54.     * holds an array of PMO_Object in the from of
  55.     * tablename => PMO_Object
  56.     *
  57.     * @var ArrayIterator from SPL
  58.     */
  59.     protected $map;
  60.     
  61.     public function __construct(){
  62.         $this->map = new PMO_MyArray();
  63.     }
  64.  
  65.     /**
  66.     * Adds an array of PMO_Object to the current map in the form of
  67.     * tablename=>PMO_Object
  68.     * 
  69.     * @param array $row          an array of PMO_Object
  70.     * @return void 
  71.     */
  72.     public function add(array $row){
  73.         $this->map->append($row);    
  74.     }
  75.  
  76.     /**
  77.     * returns a new map from the current map using a PMO_Object as filter
  78.     *
  79.     * this new map only contains rows that are relative to the passed in object
  80.     * 
  81.     * @param object $object          a PMO_Object
  82.     * @return PMO_Map 
  83.     * @throws Exception              if there is no PMO_Object for the same table name
  84.     */
  85.     public function getMapByObject(PMO_Object $object){
  86.         $oldmap $this->get();
  87.         $newmap new PMO_MyMap;
  88.         $tablename $object->getTable()->getTableName();
  89.         foreach($oldmap as $line)
  90.             if($line[$tablename=== $object)
  91.                 $newmap->add($line);
  92.                 
  93.         if($newmap->count()>0)
  94.             return $newmap;
  95.         else
  96.             throw new Exception("Error: Map doesn't contain ".$tablename);
  97.     }
  98.     
  99.     /**
  100.     * returns a new map from the current map using a PMO_Object
  101.     * primary keys as filter
  102.     *
  103.     * this new map will only contain rows that match the filter
  104.     * 
  105.     * Other values can be NULL as they are not used.
  106.     * 
  107.     * @param object          PMO_Object to use as filter
  108.     * @return PMO_Map 
  109.     * @throws Exception      if no object is found
  110.     */
  111.     public function getMapByObjectByValue(PMO_Object $object){
  112.         $oldmap $this->get();        
  113.         $newmap new PMO_MyMap;
  114.         $table $object->getTable();
  115.         $tablename $table->getTableName();
  116.         $arrayofpk $table->getPk();
  117.         
  118.         foreach($oldmap as $line){
  119.             $flag TRUE;
  120.             foreach($arrayofpk as $pk){
  121.                 if($line[$tablename]->$pk != $object->$pk)
  122.                     $flag FALSE;
  123.                     break;
  124.                 }
  125.             }
  126.  
  127.             if($flag)
  128.                 $newmap->add($line);
  129.         }
  130.         
  131.         if($newmap->count()>0)
  132.             return $newmap;
  133.         else
  134.             throw new Exception("Error: Map doesn't contain ".$tablename);
  135.     }
  136.     
  137.     /**
  138.     * Alias of {@see getMapByObject()}
  139.     *
  140.     * @param object $object          a PMO_Object to use as filter
  141.     * @return PMO_Map 
  142.     */
  143.     public function getMapLinked(PMO_Object $object){
  144.         return $this->getMapByObject($object);
  145.     }
  146.     
  147.     /**
  148.     * build a new map that only contains objects of type tablename
  149.     * relative to our object.
  150.     * 
  151.     * @param object $object          the PMO_Object to search for
  152.     * @param string $tablename   the table nae for which we want a map
  153.     * @return PMO_Map 
  154.     * @throws Exception
  155.     */
  156.     public function getMapRelated(PMO_Object $object$tablename){
  157.         $map $this->getMapByObject($object);
  158.         return $map->getMapByTable($tablename);
  159.     }
  160.     
  161.     /**
  162.     * returns a new map that only contains objects of type tablename with attribute=value.
  163.     *
  164.     * Search is done only on one fields. Faster than getMapByObjectByValue but less powerfull.
  165.     * 
  166.     * @param string $tablename          table name to search for
  167.     * @param string $attribute          attribute for which we want to check the value
  168.     * @param string $value              value to search for
  169.     * @return PMO_Map 
  170.     * @throws Exception                  if no match has been found
  171.     */
  172.     public function getMapByValue($tablename$attribute$value){
  173.         $oldmap $this->get();        
  174.         foreach($oldmap as $line)
  175.             if($line[$tablename]->get($attribute== $value)
  176.                 return $this->getMapByObject($line[$tablename]);        
  177.                             
  178.         throw new Exception("Error: No object ".$tablename." found");
  179.     }
  180.     
  181.     /**
  182.     * returns a new map that only contains objects of type tablename
  183.     * 
  184.     * @param string $tablename          the table name to search for
  185.     * @return PMO_Map 
  186.     * @throws Exception                  if none has been found
  187.     */
  188.     public function getMapByTable($tablename){
  189.         $oldmap $this->get();        
  190.         $newmap new PMO_MyMap();
  191.         foreach($oldmap as $line){
  192.             if(!isset($line[$tablename]))
  193.                 throw new Exception("Error: Object ".$tablename." not exists");
  194.                 
  195.             $array[$tablename$line[$tablename];
  196.             $newmap->add($array);
  197.         }
  198.         
  199.         if($newmap->count(0)
  200.             return $newmap;
  201.         else
  202.             throw new Exception("Error: No object ".$tablename." found");
  203.     }
  204.  
  205.     /**
  206.      * returns the current map array of PMO_Object
  207.      *  
  208.      * @return array 
  209.      * @throws Exception                if the current map is empty
  210.      */    
  211.     private function get(){
  212.         if(count($this->map== NULL)
  213.             throw new Exception('Error: Map is empty');
  214.         
  215.         return $this->map;        
  216.     }
  217.         
  218.     /**
  219.     * returns one row of the current map array structure
  220.     *
  221.     * Null is returned at the end and the iterator is reset.
  222.     * 
  223.     * The array is not poped, it's only a cursor that move
  224.     * an index and return the results.
  225.     * 
  226.     * @return array 
  227.     */
  228.     public function fetch(){    
  229.         return $this->map->fetch();    
  230.     }
  231.  
  232.     /**
  233.     * returns one PMO_Object of row of the current map array structure
  234.     *
  235.     * Null is returned at the end and the iterator is reset.
  236.     * 
  237.     * The array is not poped, it's only a cursor that move
  238.     * an index and return the results.
  239.     * 
  240.     * @return PMO_Object 
  241.     */    
  242.     public function fetchTable($tablename){    
  243.         $value $this->fetch();
  244.         if($value != NULL)
  245.             return $value[$tablename];
  246.         else
  247.             return NULL;        
  248.     }
  249.     
  250.     /**
  251.     * returns the number of rows in the map
  252.     *
  253.     * @return int 
  254.     */    
  255.     public function count(){
  256.         return $this->map->count();
  257.     }
  258.     
  259.     /**
  260.     * retrieves the first object in map matching with tablename, and attribute=>value
  261.     *
  262.     * If object is not found, returns an exception
  263.     * 
  264.     * @param string $tablename          table name to search for
  265.     * @param string $attribute          attribute for which we want to check the value
  266.     * @param string $value              value to search for
  267.     * @return PMO_Map 
  268.     * @throws Exception                  if no object matches the criteria
  269.     */
  270.     public function getObjectByValue($tablename$attribute$value){
  271.         $oldmap $this->get();
  272.         foreach($oldmap as $line){
  273.             if(!isset($line[$tablename]))
  274.                 throw new Exception("Error: Object ".$tablename." is undefined");
  275.             
  276.             if(strcmp ($line[$tablename]->get($attribute)$value))
  277.                 return $line[$tablename];
  278.         }
  279.         throw new Exception("Error: No object ".$tablename." found");
  280.     }
  281.     
  282.     /**
  283.     * retrieves one object from map using the object primary key as filter
  284.     *
  285.     * All primary keys must be set, it's more powerfull than getObjectByValue
  286.     * but slower too. If object is not found, return an exception
  287.     * 
  288.     * @param object $object          a PMO_Object to serve as filter
  289.     * @return PMO_Object 
  290.     * @throws Exception
  291.     */
  292.     public function getObjectByObject(PMO_Object $object){
  293.         $oldmap $this->get();
  294.         $table $object->getTable();
  295.         $tablename $table->getTableName();
  296.         $tablepk $table->getPk();
  297.         
  298.         foreach($oldmap as $line){
  299.             $flag TRUE;
  300.             foreach($tablepk as $pk){
  301.                 if($line[$tablename]->$pk != $object->$pk)
  302.                     $flag FALSE;
  303.                     break;
  304.                 }
  305.             }
  306.  
  307.             if($flag)
  308.                 return($line[$tablename]);
  309.         }
  310.         throw new Exception("Error: No object ".$tablename." found");
  311.     }
  312.     
  313.     /**
  314.      * Only an exception, for not use PMO_Map as PMO_Object
  315.      * @throws Exception
  316.      */
  317.     public function __get($value{
  318.         throw new Exception("Error: Try to get attribute ".$value." on a PMO_MyMap");
  319.     }
  320.     
  321. }
  322. ?>

Documentation generated on Wed, 15 Oct 2008 09:17:17 -0400 by phpDocumentor 1.4.1