1 <?php
2 /* --------------------------------------------------------------
3 KeyValueCollection.inc.php 2015-12-15
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 MainFactory::load_class('AbstractCollection');
13
14
15 /**
16 * Class KeyValueCollection
17 *
18 * @category System
19 * @package Shared
20 */
21 class KeyValueCollection extends AbstractCollection
22 {
23 /**
24 * Class Constructor
25 *
26 * @param array $keyValueArray
27 */
28 public function __construct(array $keyValueArray)
29 {
30 $this->_addToCollectionContentArray($keyValueArray);
31 }
32
33
34 /**
35 * Get the value that corresponds to the provided key.
36 *
37 * @param string $p_keyName
38 *
39 * @return mixed
40 * @throws InvalidArgumentException
41 */
42 public function getValue($p_keyName)
43 {
44 if(!$this->keyExists($p_keyName))
45 {
46 throw new InvalidArgumentException('Given keyName not found: ' . htmlentities($p_keyName));
47 }
48
49 return $this->collectionContentArray[$p_keyName];
50 }
51
52
53 /**
54 * Check if a given key exists within the collection.
55 *
56 * @param string $p_keyName
57 *
58 * @return bool
59 */
60 public function keyExists($p_keyName)
61 {
62 return array_key_exists($p_keyName, $this->collectionContentArray);
63 }
64
65
66 /**
67 * Get valid item type.
68 *
69 * @return string
70 */
71 protected function _getValidType()
72 {
73 return 'string';
74 }
75
76
77 /**
78 * Add the passed key value array to the collection content array.
79 *
80 * @param array $keyValueArray
81 */
82 protected function _addToCollectionContentArray(array $keyValueArray)
83 {
84 foreach($keyValueArray as $itemKey => $itemValue)
85 {
86 $this->collectionContentArray[$itemKey] = $itemValue;
87 }
88 }
89 }
90