/** * Extract a configuration subset * * This will return a new Config object, only containing values whose identifiers match * the given filter. * * @note Identifiers in the subset are cleaned up so that the filter part is removed. * This is one of the reasons why the '.*' pattern is automatically added to the filter. * This behaviour does not actually prevent from passing a full Matcher compatible pattern, * but discourages it. * * @param $filter * * @return Config */ public function subset($filter) { $filterLength = strlen($filter) + 1; // + 1 for the '.' following the prefix // normalize filter $filter .= '.*'; $subset = new Config(); foreach ($this as $key => $value) { if ($this->getMatcher()->match($filter, $key)) { $subset->set(substr($key, $filterLength), $value); } } return $subset; }
public function testDirectiveGroupFetchingUsingMatcher() { $config = new Config(); $config->set('x.y', 'a')->set('x.z', 'b'); $this->assertEquals(['y' => 'a', 'z' => 'b'], $config->subset('x')->toArray()); }