Source for file Repository.php

Documentation is available at Repository.php

  1. <?php
  2. /*
  3.  *  $Id: Repository.php 1080 2007-02-10 18:17:08Z romanb $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. /**
  22.  * Doctrine_Repository
  23.  * each record is added into Doctrine_Repository at the same time they are created,
  24.  * loaded from the database or retrieved from the cache
  25.  *
  26.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  27.  * @package     Doctrine
  28.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  29.  * @category    Object Relational Mapping
  30.  * @link        www.phpdoctrine.com
  31.  * @since       1.0
  32.  * @version     $Revision: 1080 $
  33.  */
  34. class Doctrine_Table_Repository implements CountableIteratorAggregate
  35. {
  36.     /**
  37.      * @var object Doctrine_Table $table 
  38.      */
  39.     private $table;
  40.     /**
  41.      * @var array $registry 
  42.      *  an array of all records
  43.      *  keys representing record object identifiers
  44.      */
  45.     private $registry = array();
  46.     /**
  47.      * constructor
  48.      *
  49.      * @param Doctrine_Table $table 
  50.      */
  51.     public function __construct(Doctrine_Table $table)
  52.     {
  53.         $this->table = $table;
  54.     }
  55.     /**
  56.      * getTable
  57.      *
  58.      * @return object Doctrine_Table 
  59.      */
  60.     public function getTable()
  61.     {
  62.         return $this->table;
  63.     }
  64.     /**
  65.      * add
  66.      *
  67.      * @param Doctrine_Record $record       record to be added into registry
  68.      * @return boolean 
  69.      */
  70.     public function add(Doctrine_Record $record)
  71.     {
  72.         $oid $record->getOID();
  73.  
  74.         if (isset($this->registry[$oid])) {
  75.             return false;
  76.         }
  77.         $this->registry[$oid$record;
  78.  
  79.         return true;
  80.     }
  81.     /**
  82.      * get
  83.      * @param integer $oid 
  84.      * @throws Doctrine_Table_Repository_Exception
  85.      */
  86.     public function get($oid)
  87.     {
  88.         if isset($this->registry[$oid])) {
  89.             throw new Doctrine_Table_Repository_Exception("Unknown object identifier");
  90.         }
  91.         return $this->registry[$oid];
  92.     }
  93.     /**
  94.      * count
  95.      * Doctrine_Registry implements interface Countable
  96.      * @return integer                      the number of records this registry has
  97.      */
  98.     public function count()
  99.     {
  100.         return count($this->registry);
  101.     }
  102.     /**
  103.      * @param integer $oid                  object identifier
  104.      * @return boolean                      whether ot not the operation was successful
  105.      */
  106.     public function evict($oid)
  107.     {
  108.         if isset($this->registry[$oid])) {
  109.             return false;
  110.         }
  111.         unset($this->registry[$oid]);
  112.         return true;
  113.     }
  114.     /**
  115.      * @return integer                      number of records evicted
  116.      */
  117.     public function evictAll()
  118.     {
  119.         $evicted 0;
  120.         foreach ($this->registry as $oid=>$record{
  121.             if ($this->evict($oid)) {
  122.                 $evicted++;
  123.             }
  124.         }
  125.         return $evicted;
  126.     }
  127.     /**
  128.      * getIterator
  129.      * @return ArrayIterator 
  130.      */
  131.     public function getIterator()
  132.     {
  133.         return new ArrayIterator($this->registry);
  134.     }
  135.     /**
  136.      * contains
  137.      * @param integer $oid                  object identifier
  138.      */
  139.     public function contains($oid)
  140.     {
  141.         return isset($this->registry[$oid]);
  142.     }
  143.     /**
  144.      * loadAll
  145.      * @return void 
  146.      */
  147.     public function loadAll()
  148.     {
  149.         $this->table->findAll();
  150.     }
  151. }