diff --git a/AbstractFactory/AbstractFactory.php b/AbstractFactory/AbstractFactory.php index ea5253b..6db43b8 100644 --- a/AbstractFactory/AbstractFactory.php +++ b/AbstractFactory/AbstractFactory.php @@ -1,42 +1,43 @@ ', $this->_path, $this->_name); + } + +} \ No newline at end of file diff --git a/AbstractFactory/Html/Text.php b/AbstractFactory/Html/Text.php new file mode 100644 index 0000000..49be0dc --- /dev/null +++ b/AbstractFactory/Html/Text.php @@ -0,0 +1,22 @@ +" . htmlspecialchars($this->_text) . ''; + } + +} \ No newline at end of file diff --git a/AbstractFactory/HtmlFactory.php b/AbstractFactory/HtmlFactory.php new file mode 100644 index 0000000..7c9712c --- /dev/null +++ b/AbstractFactory/HtmlFactory.php @@ -0,0 +1,25 @@ + $this->_name, 'path' => $this->_path)); + } + +} \ No newline at end of file diff --git a/AbstractFactory/Json/Text.php b/AbstractFactory/Json/Text.php new file mode 100644 index 0000000..d9aa4b3 --- /dev/null +++ b/AbstractFactory/Json/Text.php @@ -0,0 +1,22 @@ + $this->_text)); + } + +} \ No newline at end of file diff --git a/AbstractFactory/JsonFactory.php b/AbstractFactory/JsonFactory.php new file mode 100644 index 0000000..e8a6059 --- /dev/null +++ b/AbstractFactory/JsonFactory.php @@ -0,0 +1,26 @@ +createVehicle($type); + $obj->setColor("#f00"); - return new $className(); + return $obj; } -} -interface Formatter -{ - -} - -class FormatString implements Formatter -{ - -} - -class FormatNumber implements Formatter -{ - -} +} \ No newline at end of file diff --git a/FactoryMethod/Ferrari.php b/FactoryMethod/Ferrari.php new file mode 100644 index 0000000..f57d728 --- /dev/null +++ b/FactoryMethod/Ferrari.php @@ -0,0 +1,20 @@ +addTuningAMG(); + return $obj; + break; + + default : + throw new \InvalidArgumentException("$type is not a valid vehicle"); + } + } + +} \ No newline at end of file diff --git a/FactoryMethod/ItalianFactory.php b/FactoryMethod/ItalianFactory.php new file mode 100644 index 0000000..db8514c --- /dev/null +++ b/FactoryMethod/ItalianFactory.php @@ -0,0 +1,35 @@ + global => evil + * + * Therefore, you can haZ multiple factories, differently parametrized, + * you can subclass it and you can mock-up it. + */ +class ConcreteFactory +{ + + protected $typeList; + + /** + * You can imagine to inject your own type list or merge with + * the default ones... + */ + public function __construct() + { + $this->typeList = array( + 'bicycle' => __NAMESPACE__ . '\Bicycle', + 'other' => __NAMESPACE__ . '\Scooter' + ); + } + + /** + * Creates a vehicle + * + * @param string $type a known type key + * @return Vehicle a new instance of Vehicle + * @throws \InvalidArgumentException + */ + public function createVehicle($type) + { + if (!array_key_exists($type, $this->typeList)) { + throw new \InvalidArgumentException("$type is not valid vehicle"); + } + $className = $this->typeList[$type]; + + return new $className(); + } + +} \ No newline at end of file diff --git a/SimpleFactory/Scooter.php b/SimpleFactory/Scooter.php new file mode 100644 index 0000000..925d7d2 --- /dev/null +++ b/SimpleFactory/Scooter.php @@ -0,0 +1,20 @@ + global => evil + * Note2: Cannot be subclassed or mock-uped or have multiple different instances + */ +class StaticFactory +{ + + /** + * the parametrized function to get create an instance + * + * @static + * @return Formatter + */ + public static function factory($type) + { + $className = __NAMESPACE__ . '\Format' . ucfirst($type); + + if (!class_exists($className)) { + throw new \InvalidArgumentException('Missing format class.'); + } + + return new $className(); + } + +} diff --git a/Tests/AbstractFactory/AbstractFactoryTest.php b/Tests/AbstractFactory/AbstractFactoryTest.php new file mode 100644 index 0000000..81d0401 --- /dev/null +++ b/Tests/AbstractFactory/AbstractFactoryTest.php @@ -0,0 +1,50 @@ +createText('Lorem Ipsum'), + $factory->createPicture('/image.jpg', 'caption'), + $factory->createText('footnotes') + ); + + $this->assertContainsOnly('DesignPatterns\AbstractFactory\Media', $article); + /* this is the time to look at the Builder pattern. This pattern + * helps you to create complex object like that article above with + * a given Abstract Factory + */ + + } + +} \ No newline at end of file diff --git a/Tests/FactoryMethod/FactoryMethodTest.php b/Tests/FactoryMethod/FactoryMethodTest.php new file mode 100644 index 0000000..463c1c8 --- /dev/null +++ b/Tests/FactoryMethod/FactoryMethodTest.php @@ -0,0 +1,55 @@ +type as $oneType) { + $vehicle = $shop->create($oneType); + $this->assertInstanceOf('DesignPatterns\FactoryMethod\Vehicle', $vehicle); + } + } + + /** + * @dataProvider getShop + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage spaceship is not a valid vehicle + */ + public function testUnknownType(FactoryMethod $shop) + { + $shop->create('spaceship'); + } + +} \ No newline at end of file diff --git a/Tests/SimpleFactory/SimpleFactoryTest.php b/Tests/SimpleFactory/SimpleFactoryTest.php new file mode 100644 index 0000000..3494ca2 --- /dev/null +++ b/Tests/SimpleFactory/SimpleFactoryTest.php @@ -0,0 +1,51 @@ +factory = new ConcreteFactory(); + } + + public function getType() + { + return array( + array('bicycle'), + array('other') + ); + } + + /** + * @dataProvider getType + */ + public function testCreation($type) + { + $obj = $this->factory->createVehicle($type); + $this->assertInstanceOf('DesignPatterns\SimpleFactory\Vehicle', $obj); + } + + /** + * @expectedException \InvalidArgumentException + */ + public function testBadType() + { + $this->factory->createVehicle('car'); + } + +} \ No newline at end of file diff --git a/Tests/StaticFactory/StaticFactoryTest.php b/Tests/StaticFactory/StaticFactoryTest.php new file mode 100644 index 0000000..e906705 --- /dev/null +++ b/Tests/StaticFactory/StaticFactoryTest.php @@ -0,0 +1,31 @@ +assertInstanceOf('DesignPatterns\StaticFactory\Formatter', $obj); + } + +}