<?php

/**
 * @copyright     Copyright (c) 2009-2020 Ryan Demmer. All rights reserved
 * @license       GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * JCE is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses
 */
class WFS3FileSystemCache
{
    private $cacheStore;

    protected static $items = array();

    public function __construct($adapter = 'memory')
    {
        $path = __DIR__ . '/cache/' . $adapter . '.php';
        
        if (is_file($path)) {
            require_once($path);
        }
        
        $classname = 'WFS3FileSystemCache' . ucfirst($adapter);

        if (class_exists($classname)) {
            $this->cacheStore = new $classname;        
        }

        return $this;
    }

    /**
     * @param $key
     * @param $name
     * @param $item
     *
     * @return $this
     */
    public function addItem($key, $name, $item)
    {
        $cache = $this->get();

        $key = trim($key, '/');

        if (isset($cache[$key])) {
            $cache[$key][$name] = $item;
            $this->set($cache);
        }

        return $this;
    }

    public function getItem($key, $item)
    {
        $cache = $this->get();

        $key = trim($key, '/');

        if (isset($cache[$key]) && isset($cache[$key][$item])) {
            return $cache[$key][$item];
        }

        return null;
    }

    /**
     * @param $key
     * @param $old
     * @param $new
     *
     * @return $this
     */
    public function updateItem($key, $old, $new)
    {
        $cache = $this->get();

        $key = trim($key, '/');

        if (isset($cache[$key]) && isset($cache[$key][$old])) {
            $name = $new['name'];

            $cache[$key][$name] = array_merge($cache[$key][$old], $new);

            if ($name !== $old) {
                unset($cache[$key][$old]);
            }

            $this->set($cache);
        }

        return $this;
    }

    /**
     * @param $key
     * @param $item
     *
     * @return $this
     */
    public function removeItem($key, $item)
    {
        $cache = $this->get();

        $key = trim($key, '/');

        if (isset($cache[$key]) && isset($cache[$key][$item])) {
            unset($cache[$key][$item]);

            $this->set($cache);
        }

        return $this;
    }

    /**
     * @return array|mixed
     */
    public function get($path = '', $count = 0)
    {
        $data = $this->cacheStore->get($path);

        $path = trim($path, '/');

        // must be an integer or 0
        if (!is_numeric($count)) {
            $count = 0;
        }

        if (is_array($data)) {
            if ($path && isset($data[$path])) {
                return $data[$path];
            }
        }

        return $data;
    }

    public function set($data, $path = '')
    {
        if (empty($data)) {
            $this->cacheStore->remove();
            return $this;
        }

        $this->cacheStore->store($data, $path);

        return $this;
    }

    public function clear()
    {
        $this->set(null);

        return $this;
    }
}
