Skip to content

Commit

Permalink
Allow to return default config value is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCavens committed Apr 24, 2018
1 parent 4aa7221 commit 6f986ea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Values/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public static function from(array $config)
return new static($config);
}

public function get($key)
public function get($key, $default = null)
{
if (!isset($this->config[$key])) {
throw new \InvalidArgumentException('No config value found by key ['.$key.']');
return $default;
}

return $this->config[$key];
Expand Down
13 changes: 10 additions & 3 deletions tests/Values/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ public function it_can_export_to_array()
], $config->toArray());
}

/** @test */
public function non_found_key_returns_default_value()
{
$config = Config::from(['locales' => ['*' => 'nl']]);

$this->assertEquals('foobar',$config->get('unknown', 'foobar'));
$this->assertEquals([],$config->get('unknown', []));
}

/** @test */
public function it_can_set_value_by_key()
{
Expand All @@ -128,12 +137,10 @@ public function it_can_set_value_by_key()
/** @test */
public function it_can_unset_a_value()
{
$this->expectException(\InvalidArgumentException::class, 'No config value found');

$config = Config::from(['locales' => ['*' => 'nl'], 'foobar' => 'nl']);
unset($config['locales']);

$config->get('locales');
$this->assertNull($config->get('locales'));
}

/** @test */
Expand Down

0 comments on commit 6f986ea

Please sign in to comment.