Repository.php 4.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.com>.
 */
/**
 * Doctrine_Repository
 * each record is added into Doctrine_Repository at the same time they are created,
 * loaded from the database or retrieved from the cache
 *
lsmith's avatar
lsmith committed
26 27 28 29 30 31 32 33
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
34 35
class Doctrine_Table_Repository implements Countable, IteratorAggregate
{
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    /**
     * @var object Doctrine_Table $table
     */
    private $table;
    /**
     * @var array $registry
     * an array of all records
     * keys representing record object identifiers
     */
    private $registry = array();
    /**
     * constructor
     *
     * @param Doctrine_Table $table
     */
lsmith's avatar
lsmith committed
51 52
    public function __construct(Doctrine_Table $table)
    {
53 54 55 56 57 58 59
        $this->table = $table;
    }
    /**
     * getTable
     *
     * @return object Doctrine_Table
     */
lsmith's avatar
lsmith committed
60 61
    public function getTable()
    {
62 63 64 65 66 67 68 69
        return $this->table;
    }
    /**
     * add
     *
     * @param Doctrine_Record $record       record to be added into registry
     * @return boolean
     */
lsmith's avatar
lsmith committed
70 71
    public function add(Doctrine_Record $record)
    {
72 73
        $oid = $record->getOID();

lsmith's avatar
lsmith committed
74
        if (isset($this->registry[$oid])) {
75
            return false;
lsmith's avatar
lsmith committed
76
        }
77 78 79 80 81 82 83 84 85
        $this->registry[$oid] = $record;

        return true;
    }
    /**
     * get
     * @param integer $oid
     * @throws Doctrine_Table_Repository_Exception
     */
lsmith's avatar
lsmith committed
86 87
    public function get($oid)
    {
lsmith's avatar
lsmith committed
88
        if ( ! isset($this->registry[$oid])) {
89
            throw new Doctrine_Table_Repository_Exception("Unknown object identifier");
lsmith's avatar
lsmith committed
90
        }
91 92 93 94 95 96 97
        return $this->registry[$oid];
    }
    /**
     * count
     * Doctrine_Registry implements interface Countable
     * @return integer                      the number of records this registry has
     */
lsmith's avatar
lsmith committed
98 99
    public function count()
    {
100 101 102 103 104 105
        return count($this->registry);
    }
    /**
     * @param integer $oid                  object identifier
     * @return boolean                      whether ot not the operation was successful
     */
lsmith's avatar
lsmith committed
106 107
    public function evict($oid)
    {
lsmith's avatar
lsmith committed
108
        if ( ! isset($this->registry[$oid])) {
109
            return false;
lsmith's avatar
lsmith committed
110
        }
111 112 113 114 115 116
        unset($this->registry[$oid]);
        return true;
    }
    /**
     * @return integer                      number of records evicted
     */
lsmith's avatar
lsmith committed
117 118
    public function evictAll()
    {
119
        $evicted = 0;
lsmith's avatar
lsmith committed
120 121
        foreach ($this->registry as $oid=>$record) {
            if ($this->evict($oid)) {
122
                $evicted++;
lsmith's avatar
lsmith committed
123
            }
124 125 126 127 128 129 130
        }
        return $evicted;
    }
    /**
     * getIterator
     * @return ArrayIterator
     */
lsmith's avatar
lsmith committed
131 132
    public function getIterator()
    {
133 134 135 136 137 138
        return new ArrayIterator($this->registry);
    }
    /**
     * contains
     * @param integer $oid                  object identifier
     */
lsmith's avatar
lsmith committed
139 140
    public function contains($oid)
    {
141 142 143 144 145 146
        return isset($this->registry[$oid]);
    }
    /**
     * loadAll
     * @return void
     */
lsmith's avatar
lsmith committed
147 148
    public function loadAll()
    {
149 150 151
        $this->table->findAll();
    }
}