'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 * ``` * * @since [*next-version*] * @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 * * @since [*next-version*] */ public function get($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 * * @since [*next-version*] */ public function has($key) { $key = (string) $key; return $this->inner->has($key); } }