-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c02c8df
commit 8165c1d
Showing
4 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* This file is part of the "Easy System" package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Damon Smith <damon.easy.system@gmail.com> | ||
*/ | ||
namespace Es\Container\Options; | ||
|
||
/** | ||
* Represents the feature of options. | ||
*/ | ||
interface OptionsInterface | ||
{ | ||
/** | ||
* Sets the options. | ||
* If the object has a corresponding setter - use it to set values. | ||
* | ||
* @param array $options The options | ||
* | ||
* @return self | ||
*/ | ||
public function setOptions(array $options); | ||
|
||
/** | ||
* Gets the options. | ||
* | ||
* @return array The options | ||
*/ | ||
public function getOptions(); | ||
|
||
/** | ||
* Gets the option. | ||
* | ||
* @param string $name The name of option | ||
* @param mixed $default Optiona; null by default. The default value | ||
* | ||
* @return mixed Returns the value of option, if any, $default otherwise | ||
*/ | ||
public function getOption($name, $default = null); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* This file is part of the "Easy System" package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Damon Smith <damon.easy.system@gmail.com> | ||
*/ | ||
namespace Es\Container\Options; | ||
|
||
/** | ||
* Provides the feature of options. | ||
*/ | ||
trait OptionsTrait | ||
{ | ||
/** | ||
* The options. | ||
* | ||
* @var array | ||
*/ | ||
protected $options = []; | ||
|
||
/** | ||
* Sets the options. | ||
* If the object has a corresponding setter - use it to set values. | ||
* | ||
* @param array $options The options | ||
* | ||
* @return self | ||
*/ | ||
public function setOptions(array $options) | ||
{ | ||
foreach ($options as $name => $value) { | ||
$setter = 'set' . str_replace('_', '', $name); | ||
if (method_exists($this, $setter)) { | ||
$this->{$setter}($value); | ||
} | ||
} | ||
$this->options = $options; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Gets the options. | ||
* | ||
* @return array The options | ||
*/ | ||
public function getOptions() | ||
{ | ||
return $this->options; | ||
} | ||
|
||
/** | ||
* Gets the option. | ||
* | ||
* @param string $name The name of option | ||
* @param mixed $default Optiona; null by default. The default value | ||
* | ||
* @return mixed Returns the value of option, if any, $default otherwise | ||
*/ | ||
public function getOption($name, $default = null) | ||
{ | ||
if (isset($this->options[$name])) { | ||
return $this->options[$name]; | ||
} | ||
|
||
return $default; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
/** | ||
* This file is part of the "Easy System" package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Damon Smith <damon.easy.system@gmail.com> | ||
*/ | ||
namespace Es\Container\Test\Options; | ||
|
||
use Es\Container\Options\OptionsTrait; | ||
use LogicException; | ||
|
||
class OptionsTemplate | ||
{ | ||
use OptionsTrait; | ||
|
||
public function setItem($arg) | ||
{ | ||
throw new LogicException(sprintf('The "%s" is stub.', __METHOD__)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* This file is part of the "Easy System" package. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author Damon Smith <damon.easy.system@gmail.com> | ||
*/ | ||
namespace Es\Container\Test\Options; | ||
|
||
use ReflectionProperty; | ||
|
||
class OptionsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
require_once 'OptionsTemplate.php'; | ||
} | ||
|
||
public function testSetOptionsSetsOptions() | ||
{ | ||
$options = [ | ||
'foo' => 'bar', | ||
'bat' => 'baz', | ||
]; | ||
$template = new OptionsTemplate(); | ||
$template->setOptions($options); | ||
|
||
$reflection = new ReflectionProperty($template, 'options'); | ||
$reflection->setAccessible(true); | ||
$this->assertSame($options, $reflection->getValue($template)); | ||
} | ||
|
||
public function testsetOptionsCallSetter() | ||
{ | ||
$options = [ | ||
'item' => 'foo', | ||
]; | ||
$template = $this->getMock(OptionsTemplate::CLASS, ['setItem']); | ||
$template | ||
->expects($this->once()) | ||
->method('setItem') | ||
->with($this->identicalTo('foo')); | ||
|
||
$template->setOptions($options); | ||
} | ||
|
||
public function testGetOptions() | ||
{ | ||
$options = [ | ||
'foo' => 'bar', | ||
'bat' => 'baz', | ||
]; | ||
$template = new OptionsTemplate(); | ||
$template->setOptions($options); | ||
$this->assertSame($options, $template->getOptions()); | ||
} | ||
|
||
public function testGetOptionReturnsOptionValue() | ||
{ | ||
$options = [ | ||
'foo' => 'bar', | ||
]; | ||
$template = new OptionsTemplate(); | ||
$template->setOptions($options); | ||
$this->assertSame('bar', $template->getOption('foo')); | ||
} | ||
|
||
public function testGetOptionReturnsDefault() | ||
{ | ||
$template = new OptionsTemplate(); | ||
$this->assertSame('bar', $template->getOption('foo', 'bar')); | ||
} | ||
} |