| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* LICENSE |
| 6 |
|
* |
| 7 |
|
* This source file is subject to the new BSD license that is bundled |
| 8 |
|
* with this package in the file LICENSE.txt. |
| 9 |
|
* It is also available through the world-wide-web at this URL: |
| 10 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 11 |
|
* If you did not receive a copy of the license and are unable to |
| 12 |
|
* obtain it through the world-wide-web, please send an email |
| 13 |
|
* to xyster@devweblog.org so we can send you a copy immediately. |
| 14 |
|
* |
| 15 |
|
* @category Xyster |
| 16 |
|
* @package Xyster_Orm |
| 17 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 18 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 19 |
|
* @version $Id: Set.php 70 2007-08-17 17:15:45Z doublecompile $ |
| 20 |
|
*/ |
| 21 |
|
/** |
| 22 |
|
* @see Xyster_Data_Set |
| 23 |
|
*/ |
| 24 |
1 |
require_once 'Xyster/Data/Set.php'; |
| 25 |
|
/** |
| 26 |
|
* A set of {@link Xyster_Orm_Entity} objects |
| 27 |
|
* |
| 28 |
|
* @category Xyster |
| 29 |
|
* @package Xyster_Orm |
| 30 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 31 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 32 |
|
*/ |
| 33 |
|
abstract class Xyster_Orm_Set extends Xyster_Data_Set |
| 34 |
|
{ |
| 35 |
|
/** |
| 36 |
|
* The initial snapshot of the set |
| 37 |
|
* |
| 38 |
|
* @var array |
| 39 |
|
*/ |
| 40 |
|
protected $_base = array(); |
| 41 |
|
|
| 42 |
|
/** |
| 43 |
|
* The entity to which this set belongs |
| 44 |
|
* |
| 45 |
|
* This is only set if the set is a many relation, not if pulling entities |
| 46 |
|
* from the data store |
| 47 |
|
* |
| 48 |
|
* @var Xyster_Orm_Entity |
| 49 |
|
*/ |
| 50 |
|
protected $_entity; |
| 51 |
|
|
| 52 |
|
/** |
| 53 |
|
* The relation of which the set is a result |
| 54 |
|
* |
| 55 |
|
* This is only set if the set is a many relation, not if pulling entities |
| 56 |
|
* from the data store |
| 57 |
|
* |
| 58 |
|
* @var Xyster_Orm_Relation |
| 59 |
|
*/ |
| 60 |
|
protected $_relation; |
| 61 |
|
|
| 62 |
|
/** |
| 63 |
|
* Creates a new entity set |
| 64 |
|
* |
| 65 |
|
* @param Xyster_Collection_Interface $values |
| 66 |
|
*/ |
| 67 |
|
public function __construct( Xyster_Collection_Interface $values = null ) |
| 68 |
|
{ |
| 69 |
214 |
parent::__construct($values); |
| 70 |
214 |
if ( $values ) { |
| 71 |
18 |
$this->_base = $this->_items; |
| 72 |
18 |
} |
| 73 |
|
} |
| 74 |
|
|
| 75 |
|
/** |
| 76 |
|
* Adds an item to the set |
| 77 |
|
* |
| 78 |
|
* This collection doesn't accept duplicate values, and will return false |
| 79 |
|
* if the provided value is already in the collection. |
| 80 |
|
* |
| 81 |
|
* It can only accept Xyster_Orm_Entity objects, otherwise it will throw a |
| 82 |
|
* Xyster_Data_Set_Exception. |
| 83 |
|
* |
| 84 |
|
* @param mixed $item The item to add |
| 85 |
|
* @return boolean Whether the set changed as a result of this method |
| 86 |
|
* @throws Xyster_Data_Set_Exception if the collection cannot contain the value |
| 87 |
|
*/ |
| 88 |
|
public function add( $item ) |
| 89 |
|
{ |
| 90 |
63 |
$class = $this->getEntityName(); |
| 91 |
63 |
if (! $item instanceof $class ) { |
| 92 |
1 |
require_once 'Xyster/Data/Set/Exception.php'; |
| 93 |
1 |
throw new Xyster_Data_Set_Exception('This set can only contain Xyster_Orm_Entity objects'); |
| 94 |
0 |
} |
| 95 |
|
|
| 96 |
|
/* |
| 97 |
|
* Relate the owning entity to the new item |
| 98 |
|
*/ |
| 99 |
62 |
if ( $this->_relation && |
| 100 |
62 |
$this->_relation->getType() == 'many' && $this->_entity ) { |
| 101 |
6 |
$this->_relation->relate($this->_entity, $item); |
| 102 |
6 |
} |
| 103 |
|
|
| 104 |
62 |
$added = parent::add($item); |
| 105 |
|
|
| 106 |
62 |
if ( $added && $this->_entity ) { |
| 107 |
9 |
$this->_entity->setDirty(); |
| 108 |
9 |
} |
| 109 |
62 |
return $added; |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
/** |
| 113 |
|
* Baselines the set |
| 114 |
|
* |
| 115 |
|
* This method is used by {@link Xyster_Orm_Mapper} to baseline the set |
| 116 |
|
* after its pending changes have been delt with |
| 117 |
|
* |
| 118 |
|
* Do not call this method; it will lose track of changes to the set after |
| 119 |
|
* it was loaded. |
| 120 |
|
*/ |
| 121 |
|
public function baseline() |
| 122 |
|
{ |
| 123 |
49 |
$this->_base = $this->_items; |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
/** |
| 127 |
|
* Removes all items from the collection |
| 128 |
|
*/ |
| 129 |
|
public function clear() |
| 130 |
|
{ |
| 131 |
1 |
if ( $this->count() && $this->_entity ) { |
| 132 |
1 |
$this->_entity->setDirty(); |
| 133 |
1 |
} |
| 134 |
1 |
parent::clear(); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
/** |
| 138 |
|
* Gets the base state of this set |
| 139 |
|
* |
| 140 |
|
* @return array |
| 141 |
|
*/ |
| 142 |
|
public function getBase() |
| 143 |
|
{ |
| 144 |
1 |
return $this->_base; |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
/** |
| 148 |
|
* Gets any added entities since creation |
| 149 |
|
* |
| 150 |
|
* @return array |
| 151 |
|
*/ |
| 152 |
|
public function getDiffAdded() |
| 153 |
|
{ |
| 154 |
5 |
return array_values(array_diff($this->_items,$this->_base)); |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
/** |
| 158 |
|
* Gets any removed entities since creation |
| 159 |
|
* |
| 160 |
|
* @return array |
| 161 |
|
*/ |
| 162 |
|
public function getDiffRemoved() |
| 163 |
|
{ |
| 164 |
5 |
return array_values(array_diff($this->_base,$this->_items)); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
/** |
| 168 |
|
* Gets the class name of the entity supported by this set |
| 169 |
|
* |
| 170 |
|
* By default this method will return the class name minus the 'Set' suffix. |
| 171 |
|
* |
| 172 |
|
* For instance, for 'UserSet' this method will return 'User'. |
| 173 |
|
* |
| 174 |
|
* Class authors can overwrite this method if their entity names aren't |
| 175 |
|
* the same as the set name. |
| 176 |
|
* |
| 177 |
|
* @return string |
| 178 |
|
*/ |
| 179 |
|
public function getEntityName() |
| 180 |
|
{ |
| 181 |
64 |
return substr(get_class($this),0,-3); |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
/** |
| 185 |
|
* Gets the primary keys of the entities in the set |
| 186 |
|
* |
| 187 |
|
* @return array |
| 188 |
|
*/ |
| 189 |
|
public function getPrimaryKeys() |
| 190 |
|
{ |
| 191 |
4 |
$keys = array(); |
| 192 |
4 |
foreach( $this->_items as $entity ) { |
| 193 |
|
/* @var $entity Xyster_Orm_Entity */ |
| 194 |
4 |
$keys[] = $entity->getPrimaryKey(); |
| 195 |
4 |
} |
| 196 |
4 |
return $keys; |
| 197 |
|
} |
| 198 |
|
|
| 199 |
|
/** |
| 200 |
|
* Gets the entity related to this set |
| 201 |
|
* |
| 202 |
|
* @return Xyster_Orm_Entity the related entity |
| 203 |
|
*/ |
| 204 |
|
public function getRelatedEntity() |
| 205 |
|
{ |
| 206 |
4 |
return $this->_entity; |
| 207 |
|
} |
| 208 |
|
/** |
| 209 |
|
* Gets the relation for this set |
| 210 |
|
* |
| 211 |
|
* @return Xyster_Orm_Relation the relation |
| 212 |
|
*/ |
| 213 |
|
public function getRelation() |
| 214 |
|
{ |
| 215 |
4 |
return $this->_relation; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
/** |
| 219 |
|
* Relates the set to an entity and relationship |
| 220 |
|
* |
| 221 |
|
* @param Xyster_Orm_Relation $relation The relation |
| 222 |
|
* @param Xyster_Orm_Entity $entity The entity |
| 223 |
|
* @throws Xyster_Orm_Exception if the set is already associated |
| 224 |
|
*/ |
| 225 |
|
public function relateTo( Xyster_Orm_Relation $relation, Xyster_Orm_Entity $entity ) |
| 226 |
|
{ |
| 227 |
16 |
if ( $this->_relation instanceof Xyster_Orm_Relation || |
| 228 |
16 |
$this->_entity instanceof Xyster_Orm_Entity ) { |
| 229 |
1 |
require_once 'Xyster/Orm/Exception.php'; |
| 230 |
1 |
throw new Xyster_Orm_Exception('This set is already related to an entity'); |
| 231 |
0 |
} |
| 232 |
16 |
$this->_relation = $relation; |
| 233 |
16 |
$this->_entity = $entity; |
| 234 |
|
|
| 235 |
16 |
if ( $relation->getType() == 'many') { |
| 236 |
10 |
foreach( $this->_items as $item ) { |
| 237 |
3 |
$this->_relation->relate($this->_entity, $item); |
| 238 |
3 |
} |
| 239 |
10 |
} |
| 240 |
|
} |
| 241 |
|
|
| 242 |
|
/** |
| 243 |
|
* Removes the specified value from the collection |
| 244 |
|
* |
| 245 |
|
* @param mixed $item The value to remove |
| 246 |
|
* @return boolean If the value was in the collection |
| 247 |
|
*/ |
| 248 |
|
public function remove( $item ) |
| 249 |
|
{ |
| 250 |
3 |
$removed = parent::remove($item); |
| 251 |
3 |
if ( $removed && $this->_entity ) { |
| 252 |
2 |
$this->_entity->setDirty(); |
| 253 |
2 |
} |
| 254 |
3 |
return $removed; |
| 255 |
|
} |
| 256 |
|
/** |
| 257 |
|
* Removes all of the specified values from the collection |
| 258 |
|
* |
| 259 |
|
* @param Xyster_Collection_Interface $values The values to remove |
| 260 |
|
* @return boolean Whether the collection changed as a result of this method |
| 261 |
|
*/ |
| 262 |
|
public function removeAll( Xyster_Collection_Interface $values ) |
| 263 |
|
{ |
| 264 |
1 |
$removed = parent::removeAll($values); |
| 265 |
1 |
if ( $removed && $this->_entity ) { |
| 266 |
1 |
$this->_entity->setDirty(); |
| 267 |
1 |
} |
| 268 |
1 |
return $removed; |
| 269 |
|
} |
| 270 |
|
/** |
| 271 |
|
* Removes all values from the collection except for the ones specified |
| 272 |
|
* |
| 273 |
|
* {@inherit} |
| 274 |
|
* |
| 275 |
|
* @param Xyster_Collection_Interface $values The values to keep |
| 276 |
|
* @return boolean Whether the collection changed as a result of this method |
| 277 |
|
*/ |
| 278 |
|
public function retainAll( Xyster_Collection_Interface $values ) |
| 279 |
|
{ |
| 280 |
2 |
$removed = parent::retainAll($values); |
| 281 |
2 |
if ( $removed && $this->_entity ) { |
| 282 |
2 |
$this->_entity->setDirty(); |
| 283 |
2 |
} |
| 284 |
2 |
return $removed; |
| 285 |
|
} |
| 286 |
|
} |