2023-10-04 18:23:30 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.Component.Builder
|
|
|
|
*
|
|
|
|
* @created 4th September, 2022
|
|
|
|
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
|
|
|
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
|
|
|
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace VDM\Joomla\Abstraction;
|
|
|
|
|
|
|
|
|
2023-10-10 09:39:09 +00:00
|
|
|
use VDM\Joomla\Interfaces\Activeregistryinterface;
|
|
|
|
use VDM\Joomla\Interfaces\Registryinterface;
|
|
|
|
use VDM\Joomla\Abstraction\ActiveRegistry;
|
2023-10-04 18:23:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-10-10 09:39:09 +00:00
|
|
|
* VDM Basic Registry.
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
|
|
|
* Don't use this beyond 10 dimensional depth for best performance.
|
|
|
|
*
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2023-10-10 09:39:09 +00:00
|
|
|
abstract class Registry extends ActiveRegistry implements Activeregistryinterface, Registryinterface
|
2023-10-04 18:23:30 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Path separator
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected ?string $separator = '.';
|
|
|
|
|
|
|
|
/**
|
2023-10-10 09:39:09 +00:00
|
|
|
* Sets a value into the registry using multiple keys.
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
2023-10-10 09:39:09 +00:00
|
|
|
* @param string $path Registry path (e.g. vdm.content.builder)
|
2023-10-04 18:23:30 +00:00
|
|
|
* @param mixed $value Value of entry
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If any of the path values are not a number or string.
|
2024-04-22 14:43:33 +00:00
|
|
|
* @return $this
|
2023-10-04 18:23:30 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2024-04-22 14:43:33 +00:00
|
|
|
public function set(string $path, $value): static
|
2023-10-04 18:23:30 +00:00
|
|
|
{
|
|
|
|
if (($keys = $this->getActiveKeys($path)) === null)
|
|
|
|
{
|
|
|
|
throw new \InvalidArgumentException("Path must only be strings or numbers to set any value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->setActive($value, ...$keys);
|
2024-04-22 14:43:33 +00:00
|
|
|
|
|
|
|
return $this;
|
2023-10-04 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 09:39:09 +00:00
|
|
|
* Adds content into the registry. If a key exists,
|
2023-10-04 18:23:30 +00:00
|
|
|
* it either appends or concatenates based on $asArray switch.
|
|
|
|
*
|
2024-01-27 07:08:12 +00:00
|
|
|
* @param string $path Registry path (e.g. vdm.content.builder)
|
|
|
|
* @param mixed $value Value of entry
|
|
|
|
* @param bool|null $asArray Determines if the new value should be treated as an array.
|
|
|
|
* Default is $addAsArray = false (if null) in base class.
|
|
|
|
* Override in child class allowed set class property $addAsArray = true.
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If any of the path values are not a number or string.
|
2024-04-22 14:43:33 +00:00
|
|
|
* @return $this
|
2023-10-04 18:23:30 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2024-04-22 14:43:33 +00:00
|
|
|
public function add(string $path, $value, ?bool $asArray = null): static
|
2023-10-04 18:23:30 +00:00
|
|
|
{
|
|
|
|
if (($keys = $this->getActiveKeys($path)) === null)
|
|
|
|
{
|
|
|
|
throw new \InvalidArgumentException("Path must only be strings or numbers to add any value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addActive($value, $asArray, ...$keys);
|
2024-04-22 14:43:33 +00:00
|
|
|
|
|
|
|
return $this;
|
2023-10-04 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 09:39:09 +00:00
|
|
|
* Retrieves a value (or sub-array) from the registry using multiple keys.
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
2023-10-10 09:39:09 +00:00
|
|
|
* @param string $path Registry path (e.g. vdm.content.builder)
|
2023-10-04 18:23:30 +00:00
|
|
|
* @param mixed $default Optional default value, returned if the internal doesn't exist.
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If any of the path values are not a number or string.
|
|
|
|
* @return mixed The value or sub-array from the storage. Null if the location doesn't exist.
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function get(string $path, $default = null)
|
|
|
|
{
|
|
|
|
if (($keys = $this->getActiveKeys($path)) === null)
|
|
|
|
{
|
|
|
|
throw new \InvalidArgumentException("Path must only be strings or numbers to get any value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getActive($default, ...$keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 09:39:09 +00:00
|
|
|
* Removes a value (or sub-array) from the registry using multiple keys.
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
2023-10-10 09:39:09 +00:00
|
|
|
* @param string $path Registry path (e.g. vdm.content.builder)
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If any of the path values are not a number or string.
|
2024-04-22 14:43:33 +00:00
|
|
|
* @return $this
|
2023-10-04 18:23:30 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2024-04-22 14:43:33 +00:00
|
|
|
public function remove(string $path): static
|
2023-10-04 18:23:30 +00:00
|
|
|
{
|
|
|
|
if (($keys = $this->getActiveKeys($path)) === null)
|
|
|
|
{
|
|
|
|
throw new \InvalidArgumentException("Path must only be strings or numbers to remove any value.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->removeActive(...$keys);
|
2024-04-22 14:43:33 +00:00
|
|
|
|
|
|
|
return $this;
|
2023-10-04 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-10 09:39:09 +00:00
|
|
|
* Checks the existence of a particular location in the registry using multiple keys.
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
2023-10-10 09:39:09 +00:00
|
|
|
* @param string $path Registry path (e.g. vdm.content.builder)
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If any of the path values are not a number or string.
|
|
|
|
* @return bool True if the location exists, false otherwise.
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
public function exists(string $path): bool
|
|
|
|
{
|
|
|
|
if (($keys = $this->getActiveKeys($path)) === null)
|
|
|
|
{
|
|
|
|
throw new \InvalidArgumentException("Path must only be strings or numbers to check if any value exist.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->existsActive(...$keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a separator value
|
|
|
|
*
|
|
|
|
* @param string|null $value The value to set.
|
|
|
|
*
|
2024-04-22 14:43:33 +00:00
|
|
|
* @return $this
|
2023-10-04 18:23:30 +00:00
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
2024-04-22 14:43:33 +00:00
|
|
|
public function setSeparator(?string $value): static
|
2023-10-04 18:23:30 +00:00
|
|
|
{
|
|
|
|
$this->separator = $value;
|
2024-04-22 14:43:33 +00:00
|
|
|
|
|
|
|
return $this;
|
2023-10-04 18:23:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get that the active keys from a path
|
|
|
|
*
|
2023-10-10 09:39:09 +00:00
|
|
|
* @param string $path The path to determine the location registry.
|
2023-10-04 18:23:30 +00:00
|
|
|
*
|
|
|
|
* @return array|null The valid array of keys
|
|
|
|
* @since 3.2.0
|
|
|
|
*/
|
|
|
|
protected function getActiveKeys(string $path): ?array
|
|
|
|
{
|
|
|
|
// empty path no allowed
|
|
|
|
if ($path === '')
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flatten the path
|
|
|
|
if ($this->separator === null || $this->separator === '')
|
|
|
|
{
|
|
|
|
return [$path];
|
|
|
|
}
|
|
|
|
|
|
|
|
$keys = array_values(array_filter(explode($this->separator, $path), 'strlen'));
|
|
|
|
|
|
|
|
if (empty($keys))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|