1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 
<?php
/* --------------------------------------------------------------
   KeyValueCollection.inc.php 2015-12-15
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2015 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('AbstractCollection');


/**
 * Class KeyValueCollection
 *
 * @category System
 * @package  Shared
 */
class KeyValueCollection extends AbstractCollection
{
    /**
     * Class Constructor
     *
     * @param array $keyValueArray
     */
    public function __construct(array $keyValueArray)
    {
        $this->_addToCollectionContentArray($keyValueArray);
    }
    
    
    /**
     * Get the value that corresponds to the provided key.
     *
     * @param string $p_keyName
     *
     * @return mixed
     * @throws InvalidArgumentException
     */
    public function getValue($p_keyName)
    {
        if(!$this->keyExists($p_keyName))
        {
            throw new InvalidArgumentException('Given keyName not found: ' . htmlentities($p_keyName));
        }
        
        return $this->collectionContentArray[$p_keyName];
    }
    
    
    /**
     * Check if a given key exists within the collection.
     *
     * @param string $p_keyName
     *
     * @return bool
     */
    public function keyExists($p_keyName)
    {
        return array_key_exists($p_keyName, $this->collectionContentArray);
    }
    
    
    /**
     * Get valid item type.
     *
     * @return string
     */
    protected function _getValidType()
    {
        return 'string';
    }
    
    
    /**
     * Add the passed key value array to the collection content array.
     *
     * @param array $keyValueArray
     */
    protected function _addToCollectionContentArray(array $keyValueArray)
    {
        foreach($keyValueArray as $itemKey => $itemValue)
        {
            $this->collectionContentArray[$itemKey] = $itemValue;
        }
    }
}