Skip to content

Commit 9dd7888

Browse files
committed
Merge remote-tracking branch 'origin/master' into feature/json-parsing
2 parents dc09ce3 + 06bb759 commit 9dd7888

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/DefinitionIterator.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ public function __clone()
4242
}
4343

4444
/**
45-
* Travserse the Definition for a value, using a resolver if necessary.
45+
* Traverse the Definition for a value, using a resolver if necessary.
4646
*
4747
* @param string|mixed $lookup
4848
* @param Definition|string|null $definition Definition to iterate rather than root definition
4949
*
50-
* @throws RuntimeException if iterator is already attempting to resolve $lookup
51-
* (ie, definition appears to contain a loop)
52-
* @throws RuntimeException if $lookup does not exist in definition
50+
* @throws \RuntimeException if iterator is already attempting to resolve $lookup
51+
* (ie, definition appears to contain a loop)
52+
* @throws \RuntimeException if $lookup does not exist in definition
53+
* @throws \Exception if lookup is undefined in the context
5354
*/
5455
public function get($lookup, $definition = null)
5556
{

src/Resolver/Conditional.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ public function resolve($definition)
6464
}
6565

6666
foreach ($definition->get($this->getIndicator()) as $matcher) {
67-
$rawValue = $this->getIterator()->get($matcher->get('matches'));
68-
$value = is_scalar($rawValue) ? (string) $rawValue : json_encode($rawValue);
69-
$pattern = '/' . addcslashes($matcher->get('pattern'), '/') . '/';
67+
try {
68+
$rawValue = $this->getIterator()->get($matcher->get('matches'));
69+
} catch (\Exception $exception) {
70+
// exceptions thrown for match value lookups should silently fail and fall through
71+
continue;
72+
}
73+
$value = is_scalar($rawValue) ? (string) $rawValue : json_encode($rawValue);
74+
$pattern = '/' . addcslashes($matcher->get('pattern'), '/') . '/';
7075

7176
if (preg_match($pattern, $value, $matches)) {
7277
// preface each key with '$'

test/Resolver/ConditionalTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,37 @@ public function testIsShorthand(): void
134134
verify($this->resolver->isShortHand('anything'))->is()->false();
135135
}
136136

137+
public function testMatchExceptionFallsThrough(): void
138+
{
139+
$definition = new Definition([
140+
'when' => [[
141+
'matches' => 'key for undefined value',
142+
'pattern' => '[a-zA-Z]+',
143+
'use' => 'some missing resolver',
144+
], [
145+
'matches' => 'key for alpha value',
146+
'pattern' => '[0-9]+',
147+
'use' => 'another missing resolver',
148+
]],
149+
'default' => 'expected resolver',
150+
]);
151+
152+
$iterator = Mockery::mock(DefinitionIterator::class);
153+
$this->resolver->setIterator($iterator);
154+
155+
$iterator->shouldReceive('get')
156+
->with('key for undefined value')
157+
->andThrow(new \Exception('Undefined value in context'));
158+
$iterator->shouldReceive('get')
159+
->with('key for alpha value')
160+
->andReturn('abcdefgHIJKLMNOP');
161+
$iterator->shouldReceive('get')
162+
->with('default', $definition)
163+
->andReturn('value for default resolver');
164+
165+
verify($this->resolver->resolve($definition))->is()->sameAs('value for default resolver');
166+
}
167+
137168
public function testResolveThrowsException(): void
138169
{
139170
$this->expectException(\InvalidArgumentException::class);

0 commit comments

Comments
 (0)