-
Notifications
You must be signed in to change notification settings - Fork 3
/
AliasingContainer.php
83 lines (71 loc) · 1.85 KB
/
AliasingContainer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Dhii\Collection\ContainerInterface;
use Dhii\Container\Util\StringTranslatingTrait;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use function array_key_exists;
/**
* A container implementation that wraps around an inner container to alias its keys, so consumers can use the aliases
* to fetch data from the inner container.
*/
class AliasingContainer implements ContainerInterface
{
/* @since [*next-version*] */
use StringTranslatingTrait;
/**
* @since [*next-version*]
*
* @var PsrContainerInterface
*/
protected $inner;
/**
* @since [*next-version*]
*
* @var array<array-key, string>
*/
protected $aliases;
/**
* Constructor.
*
* @since [*next-version*]
*
* @param PsrContainerInterface $inner The container whose keys to alias.
* @param array<array-key, string> $aliases A mapping of aliases to their original container key counterparts.
*/
public function __construct(PsrContainerInterface $inner, array $aliases)
{
$this->inner = $inner;
$this->aliases = $aliases;
}
/**
* @inheritdoc
*/
public function get(string $key)
{
return $this->inner->get($this->getInnerKey($key));
}
/**
* @inheritdoc
*/
public function has(string $key): bool
{
return $this->inner->has($this->getInnerKey($key));
}
/**
* Retrieves the key to use for the inner container.
*
* @since [*next-version*]
*
* @param string $key The outer key.
*
* @return string The inner key.
*/
protected function getInnerKey(string $key): string
{
if (array_key_exists($key, $this->aliases)) {
return $this->aliases[$key];
}
return $key;
}
}