<?php

namespace KupShop\GTMOldBundle\Utils;

/**
 * Class DataContainer.
 */
class DataContainer
{
    /**
     * @return array
     */
    public function getDataLayer()
    {
        return $this->data;
    }

    /**
     * @var array
     */
    protected $data = [];

    /**
     * Checks if the container has the given key.
     *
     * @param string $key the key to check
     *
     * @return bool
     */
    public function has($key)
    {
        return array_key_exists($key, $this->data);
    }

    /**
     * Gets the given key from the container, or returns the default if it does not
     * exist.
     *
     * @param string $key     the key to get
     * @param mixed  $default default value to return
     */
    public function get($key, $default = null)
    {
        return $this->has($key) ? $this->data[$key] : $default;
    }

    /**
     * Sets the given key in the container.
     *
     * @param mixed $key   The key to set
     * @param mixed $value the value
     *
     * @return $this
     */
    public function set($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $v) {
                $this->set($k, $v);
            }
        } else {
            $this->data[$key] = $value;
        }

        return $this;
    }

    /**
     * Magic method to allow object-type semantics for the container.
     *
     * @param string $key the key to get
     */
    public function __get($key)
    {
        return $this->get($key);
    }

    /**
     * Magic method to allow object-type semantics for the container.
     *
     * @param string $key   the key to set
     * @param mixed  $value Value to set
     */
    public function __set($key, $value)
    {
        return $this->set($key, $value);
    }

    /**
     * ArrayAccess: Checks if the key exists.
     *
     * @param string $key the key to check
     *
     * @return bool
     */
    public function offsetExists($key)
    {
        return $this->has($key);
    }

    /**
     * ArrayAccess: Unsets the given key.
     *
     * @param string $key the key to unset
     *
     * @return bool
     */
    public function offsetUnset($key)
    {
        $this->remove($key);
    }

    /**
     * ArrayAccess: Gets the given key.
     *
     * @param string $key the key to get
     */
    public function offsetGet($key)
    {
        return $this->get($key);
    }

    /**
     * ArrayAccess: Sets the given offset.
     *
     * @param string $key   the key to set
     * @param mixed  $value the value to set
     */
    public function offsetSet($key, $value)
    {
        $this->set($key, $value);
    }
}
