-
Notifications
You must be signed in to change notification settings - Fork 3
/
SegmentingContainer.php
114 lines (101 loc) · 3.07 KB
/
SegmentingContainer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Dhii\Container;
use Dhii\Collection\ContainerInterface;
use Psr\Container\ContainerInterface as PsrContainerInterface;
use function array_filter;
use function ltrim;
/**
* This container implementation decorates another to provide nested container access even when the decorated
* container's internal data is flat.
*
* Segmenting containers are intended to be used with keys that contain segments, i.e. keys that use a delimiter to
* indicate hierarchy. For example: "some/test/key" or "some.deep.config.value". The delimiter can be configured during
* construction of a segmenting container.
*
* A segmenting container can yield 2 different kinds of results when {@link SegmentingContainer::get()} is called:
*
* **Values**
*
* If the inner container has a value for the given key, that value is returned.
*
* **Segments**
*
* If the inner container has no value for the given key, a new {@link SegmentingContainer} instance is returned. This
* segmenting container will be aware of the key that resulted in its creation, and will automatically prepend that key
* to parameter keys given in `get()`.
*
* **Example usage:**
*
* Consider the below data and a regular `$container` that provides access to it:
*
* ```php
* $data = [
* 'config.db.host' => 'localhost',
* 'config.db.post' => '3306',
* ];
* ```
*
* A segmenting container can be created that provides access to the "host" and "port":
*
* ```php
* $segmented = new SegmentingContainer($container, '.');
* $dbConfig = $config->get('config')->get('db');
* $dbConfig->get("host"); // "localhost"
* $dbConfig->get("port"); // 3306
* ```
*
* @see PathContainer For an implementation that achieves the opposite effect.
*/
class SegmentingContainer implements ContainerInterface
{
/**
* @var PsrContainerInterface
*/
protected $inner;
/**
* @var string
*/
protected $root;
/**
* @var string
*/
protected $delimiter;
/**
* Constructor.
*
* @since [*next-version*]
*
* @param PsrContainerInterface $inner The container to decorate.
* @param string $delimiter The path delimiter.
*/
public function __construct(PsrContainerInterface $inner, string $delimiter = '/')
{
$this->inner = $inner;
$this->root = '';
$this->delimiter = $delimiter;
}
/**
* @inheritdoc
*/
public function get(string $key)
{
$tKey = ltrim($key, $this->delimiter);
$tRoot = rtrim($this->root, $this->delimiter);
// Implode to glue together the key and root, and array_filter to ignore them if they're empty
$fullKey = implode($this->delimiter, array_filter([$tRoot, $tKey]));
if ($this->inner->has($fullKey)) {
return $this->inner->get($fullKey);
}
$instance = clone $this;
$instance->root = $fullKey;
return $instance;
}
/**
* @inheritdoc
*/
public function has(string $key): bool
{
return $this->inner->has($key);
}
}