http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 14 LOC: 233 Statements: 56
Legend: executednot executeddead code
Source file Statements Methods Total coverage
AbstractCollection.php 96.4% 92.9% 95.7%
   
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_Collection
12
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 */
15
namespace Xyster\Collection;
16
/**
17
 * Abstract class for collections
18
 *
19
 * @category  Xyster
20
 * @package   Xyster_Collection
21
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
22
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
23
 */
24
class AbstractCollection implements ICollection
25
{
26
    /**
27
     * The actual collection entries
28
     *
29
     * @var array
30
     */
31
    protected $_items = array();
32
33
    /**
34
     * Adds an item to the collection
35
     *
36
     * @param mixed $item The item to add
37
     * @return boolean Whether the collection changed as a result of this method
38
     */
39
    public function add($item)
40
    {
41 160
        $this->_items[] = $item;
42 160
        return true;
43
    }
44
45
    /**
46
     * Removes all items from the collection
47
     */
48
    public function clear()
49
    {
50 14
        $this->_items = array();
51
    }
52
53
    /**
54
     * Tests to see whether the collection contains the value supplied
55
     *
56
     * If the supplied value is an object, the comparison will be done for
57
     * identity (===) and not for value (==).
58
     *
59
     * @param mixed $item The item to test
60
     * @return boolean Whether the collection contains the supplied value
61
     */
62
    public function contains($item)
63
    {
64 118
        return in_array($item, $this->_items, true);
65
    }
66
67
    /**
68
     * Tests to see whether the collection contains all of the supplied values
69
     *
70
     * @param ICollection $values The values to test
71
     * @return boolean Whether the collection contains all of the supplied values
72
     */
73
    public function containsAll(ICollection $values)
74
    {
75 21
        foreach ($values as $v) {
76 21
            if (!$this->contains($v)) {
77 7
                return false;
78
            }
79 21
        }
80 21
        return true;
81
    }
82
83
    /**
84
     * Tests to see whether the collection contains any of the supplied values
85
     *
86
     * Basically, implementations can safely return true on the first item that
87
     * is found.
88
     *
89
     * @param ICollection $values The values to test
90
     * @return boolean Whether the collection contains any of the supplied values
91
     */
92
    public function containsAny(ICollection $values)
93
    {
94 20
        foreach ($values as $v) {
95 20
            if ($this->contains($v)) {
96 14
                return true;
97
            }
98 13
        }
99 13
        return false;
100
    }
101
102
    /**
103
     * Gets the number of items in the collection
104
     *
105
     * @return int The number of items
106
     */
107
    public function count()
108
    {
109 133
        return count($this->_items);
110
    }
111
112
    /**
113
     * Gets an iterator for the values in the collection
114
     *
115
     * @return \SeekableIterator
116
     */
117
    public function getIterator()
118
    {
119 64
        return count($this->_items) ?
120 64
                new \ArrayIterator(array_values($this->_items)) : new \EmptyIterator;
121
    }
122
123
    /**
124
     * Tests to see if the collection contains no elements
125
     *
126
     * The return value from this method should be equivalent to
127
     * <code>( $collection->count() == 0 )</code>.
128
     *
129
     * @return boolean Whether this collection has no elements
130
     */
131
    public function isEmpty()
132
    {
133 17
        return $this->count() == 0;
134
    }
135
136
    /**
137
     * Merges the values from the supplied collection into this one
138
     *
139
     * @param ICollection $values
140
     * @return boolean Whether the collection changed as a result of this method
141
     */
142
    public function merge(ICollection $values)
143
    {
144 16
        $before = count($this);
145 16
        foreach ($values as $v) {
146 16
            $this->add($v);
147 16
        }
148 16
        return $this->count() != $before;
149
    }
150
151
    /**
152
     * Removes the specified value from the collection
153
     *
154
     * @param mixed $item The value to remove
155
     * @return boolean If the value was in the collection
156
     */
157
    public function remove($item)
158
    {
159 7
        $before = $this->count();
160 7
        foreach ($this->_items as $key => $value) {
161 7
            if ($value === $item) {
162 7
                unset($this->_items[$key]);
163 7
            }
164 7
        }
165 7
        return $this->count() != $before;
166
    }
167
168
    /**
169
     * Removes all of the specified values from the collection
170
     *
171
     * @param ICollection $values The values to remove
172
     * @return boolean Whether the collection changed as a result of this method
173
     */
174
    public function removeAll(ICollection $values)
175
    {
176 7
        $before = $this->count();
177 7
        foreach ($this->_items as $key => $value) {
178 7
            if ($values->contains($value)) {
179 7
                unset($this->_items[$key]);
180 7
            }
181 7
        }
182 7
        return $this->count() != $before;
183
    }
184
185
    /**
186
     * Removes all values from the collection except for the ones specified
187
     *
188
     * If the collection doesn't contain any of the values supplied, it should
189
     * simply be emptied.
190
     *
191
     * @param ICollection $values The values to keep
192
     * @return boolean Whether the collection changed as a result of this method
193
     */
194
    public function retainAll(ICollection $values)
195
    {
196 7
        $before = $this->count();
197 7
        if (!$values->containsAny($this)) {
198 6
            $this->clear();
199 6
            return true;
200
        }
201 7
        foreach ($this->_items as $key => $value) {
202 7
            if (!$values->contains($value)) {
203 7
                unset($this->_items[$key]);
204 7
            }
205 7
        }
206 7
        return $this->count() != $before;
207
    }
208
209
    /**
210
     * Puts the items in this collection into an array
211
     *
212
     * @return array The items in this collection
213
     */
214
    public function toArray()
215
    {
216 17
        return array_values($this->_items);
217
    }
218
219
    /**
220
     * Converts the collection into a string
221
     *
222
     * @magic
223
     * @return string
224
     */
225
    public function __toString()
226
    {
227
        try {
228 5
            return '[' . implode(',', $this->_items) . ']';
229
        } catch (\Exception $e) {
230
            return '[???]';
231
        }
232
    }
233
}


Report generated at 2010-10-18T17:19:49-04:00