Skip to content

Commit

Permalink
add options feature
Browse files Browse the repository at this point in the history
  • Loading branch information
easy-system committed Jun 29, 2016
1 parent c02c8df commit 8165c1d
Show file tree
Hide file tree
Showing 4 changed files with 212 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Options/OptionsInterface.php
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);
}
71 changes: 71 additions & 0 deletions src/Options/OptionsTrait.php
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;
}
}
23 changes: 23 additions & 0 deletions test/Options/OptionsTemplate.php
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__));
}
}
75 changes: 75 additions & 0 deletions test/Options/OptionsTest.php
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'));
}
}

0 comments on commit 8165c1d

Please sign in to comment.