Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Widop/Twitter/Options/OptionBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*
* @author GeLo <geloen.eric@gmail.com>
*/
class OptionBag implements \ArrayAccess, \IteratorAggregate
class OptionBag implements OptionBagInterface
{
/** @var \Widop\Twitter\Options\OptionFactory */
/** @var \Widop\Twitter\Options\OptionFactoryInterface */
private $factory;

/** @var array */
Expand All @@ -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();
Expand Down
47 changes: 47 additions & 0 deletions src/Widop/Twitter/Options/OptionBagFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Wid'op package.
*
* (c) Wid'op <contact@widop.com>
*
* 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);
}
}
24 changes: 24 additions & 0 deletions src/Widop/Twitter/Options/OptionBagFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Wid'op package.
*
* (c) Wid'op <contact@widop.com>
*
* 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);
}
27 changes: 27 additions & 0 deletions src/Widop/Twitter/Options/OptionBagInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Wid'op package.
*
* (c) Wid'op <contact@widop.com>
*
* 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);
}
4 changes: 1 addition & 3 deletions src/Widop/Twitter/Options/OptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

namespace Widop\Twitter\Options;

use Widop\Twitter\Options\OptionInterface;

/**
* Option factory.
*
* @author GeLo <geloen.eric@gmail.com>
*/
class OptionFactory
class OptionFactory implements OptionFactoryInterface
{
/** @var array */
private $mapping;
Expand Down
29 changes: 29 additions & 0 deletions src/Widop/Twitter/Options/OptionFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Wid'op package.
*
* (c) Wid'op <contact@widop.com>
*
* 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);
}
57 changes: 57 additions & 0 deletions tests/Widop/Tests/Twitter/Options/OptionBagFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Wid'op package.
*
* (c) Wid'op <contact@widop.com>
*
* 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 <geloen.eric@gmail.com>
*/
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');
}
}
4 changes: 3 additions & 1 deletion tests/Widop/Tests/Twitter/Options/OptionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down