| 1 | <?php |
| 2 | /* |
| 3 | * $Id: Repository.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $ |
| 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.org>. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * Doctrine_Repository |
| 24 | * each record is added into Doctrine_Repository at the same time they are created, |
| 25 | * loaded from the database or retrieved from the cache |
| 26 | * |
| 27 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi> |
| 28 | * @package Doctrine |
| 29 | * @subpackage Table |
| 30 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
| 31 | * @link www.phpdoctrine.org |
| 32 | * @since 1.0 |
| 33 | * @version $Revision: 2963 $ |
| 34 | */ |
| 35 | class Doctrine_Table_Repository implements Countable, IteratorAggregate |
| 36 | { |
| 37 | /** |
| 38 | * @var object Doctrine_Table $table |
| 39 | */ |
| 40 | private $table; |
| 41 | |
| 42 | /** |
| 43 | * @var array $registry |
| 44 | * an array of all records |
| 45 | * keys representing record object identifiers |
| 46 | */ |
| 47 | private $registry = array(); |
| 48 | |
| 49 | /** |
| 50 | * constructor |
| 51 | * |
| 52 | * @param Doctrine_Table $table |
| 53 | */ |
| 54 | public function __construct(Doctrine_Table $table) |
| 55 | { |
| 56 | $this->table = $table; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * getTable |
| 61 | * |
| 62 | * @return object Doctrine_Table |
| 63 | */ |
| 64 | public function getTable() |
| 65 | { |
| 66 | return $this->table; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * add |
| 71 | * |
| 72 | * @param Doctrine_Record $record record to be added into registry |
| 73 | * @return boolean |
| 74 | */ |
| 75 | public function add(Doctrine_Record $record) |
| 76 | { |
| 77 | $oid = $record->getOID(); |
| 78 | |
| 79 | if (isset($this->registry[$oid])) { |
| 80 | return false; |
| 81 | } |
| 82 | $this->registry[$oid] = $record; |
| 83 | |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * get |
| 89 | * @param integer $oid |
| 90 | * @throws Doctrine_Table_Repository_Exception |
| 91 | */ |
| 92 | public function get($oid) |
| 93 | { |
| 94 | if ( ! isset($this->registry[$oid])) { |
| 95 | throw new Doctrine_Table_Repository_Exception("Unknown object identifier"); |
| 96 | } |
| 97 | return $this->registry[$oid]; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * count |
| 102 | * Doctrine_Registry implements interface Countable |
| 103 | * @return integer the number of records this registry has |
| 104 | */ |
| 105 | public function count() |
| 106 | { |
| 107 | return count($this->registry); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param integer $oid object identifier |
| 112 | * @return boolean whether ot not the operation was successful |
| 113 | */ |
| 114 | public function evict($oid) |
| 115 | { |
| 116 | if ( ! isset($this->registry[$oid])) { |
| 117 | return false; |
| 118 | } |
| 119 | unset($this->registry[$oid]); |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @return integer number of records evicted |
| 125 | */ |
| 126 | public function evictAll() |
| 127 | { |
| 128 | $evicted = 0; |
| 129 | foreach ($this->registry as $oid=>$record) { |
| 130 | if ($this->evict($oid)) { |
| 131 | $evicted++; |
| 132 | } |
| 133 | } |
| 134 | return $evicted; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * getIterator |
| 139 | * @return ArrayIterator |
| 140 | */ |
| 141 | public function getIterator() |
| 142 | { |
| 143 | return new ArrayIterator($this->registry); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * contains |
| 148 | * @param integer $oid object identifier |
| 149 | */ |
| 150 | public function contains($oid) |
| 151 | { |
| 152 | return isset($this->registry[$oid]); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * loadAll |
| 157 | * @return void |
| 158 | */ |
| 159 | public function loadAll() |
| 160 | { |
| 161 | $this->table->findAll(); |
| 162 | } |
| 163 | } |