AbstractCollection::toArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * \AppserverIo\Collections\AbstractCollection
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/collections
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Collections;
22
23
use AppserverIo\Lang\Object;
24
use AppserverIo\Lang\Strng;
25
use AppserverIo\Lang\Flt;
26
use AppserverIo\Lang\Integer;
27
use AppserverIo\Lang\Boolean;
28
use AppserverIo\Lang\NullPointerException;
29
30
/**
31
 * Abstract base class of the IndexedCollections.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2015 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/appserver-io/collections
37
 * @link      http://www.appserver.io
38
 */
39
abstract class AbstractCollection extends Object implements CollectionInterface, \IteratorAggregate
40
{
41
42
    /**
43
     * Holds the items of the ArrayList
44
     *
45
     * @var array
46
     */
47
    protected $items = array();
48
49
    /**
50
     * Default constructor which makes sure that our properties are preset properly
51
     */
52 19
    public function __construct()
53
    {
54 19
        $this->items = array();
55 19
    }
56
57
    /**
58
     * This method returns a new Iter object
59
     * needed by a foreach structure.
60
     *
61
     * @return \AppserverIo\Collections\Iter Holds the Iter object
62
     * @see \AppserverIo\Collections\IteratorAggregate::getIterator()
63
     */
64 3
    public function getIterator()
65
    {
66 3
        return new Iter($this->items);
67
    }
68
69
    /**
70
     * This method appends all elements of the
71
     * passed array to the Collection.
72
     *
73
     * @param array $array Holds the array with the values to add
74
     *
75
     * @return \AppserverIo\Collections\CollectionInterface The instance
76
     * @see \AppserverIo\Collections\Collection::addAll($array)
77
     */
78 3
    public function addAll($array)
79
    {
80 3
        foreach ($array as $key => $value) {
81 3
            $this->items[$key] = $value;
82 3
        }
83
        // return the instance
84 3
        return $this;
85
    }
86
87
    /**
88
     * This method returns the element with the passed key
89
     * from the Collection.
90
     *
91
     * @param mixed $key Holds the key of the element to return
92
     *
93
     * @return mixed The requested element
94
     * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an integer
95
     * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key OR value are NULL
96
     * @throws \AppserverIo\Collections\IndexOutOfBoundsException Is thrown if no element with the passed key exists in the Collection
97
     * @see \AppserverIo\Collections\CollectionInterface::get($key)
98
     */
99 8 View Code Duplication
    public function get($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101 8
        if (is_null($key)) {
102
            throw new NullPointerException('Passed key is null');
103
        }
104
        // check if a primitive datatype is passed
105 8
        if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) {
106
            // return the value for the passed key, if it exists
107 8
            if (isset($this->items[$key])) {
108 6
                return $this->items[$key];
109
            } else {
110 2
                throw new IndexOutOfBoundsException('Index ' . $key . ' out of bounds');
111
            }
112
        }
113
        // check if an object is passed
114
        if (is_object($key)) {
115
            if ($key instanceof Strng) {
116
                $newKey = $key->stringValue();
117
            } elseif ($key instanceof Flt) {
118
                $newKey = $key->floatValue();
119
            } elseif ($key instanceof Integer) {
120
                $newKey = $key->intValue();
121
            } elseif ($key instanceof Boolean) {
122
                $newKey = $key->booleanValue();
123
            } elseif (method_exists($key, '__toString')) {
124
                $newKey = $key->__toString();
125
            } else {
126
                throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method');
127
            }
128
            // return the value for the passed key, if it exists
129
            if (isset($this->items[$newKey])) {
130
                return $this->items[$newKey];
131
            } else {
132
                throw new IndexOutOfBoundsException('Index ' . $newKey . ' out of bounds');
133
            }
134
        }
135
        throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method');
136
    }
137
138
    /**
139
     * This method checks if an element with the passed
140
     * key exists in the Collection.
141
     * If yes the method
142
     * returns TRUE, else FALSE.
143
     *
144
     * @param mixed $key Holds the key of the element to check for
145
     *
146
     * @return boolean Returns TRUE if the element exists in the Collection else FALSE
147
     * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an integer
148
     * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL
149
     * @see \AppserverIo\Collections\CollectionInterface::exists($key)
150
     */
151 1
    public function exists($key)
152
    {
153 1
        if (is_null($key)) {
154
            throw new NullPointerException('Passed key is null');
155
        }
156
        // check if a primitive datatype is passed
157 1 View Code Duplication
        if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
158
            // return TRUE if a value for the passed key exists, else FALSE
159 1
            return isset($this->items[$key]);
160
        }
161
        // check if an object is passed
162
        if (is_object($key)) {
163
            if ($key instanceof Strng) {
164
                $newKey = $key->stringValue();
165
            } elseif ($key instanceof Flt) {
166
                $newKey = $key->floatValue();
167
            } elseif ($key instanceof Integer) {
168
                $newKey = $key->intValue();
169
            } elseif ($key instanceof Boolean) {
170
                $newKey = $key->booleanValue();
171
            } elseif (method_exists($key, '__toString')) {
172
                $newKey = $key->__toString();
173
            } else {
174
                throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method');
175
            }
176
            // return TRUE if a value for the passed key exists, else FALSE
177
            return isset($this->items[$newKey]);
178
        }
179
        throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method');
180
    }
181
182
    /**
183
     * This method returns an array with the
184
     * items of the Dictionary. The keys are
185
     * lost in the array.
186
     *
187
     * @return array Holds an array with the items of the Dictionary
188
     * @see \AppserverIo\Collections\CollectionInterface::toArray()
189
     */
190 4
    public function toArray()
191
    {
192 4
        return array_values($this->items);
193
    }
194
195
    /**
196
     * This method returns the number of entries of the Collection.
197
     *
198
     * @return integer The number of entries
199
     * @see \AppserverIo\Collections\CollectionInterface::size()
200
     */
201 11
    public function size()
202
    {
203 11
        return sizeof($this->items);
204
    }
205
206
    /**
207
     * This method initializes the Collection and removes
208
     * all existing entries.
209
     *
210
     * @return void
211
     * @see \AppserverIo\Collections\CollectionInterface::clear()
212
     */
213 6
    public function clear()
214
    {
215 6
        $this->items = array();
216 6
    }
217
218
    /**
219
     * This returns true if the Collection has no
220
     * entries, otherwise false.
221
     *
222
     * @return boolean
223
     * @see \AppserverIo\Collections\Collection::isEmpty()
224
     */
225 3
    public function isEmpty()
226
    {
227 3
        if ($this->size() == 0) {
228 3
            return true;
229
        }
230 3
        return false;
231
    }
232
233
    /**
234
     * This method removes the element with the passed key, that has to be an integer, from the IndexedCollection.
235
     *
236
     * @param mixed $key Holds the key of the element to remove
237
     *
238
     * @return void
239
     * @throws \AppserverIo\Collections\InvalidKeyException Is thrown if the passed key is NOT an integer
240
     * @throws \AppserverIo\Lang\NullPointerException Is thrown if the passed key is NULL
241
     * @throws \AppserverIo\Collections\IndexOutOfBoundsException
242
     */
243 6 View Code Duplication
    public function remove($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
244
    {
245 6
        if (is_null($key)) {
246
            throw new NullPointerException('Passed key is null');
247
        }
248
        // check if a primitive datatype is passed
249 6
        if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) {
250 6
            if (isset($this->items[$key])) {
251
                // remove the item
252 3
                unset($this->items[$key]);
253
                // return the instance
254 3
                return $this;
255
            } else {
256 3
                throw new IndexOutOfBoundsException('Index ' . $key . ' out of bounds');
257
            }
258
        }
259
        // check if an object is passed
260
        if (is_object($key)) {
261
            if ($key instanceof Strng) {
262
                $newKey = $key->stringValue();
263
            } elseif ($key instanceof Flt) {
264
                $newKey = $key->floatValue();
265
            } elseif ($key instanceof Integer) {
266
                $newKey = $key->intValue();
267
            } elseif ($key instanceof Boolean) {
268
                $newKey = $key->booleanValue();
269
            } elseif (method_exists($key, '__toString')) {
270
                $newKey = $key->__toString();
271
            } else {
272
                throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method');
273
            }
274
            if (isset($this->items[$newKey])) {
275
                // remove the item
276
                unset($this->items[$newKey]);
277
                // returns the instance
278
                return $this;
279
            } else {
280
                throw new IndexOutOfBoundsException('Index ' . $newKey . ' out of bounds');
281
            }
282
        }
283
        throw new InvalidKeyException('Passed key has to be a primitive datatype or ' . 'has to implement the __toString() method');
284
    }
285
286
    /**
287
     * This method merges the elements of the passed map
288
     * with the elements of the actual map.
289
     *
290
     * If the keys are equal, the existing value is taken, else
291
     * the new one is appended.
292
     *
293
     * @param \AppserverIo\Collections\CollectionInterface $col Holds the Collection with the values to merge
294
     *
295
     * @return void
296
     * @deprecated Does not work correctly
297
     */
298
    public function merge(CollectionInterface $col)
299
    {
300
        // union the items of the two maps
301
        $this->items = $this->items + $col->toArray();
302
    }
303
304
    /**
305
     * This method checks if an element with the passed
306
     * value exists in the ArrayList.
307
     *
308
     * @param mixed $value Holds the value to check the elements of the array list for
309
     *
310
     * @return boolean Returns true if an element with the passed value exists in the array list
311
     */
312
    public function contains($value)
313
    {
314
        // initialize the return value
315
        $isEqual = false;
316
        // run through all elements an check if the one
317
        // of the items is equal to the passed one
318
        foreach ($this->items as $item) {
319
            if ($item == $value) {
320
                $isEqual = true;
321
            }
322
        }
323
        // return false if the value is not found
324
        return $isEqual;
325
    }
326
}
327