http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 15 LOC: 281 Statements: 62

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


Report generated at 2008-03-05T18:27:43-05:00