mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
53 lines
1.1 KiB
PHP
53 lines
1.1 KiB
PHP
<?php
|
|
|
|
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];
|
|
}
|
|
}
|