<?php
/**
 * This file is part of the JustKnit package.
 *
 * @copyright JustKnit (Iris Hammer) <iris@justknit.de>
 * @license All rights reserved (@https://dejure.org/gesetze/UrhG/69a.html)
 * Created on 04-21-2023
 *
 * @package justknit
 */

namespace JustKnit\Register;

use MODX\Revolution\modX;
/**
 * Base class which abstracts storage of values of POST fields, GET fields + fields set by hooks.
 *
 * @package justknit
 * @subpackage register
 */
class Dictionary
{
    /** @var modX $modx A reference to the modX instance */
    public $modx = null;

    /** @var Subscription $register A reference to the main Subscription instance */
    public $register = null;

    /** @var array $config A configuration array */
    public $config = [];

    /** @var array $fields An array of key->name pairs storing the fields passed */
    public $fields = [];
    /**
     * The constructor for the Dictionary class.
     *
     * @param Subscription $register A reference to the main Register instance
     * @param array $config A configuration array
     */
    public function __construct(Register &$register, array $config = [])
    {
        $this->modx = &$register->modx;
        $this->register = &$register;
        $this->config = array_merge($this->config, $config);
    }

    /**
     * Get the fields from POST.
     *
     * @param array $fields A default set of fields to load
     * @return void
     */
    public function gather(array $fields = [])
    {
        $this->fields['post'] = false;
        $this->fields['get'] = false;
        if (!empty($_POST)){
            $this->fields['post'] = true;
            $this->fields = array_merge($this->fields, $_POST);
        }
        if (!empty($_GET)){
            $this->fields['get'] = true;
            $this->fields = array_merge($this->fields, $_GET);
        }
        if (!empty($_FILES)) { $this->fields = array_merge($this->fields,$_FILES); }
/*
        if (empty($fields)) $fields = array();
        $this->fields = array_merge($fields,$_POST);
        if (!empty($_FILES)) { $this->fields = array_merge($this->fields,$_FILES); }*/
//$this->modx->log( modX::LOG_LEVEL_ERROR,'[JustKnit] gather() '. print_r($this->fields, 1));
    }

    /**
     * Set a dictionary field value.
     *
     * @param string $field
     * @param mixed $value
     * @return void
     */
    public function set($field, $value)
    {
        $this->fields[$field] = $value;
    }

    /**
     * Get a dictionary field value.
     *
     * @param string $field
     * @param mixed $default
     * @return mixed
     */
    public function get($field, $default = null)
    {
        return isset($this->fields[$field]) ? $this->fields[$field] : $default;
    }

    /**
     * Return all field values in an array of key->name pairs.
     *
     * @return array
     */
    public function toArray()
    {
        return $this->fields;
    }

    /**
     * Set a variable number of fields by passing in a key->name pair array.
     *
     * @param array $array
     * @return void
     */
    public function fromArray(array $array)
    {
        foreach ($array as $k => $v) {
            $this->fields[$k] = $v;
        }
    }

    /**
     * Remove a field from the stack.
     *
     * @param string $key
     * @return void
     */
    public function remove($key)
    {
        unset($this->fields[$key]);
    }

    /**
     * Reset all fields.
     *
     * @return void
     */
    public function reset()
    {
        $this->fields = [];
    }

}
