mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 13:37:27 +02:00
PHP7 Static Factory
This commit is contained in:
@@ -3,30 +3,26 @@
|
|||||||
namespace DesignPatterns\Creational\StaticFactory;
|
namespace DesignPatterns\Creational\StaticFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Note1: Remember, static => global => evil
|
* Note1: Remember, static means global state which is evil because it can't be mocked for tests
|
||||||
* Note2: Cannot be subclassed or mock-upped or have multiple different instances.
|
* Note2: Cannot be subclassed or mock-upped or have multiple different instances.
|
||||||
*/
|
*/
|
||||||
class StaticFactory
|
final class StaticFactory
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* the parametrized function to get create an instance.
|
|
||||||
*
|
|
||||||
* @param string $type
|
* @param string $type
|
||||||
*
|
*
|
||||||
* @static
|
|
||||||
*
|
|
||||||
* @throws \InvalidArgumentException
|
|
||||||
*
|
|
||||||
* @return FormatterInterface
|
* @return FormatterInterface
|
||||||
*/
|
*/
|
||||||
public static function factory($type)
|
public static function factory(string $type): FormatterInterface
|
||||||
{
|
{
|
||||||
$className = __NAMESPACE__.'\Format'.ucfirst($type);
|
if ($type == 'number') {
|
||||||
|
return new FormatNumber();
|
||||||
if (!class_exists($className)) {
|
|
||||||
throw new \InvalidArgumentException('Missing format class.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new $className();
|
if ($type == 'string') {
|
||||||
|
return new FormatString();
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new \InvalidArgumentException('Unknown format given');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -4,33 +4,29 @@ namespace DesignPatterns\Creational\StaticFactory\Tests;
|
|||||||
|
|
||||||
use DesignPatterns\Creational\StaticFactory\StaticFactory;
|
use DesignPatterns\Creational\StaticFactory\StaticFactory;
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests for Static Factory pattern.
|
|
||||||
*/
|
|
||||||
class StaticFactoryTest extends \PHPUnit_Framework_TestCase
|
class StaticFactoryTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function getTypeList()
|
public function testCanCreateNumberFormatter()
|
||||||
{
|
{
|
||||||
return array(
|
$this->assertInstanceOf(
|
||||||
array('string'),
|
'DesignPatterns\Creational\StaticFactory\FormatNumber',
|
||||||
array('number'),
|
StaticFactory::factory('number')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCanCreateStringFormatter()
|
||||||
|
{
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'DesignPatterns\Creational\StaticFactory\FormatString',
|
||||||
|
StaticFactory::factory('string')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider getTypeList
|
* @expectedException \InvalidArgumentException
|
||||||
*/
|
|
||||||
public function testCreation($type)
|
|
||||||
{
|
|
||||||
$obj = StaticFactory::factory($type);
|
|
||||||
$this->assertInstanceOf('DesignPatterns\Creational\StaticFactory\FormatterInterface', $obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @expectedException InvalidArgumentException
|
|
||||||
*/
|
*/
|
||||||
public function testException()
|
public function testException()
|
||||||
{
|
{
|
||||||
StaticFactory::factory('');
|
StaticFactory::factory('object');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user