2019-08-17 21:58:04 +02:00

54 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace DesignPatterns\Structural\Registry;
abstract class Registry
{
const LOGGER = 'logger';
/**
* this introduces global state in your application which can not be mocked up for testing
* and is therefor considered an anti-pattern! Use dependency injection instead!
*
* @var array
*/
private static $storedValues = [];
/**
* @var array
*/
private static $allowedKeys = [
self::LOGGER,
];
/**
* @param string $key
* @param mixed $value
*
* @return void
*/
public static function set(string $key, $value)
{
if (!in_array($key, self::$allowedKeys)) {
throw new \InvalidArgumentException('Invalid key given');
}
self::$storedValues[$key] = $value;
}
/**
* @param string $key
*
* @return mixed
*/
public static function get(string $key)
{
if (!in_array($key, self::$allowedKeys) || !isset(self::$storedValues[$key])) {
throw new \InvalidArgumentException('Invalid key given');
}
return self::$storedValues[$key];
}
}