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
<?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>.
 */
21

22 23 24 25 26
/**
 * 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
27 28
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @package     Doctrine
29
 * @subpackage  Table
lsmith's avatar
lsmith committed
30 31 32 33 34
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        www.phpdoctrine.com
 * @since       1.0
 * @version     $Revision$
 */
lsmith's avatar
lsmith committed
35 36
class Doctrine_Table_Repository implements Countable, IteratorAggregate
{
37 38 39 40
    /**
     * @var object Doctrine_Table $table
     */
    private $table;
41

42 43 44 45 46 47
    /**
     * @var array $registry
     * an array of all records
     * keys representing record object identifiers
     */
    private $registry = array();
48

49 50 51 52 53
    /**
     * constructor
     *
     * @param Doctrine_Table $table
     */
lsmith's avatar
lsmith committed
54 55
    public function __construct(Doctrine_Table $table)
    {
56 57
        $this->table = $table;
    }
58

59 60 61 62 63
    /**
     * getTable
     *
     * @return object Doctrine_Table
     */
lsmith's avatar
lsmith committed
64 65
    public function getTable()
    {
66 67
        return $this->table;
    }
68

69 70 71 72 73 74
    /**
     * add
     *
     * @param Doctrine_Record $record       record to be added into registry
     * @return boolean
     */
lsmith's avatar
lsmith committed
75 76
    public function add(Doctrine_Record $record)
    {
77 78
        $oid = $record->getOID();

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

        return true;
    }
86

87 88 89 90 91
    /**
     * get
     * @param integer $oid
     * @throws Doctrine_Table_Repository_Exception
     */
lsmith's avatar
lsmith committed
92 93
    public function get($oid)
    {
lsmith's avatar
lsmith committed
94
        if ( ! isset($this->registry[$oid])) {
95
            throw new Doctrine_Table_Repository_Exception("Unknown object identifier");
lsmith's avatar
lsmith committed
96
        }
97 98
        return $this->registry[$oid];
    }
99

100 101 102 103 104
    /**
     * count
     * Doctrine_Registry implements interface Countable
     * @return integer                      the number of records this registry has
     */
lsmith's avatar
lsmith committed
105 106
    public function count()
    {
107 108
        return count($this->registry);
    }
109

110 111 112 113
    /**
     * @param integer $oid                  object identifier
     * @return boolean                      whether ot not the operation was successful
     */
lsmith's avatar
lsmith committed
114 115
    public function evict($oid)
    {
lsmith's avatar
lsmith committed
116
        if ( ! isset($this->registry[$oid])) {
117
            return false;
lsmith's avatar
lsmith committed
118
        }
119 120 121
        unset($this->registry[$oid]);
        return true;
    }
122

123 124 125
    /**
     * @return integer                      number of records evicted
     */
lsmith's avatar
lsmith committed
126 127
    public function evictAll()
    {
128
        $evicted = 0;
lsmith's avatar
lsmith committed
129 130
        foreach ($this->registry as $oid=>$record) {
            if ($this->evict($oid)) {
131
                $evicted++;
lsmith's avatar
lsmith committed
132
            }
133 134 135
        }
        return $evicted;
    }
136

137 138 139 140
    /**
     * getIterator
     * @return ArrayIterator
     */
lsmith's avatar
lsmith committed
141 142
    public function getIterator()
    {
143 144
        return new ArrayIterator($this->registry);
    }
145

146 147 148 149
    /**
     * contains
     * @param integer $oid                  object identifier
     */
lsmith's avatar
lsmith committed
150 151
    public function contains($oid)
    {
152 153
        return isset($this->registry[$oid]);
    }
154

155 156 157 158
    /**
     * loadAll
     * @return void
     */
lsmith's avatar
lsmith committed
159 160
    public function loadAll()
    {
161 162
        $this->table->findAll();
    }
163
}