diff --git a/src/Widop/Twitter/Options/OptionBag.php b/src/Widop/Twitter/Options/OptionBag.php index 972fcae..db77628 100644 --- a/src/Widop/Twitter/Options/OptionBag.php +++ b/src/Widop/Twitter/Options/OptionBag.php @@ -16,9 +16,9 @@ * * @author GeLo */ -class OptionBag implements \ArrayAccess, \IteratorAggregate +class OptionBag implements OptionBagInterface { - /** @var \Widop\Twitter\Options\OptionFactory */ + /** @var \Widop\Twitter\Options\OptionFactoryInterface */ private $factory; /** @var array */ @@ -29,7 +29,7 @@ class OptionBag implements \ArrayAccess, \IteratorAggregate * * @param \Widop\Twitter\Options\OptionFactory|null $factory The option factory. */ - public function __construct(OptionFactory $factory = null) + public function __construct(OptionFactoryInterface $factory = null) { $this->factory = $factory ?: new OptionFactory(); $this->options = array(); diff --git a/src/Widop/Twitter/Options/OptionBagFactory.php b/src/Widop/Twitter/Options/OptionBagFactory.php new file mode 100644 index 0000000..2ae47a4 --- /dev/null +++ b/src/Widop/Twitter/Options/OptionBagFactory.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Widop\Twitter\Options; + +class OptionBagFactory implements OptionBagFactoryInterface +{ + /** @var string */ + private $class; + + /** + * Creates an option bag factory. + * + * @param string $class A fully-qualified classname that implements OptionBagInterface + * + * @throws \InvalidArgumentException If the supplied classname does not implement OptionBagInterface + */ + public function __construct($class = "Widop\\Twitter\\Options\\OptionBag") + { + if (!in_array("Widop\\Twitter\\Options\\OptionBagInterface", class_implements($class))) { + throw new \InvalidArgumentException(sprintf('The class "%s" does not implement the required interface.', $class)); + } + + $this->class = $class; + } + + /** + * Creates an option bag. + * + * @param \Widop\Twitter\Options\OptionFactoryInterface A factory that creates options. + * + * @return \Widop\Twitter\Options\OptionBagInterface The option bag. + */ + public function create(OptionFactoryInterface $factory = null) + { + $class = $this->class; + return new $class($factory); + } +} diff --git a/src/Widop/Twitter/Options/OptionBagFactoryInterface.php b/src/Widop/Twitter/Options/OptionBagFactoryInterface.php new file mode 100644 index 0000000..f9c3cf7 --- /dev/null +++ b/src/Widop/Twitter/Options/OptionBagFactoryInterface.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Widop\Twitter\Options; + +interface OptionBagFactoryInterface +{ + /** + * Creates an option bag. + * + * @param \Widop\Twitter\Options\OptionFactoryInterface A factory that creates options. + * + * @return \Widop\Twitter\Options\OptionBagInterface The option bag. + */ + public function create(OptionFactoryInterface $factory = null); +} diff --git a/src/Widop/Twitter/Options/OptionBagInterface.php b/src/Widop/Twitter/Options/OptionBagInterface.php new file mode 100644 index 0000000..ead9f81 --- /dev/null +++ b/src/Widop/Twitter/Options/OptionBagInterface.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Widop\Twitter\Options; + +interface OptionBagInterface extends \ArrayAccess, \IteratorAggregate +{ + /** + * Registers an option. + * + * @param string|\Widop\Twitter\Options\OptionInterface $option The option. + * @param string $type The option type. + * + * @throws \InvalidArgumentException If the option is not valid. + * + * @return \Widop\Twitter\Options\OptionBag The option bag. + */ + public function register($option, $type = OptionInterface::TYPE_GET); +} diff --git a/src/Widop/Twitter/Options/OptionFactory.php b/src/Widop/Twitter/Options/OptionFactory.php index 3cb55d6..2189624 100644 --- a/src/Widop/Twitter/Options/OptionFactory.php +++ b/src/Widop/Twitter/Options/OptionFactory.php @@ -11,14 +11,12 @@ namespace Widop\Twitter\Options; -use Widop\Twitter\Options\OptionInterface; - /** * Option factory. * * @author GeLo */ -class OptionFactory +class OptionFactory implements OptionFactoryInterface { /** @var array */ private $mapping; diff --git a/src/Widop/Twitter/Options/OptionFactoryInterface.php b/src/Widop/Twitter/Options/OptionFactoryInterface.php new file mode 100644 index 0000000..e584197 --- /dev/null +++ b/src/Widop/Twitter/Options/OptionFactoryInterface.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Widop\Twitter\Options; + +use Widop\Twitter\Options\OptionInterface; + +interface OptionFactoryInterface +{ + /** + * Creates an option. + * + * @param string $option The option name. + * @param string $type The option type. + * + * @throws \InvalidArgumentException If the option does not exist. + * + * @return \Widop\Twitter\Options\OptionInterface The option. + */ + public function create($option, $type = OptionInterface::TYPE_GET); +} diff --git a/tests/Widop/Tests/Twitter/Options/OptionBagFactoryTest.php b/tests/Widop/Tests/Twitter/Options/OptionBagFactoryTest.php new file mode 100644 index 0000000..b801eca --- /dev/null +++ b/tests/Widop/Tests/Twitter/Options/OptionBagFactoryTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please read the LICENSE + * file that was distributed with this source code. + */ + +namespace Widop\Tests\Twitter\Options; + +use Widop\Twitter\Options\OptionBagFactory; + +/** + * Option factory test. + * + * @author GeLo + */ +class OptionBagFactoryTest extends \PHPUnit_Framework_TestCase +{ + /** @var \Widop\Twitter\Options\OptionBagFactory */ + private $optionBagFactory; + + /** + * {@inheritdoc} + */ + protected function setUp() + { + $this->optionBagFactory = new OptionBagFactory(); + } + + /** + * {@inheritdoc} + */ + protected function tearDown() + { + unset($this->optionBagFactory); + } + + public function testCreate() + { + $optionBag = $this->optionBagFactory->create(); + $this->assertInstanceOf('Widop\Twitter\Options\OptionBag', $optionBag); + $this->assertInstanceOf('Widop\Twitter\Options\OptionBagInterface', $optionBag); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The class "Widop\Twitter\Options\OptionFactory" does not implement the required interface. + */ + public function testCreateWithNonImplementingClass() + { + new OptionBagFactory('Widop\Twitter\Options\OptionFactory'); + } +} diff --git a/tests/Widop/Tests/Twitter/Options/OptionFactoryTest.php b/tests/Widop/Tests/Twitter/Options/OptionFactoryTest.php index d01ea3c..a7646b5 100644 --- a/tests/Widop/Tests/Twitter/Options/OptionFactoryTest.php +++ b/tests/Widop/Tests/Twitter/Options/OptionFactoryTest.php @@ -41,7 +41,9 @@ protected function tearDown() public function testCreate() { - $this->assertInstanceOf('Widop\Twitter\Options\AccuracyOption', $this->optionFactory->create('accuracy')); + $option = $this->optionFactory->create('accuracy'); + $this->assertInstanceOf('Widop\Twitter\Options\AccuracyOption', $option); + $this->assertInstanceOf('Widop\Twitter\Options\OptionInterface', $option); } /**