Location: PHPKode > scripts > Redstart Templating > Redstart-Templating-master/Redstart/Util/Dict.php
<?php

namespace Redstart\Util;

/**
 * Dictionary
 *
 * @package Redstart
 * @subpackage Redstart_Util
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class Dict implements \Serializable, \IteratorAggregate, \Countable {
    /**
     * Case Lower
     */
    const C_LOWER=CASE_LOWER;
    /**
     * Case Upper
     */
    const C_UPPER=CASE_UPPER;
    /**
     * array 
     * @var array
     */
    private $dictionary = array();

    /**
     * Create new dictionary
     * @param \Redstart\Util\Dict|Array $dictionary object
     * @throws \InvalidArgumentException if the given param is not array 
     * or dictionary object
     */
    public function __construct($dictionary=array()) {
        $this->merge($dictionary);
    }

    /**
     * Merge array or dictionary
     * @param \Redstart\Util\Dict|Array $dictionary object
     * @param boolean $replace true to replace existed values false to keep it
     * @throws \InvalidArgumentException if the given param is not array 
     * or dictionary object
     */
    public function merge($dictionary, $replace=true) {
        if (is_array($dictionary)) {
            if ($replace == true) {
                $this->dictionary = array_replace_recursive($this->dictionary, $dictionary);
            } else {
                $this->dictionary = array_merge_recursive($this->dictionary, $dictionary);
            }
        } else {
            if ($dictionary instanceof Dict) {
                if ($replace == true) {
                    $this->dictionary = array_replace_recursive($this->dictionary, $dictionary->items());
                } else {
                    $this->dictionary = array_merge_recursive($this->dictionary, $dictionary->items());
                }
            } else {
                throw new \InvalidArgumentException(
                        "Expect (Array) or (Dict) object But "
                        . "(" . @gettype($dictionary) . ")"
                        . " was given");
            }
        }
    }

    /**
     * Change Keys case lowercased or uppercased
     * @param string $case Dict::C_LOWER || Dict::C_UPPER
     */
    public function changeCase($case) {
        if (!in_array($case, array(self::C_LOWER, self::C_UPPER))) {
            throw new \InvalidArgumentException(
                    "Expect (CASE_UPPER) or (CASE_LOWER) But "
                    . "(" . @gettype($case) . ")"
                    . " was given");
        }
        $this->dictionary = array_change_key_case($this->dictionary, $case);
    }

    /**
     * get dict keys
     * @return array
     */
    public function keys() {
        return array_keys($this->dictionary);
    }

    /**
     * get dict values
     * @return array
     */
    public function values() {
        return array_values($this->dictionary);
    }

    /**
     * get dict items
     * @return array
     */
    public function items() {
        return $this->dictionary;
    }

    /**
     * clear dict
     */
    public function clear() {
        $this->dictionary = array();
    }

    /**
     * check if key exist in the dict
     * @param mixed $key key to look for
     * @return boolean true if exist false otherwise 
     */
    public function has($key) {
        return isset($this->dictionary[$key]) ? true : false;
    }

    /**
     * remove item from dict
     * @param mixed $key key 
     * @return boolean|string removed item on success false otherwise 
     */
    public function popItem($key) {
        $result = false;
        if ($this->has($key)) {
            $result = $this->dictionary[$key];
            unset($this->dictionary[$key]);
            if ($this->has($key)) {
                $result = false;
            }
        }
        return $result;
    }
    
    
    /**
     * remove the first item from dict
     * @return string removed item on success null otherwise
     */
    public function popFirst(){
        return array_shift($this->dictionary);
    }

    /**
     * remove last item from dict
     * @return string removed item on success null otherwise  
     */
    public function pop() {
        return array_pop($this->dictionary);
    }

    /**
     * add new value for the dict
     * @param mixed $key key
     * @param mixed $val value
     */
    public function set($key, $val) {
        if (empty($key)) {
            $this->dictionary[] = $val;
        } else {
            $this->dictionary[$key] = $val;
        }
    }

    /**
     * add new value for the dict
     * @param mixed $key key
     * @param mixed $val value
     */
    public function __set($key, $val) {
        $this->set($key, $val);
    }

    /**
     * get value from dict
     * @param mixed $key key 
     * @return mixed 
     */
    public function get($key) {
        return $this->has($key) ? $this->dictionary[$key] : null;
    }

    /**
     * get value from dict
     * @param mixed $key key 
     * @return mixed 
     */
    public function __get($key) {
        return $this->get($key);
    }

    /**
     * check if key exist in dict
     * @param mixed $key key
     * @return boolean true if exist false otherwise 
     */
    public function __isset($key) {
        return $this->has($key);
    }

    /**
     * remove item from dict
     * @param mixed $key key 
     */
    public function __unset($key) {
        $this->popItem($key);
    }

    /**
     * clear dict
     */
    public function __destruct() {
        $this->clear();
    }

    /**
     * serialize dict
     * @return serialized object
     */
    public function serialize() {
        return serialize($this->dictionary);
    }

    /**
     * unserialize dict
     * @param array $dictionary serialized dict
     */
    public function unserialize($dictionary) {
        $this->dictionary = unserialize($dictionary);
    }

    /**
     * get dict iterator
     * @return \ArrayIterator 
     */
    public function getIterator() {
        return new \ArrayIterator($this->dictionary);
    }

    /**
     * get dict length
     * @return int
     */
    public function count() {
        return count($this->dictionary);
    }

}

Return current item: Redstart Templating