Source for file Flyweight.class.php

Documentation is available at Flyweight.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 Record
  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.  * Record Flyweight Class
  22.  * 
  23.  * This class will hold a single Gumbo_Record object, reloading the contents
  24.  * with new data supplied by looping through a list of database query rows.  Simply
  25.  * loop through the results of a query, passing the next row into this class.
  26.  * The Record object will be properly loaded.
  27.  * 
  28.  * <pre>
  29.  * $fly = new Gumbo_Record_Flyweight (new Gumbo_Record ()); // any Gumbo_Interface_Record Class
  30.  * foreach (db_query ("some query") as $res) {
  31.  *         $record = $fly->get ($res);
  32.  *         // do something
  33.  *         ...
  34.  * }
  35.  * </pre>
  36.  *
  37.  * @category Gumbo
  38.  * @package Record
  39.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  40.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  41.  * @author Michael Luster <mluster79@yahoo.com>
  42.  * @link http://sourceforge.net/projects/phpgumbo
  43.  * @desc Record Flyweight Class
  44.  * @version 0.0.1
  45.  */
  46.  
  47. gumbo_load ("Interface_Flyweight");
  48. gumbo_load ("Record");
  49.  
  50. class Gumbo_Record_Flyweight implements Gumbo_Interface_Flyweight {
  51.     
  52.     /** @var Gumbo_Interface_Record $_record */
  53.     private $_record;
  54.     
  55.     
  56.     
  57.     /**
  58.      * Constructor
  59.      * @param Gumbo_Interface_Record $record Record class to implement (defaults to Gumbo_Record)
  60.      * @uses Gumbo_Record
  61.      */
  62.     public function __construct ($record=null{
  63.         if (is_null ($record)) {
  64.             $record new Gumbo_Record ();
  65.         }
  66.         $this->setRecord ($record);
  67.     }
  68.     
  69.     
  70.     
  71.     /** MUTATOR METHODS **/
  72.     /**
  73.      * Sets the Record object to use
  74.      * @param Gumbo_Interface_Record $record 
  75.      */
  76.     public function setRecord (Gumbo_Interface_Record $record{
  77.         $this->_record = $record;
  78.     }
  79.     
  80.     
  81.     
  82.     /** ACCESSOR METHODS **/
  83.     /**
  84.      * Returns the Record object, re-loading with the new data
  85.      * @param array $data associative array of data
  86.      * @return Gumbo_Interface_Record 
  87.      */
  88.     public function get ($data=null{
  89.         if (is_null ($data)) {
  90.             return $this->_record;
  91.         }
  92.         
  93.         try {
  94.             // verify precondition
  95.             if (!is_array ($data)) {
  96.                 throw new Gumbo_Exception ("Invalid Argument 'data:arr' => {$data}:gettype ($data));
  97.             }
  98.             
  99.             $this->_record->unload (true);
  100.             $this->_record->setPrimaryValue ($data);
  101.             $this->_record->load ();
  102.             
  103.             return $this->_record;
  104.         catch (Gumbo_Exception $e{
  105.             $e->setFunction (__METHOD__);
  106.             gumbo_trigger ($e);
  107.         }
  108.     }
  109.     
  110. }
  111.  
  112. ?>