Skip to content

Commit

Permalink
improves the exception message, and removes unnecessary constraint to…
Browse files Browse the repository at this point in the history
… only allow strings inside strings
  • Loading branch information
schmittjoh committed Jul 16, 2011
1 parent d0b056c commit 2c224ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Expand Up @@ -195,18 +195,21 @@ public function resolveString($value, array $resolving = array())

$self = $this;

return preg_replace_callback('/(?<!%)%([^%]+)%/', function ($match) use ($self, $resolving) {
return preg_replace_callback('/(?<!%)%([^%]+)%/', function ($match) use ($self, $resolving, $value) {
$key = strtolower($match[1]);
if (isset($resolving[$key])) {
throw new ParameterCircularReferenceException(array_keys($resolving));
}

$resolved = $self->get($key);

if (!is_string($resolved)) {
throw new RuntimeException('A parameter cannot contain a non-string parameter.');
if (!is_string($resolved) && !is_numeric($resolved)) {
$resolvingKeys = array_keys($resolving);

throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type %s inside string value "%s".', $key, gettype($resolved), $value));
}

$resolved = (string) $resolved;
$resolving[$key] = true;

return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);
Expand Down
Expand Up @@ -120,7 +120,7 @@ public function testResolveValue()
$bag->resolveValue('%foo%');
$this->fail('->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
} catch (RuntimeException $e) {
$this->assertEquals('A parameter cannot contain a non-string parameter.', $e->getMessage(), '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
$this->assertEquals('A string value must be composed of strings and/or numbers, but found parameter "bar" of type array inside string value "a %bar%".', $e->getMessage(), '->resolveValue() throws a RuntimeException when a parameter embeds another non-string parameter');
}

$bag = new ParameterBag(array('foo' => '%bar%', 'bar' => '%foobar%', 'foobar' => '%foo%'));
Expand All @@ -138,6 +138,9 @@ public function testResolveValue()
} catch (ParameterCircularReferenceException $e) {
$this->assertEquals('Circular reference detected for parameter "foo" ("foo" > "bar" > "foobar" > "foo").', $e->getMessage(), '->resolveValue() throws a ParameterCircularReferenceException when a parameter has a circular reference');
}

$bag = new ParameterBag(array('host' => 'foo.bar', 'port' => 1337));
$this->assertEquals('foo.bar:1337', $bag->resolveValue('%host%:%port%'));
}

/**
Expand Down

0 comments on commit 2c224ce

Please sign in to comment.