1 <?php
2
3 /* --------------------------------------------------------------
4 EditableCollection.php 2015-11-24
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2015 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 /**
14 * Class EditableCollection
15 *
16 * @category System
17 * @package Shared
18 */
19 class EditableCollection extends AbstractCollection
20 {
21 /**
22 * Sets an item to the collection.
23 * If the index already exists, the value gets override.
24 *
25 * @param int|IntType $index Gets validate internally with the IntType.
26 * @param string|StringType $value Gets validate internally with the StringType.
27 *
28 * @throws InvalidArgumentException When value has an invalid type.
29 * @return $this Same instance to make chained method calls possible.
30 */
31 public function setItem($index, $value)
32 {
33 // passed args through type objects to force their validation without using type hints.
34 new IntType($index);
35 new StringType($value);
36
37 if(!$this->_itemIsValid($value))
38 {
39 $exceptionText = $this->_getExceptionText();
40 throw new InvalidArgumentException($exceptionText);
41 }
42 $this->collectionContentArray[$index] = $value;
43
44 return $this;
45 }
46
47
48 /**
49 * Adds a new item to the collection.
50 *
51 * @param string|StringType $value Gets validate internally with the StringType.
52 *
53 * @throws InvalidArgumentException When value has an invalid type.
54 * @return $this Same instance to make chained method calls possible.
55 */
56 public function addItem($value)
57 {
58 // passed arg through type objects to force their validation without using type hints.
59 new StringType($value);
60
61 $this->_add($value);
62
63 return $this;
64 }
65
66
67 /**
68 * Add another collection to this one.
69 *
70 * @param \EditableCollection $collection Collection to add.
71 *
72 * @throws \InvalidArgumentException When the item types of the passed collection are invalid.
73 * @return $this Same instance to make chained method calls possible.
74 */
75 public function addCollection(EditableCollection $collection)
76 {
77 foreach($collection->getArray() as $collectionItem)
78 {
79 $this->_add($collectionItem);
80 }
81
82 return $this;
83 }
84
85
86 /**
87 * Return a clone of the current editable key value collection instance.
88 */
89 public function getClone()
90 {
91 return clone $this;
92 }
93
94
95 /**
96 * Get valid type.
97 *
98 * This method must be implemented in the child-collection classes.
99 *
100 * @return string
101 */
102 protected function _getValidType()
103 {
104 return 'string';
105 }
106 }
107