diff --git a/State/State.php b/State/State.php new file mode 100644 index 0000000..6182812 --- /dev/null +++ b/State/State.php @@ -0,0 +1,143 @@ +order = $order; + } + + public function shipOrder() + { + $this->order['status'] = 'shipping'; + $this->order['updatedTime'] = time(); + + // Setting the new order status into database; + return $this->updateOrder($order); + } + + public function completeOrder() + { + //Can not complete the order which status is created, throw exception; + throw new \Exception('Can not complete the order which status is created!'); + } +} + +class ShippingOrder implements OrderInterface +{ + + private $order; + + public function __construct(array $order) + { + if (empty($order)) { + throw new \Exception('Order can not be empty!'); + } + $this->order = $order; + } + + public function shipOrder() + { + //Can not ship the order which status is shipping, throw exception; + throw new \Exception('Can not ship the order which status is shipping!'); + } + + public function completeOrder() + { + $this->order['status'] = 'completed'; + $this->order['updatedTime'] = time(); + + // Setting the new order status into database; + return $this->updateOrder($order); + } +} + +class OrderFactory +{ + + private function __construct() + { + throw Exception('Can not instance the OrderFactory class!'); + } + + public static function getOrder($id) + { + $order = 'Get Order From Database'; + + switch ($order['status']) { + case 'created': + return new CreateOrder($order); + case 'shipping': + return new ShippingOrder($order); + default: + throw new \Exception('Order status error!'); + break; + } + } +} + + +// Client + +class OrderController +{ + /** + * @param int $id + */ + public function shipAction($id) + { + $order = OrderFactory::getOrder($id); + try { + $order->shipOrder($id); + } catch (Exception $e) { + //handle error! + } + // response to browser + } + + public function completeAction($id) + { + $order = OrderFactory::getOrder($id); + try { + $order->completeOrder($id); + } catch (Exception $e) { + //handle error! + } + // response to browser + } +} diff --git a/StaticFactory/FormatNumber.php b/StaticFactory/FormatNumber.php index f7bf851..8b20db2 100644 --- a/StaticFactory/FormatNumber.php +++ b/StaticFactory/FormatNumber.php @@ -2,7 +2,10 @@ namespace DesignPatterns\StaticFactory; -class FormatNumber implements Formatter +/** + * Class FormatNumber + */ +class FormatNumber implements FormatterInterface { - + } diff --git a/StaticFactory/FormatString.php b/StaticFactory/FormatString.php index 9f961e7..9f48fde 100644 --- a/StaticFactory/FormatString.php +++ b/StaticFactory/FormatString.php @@ -2,7 +2,10 @@ namespace DesignPatterns\StaticFactory; -class FormatString implements Formatter +/** + * Class FormatString + */ +class FormatString implements FormatterInterface { - + } diff --git a/StaticFactory/Formatter.php b/StaticFactory/Formatter.php index 54b0ba0..b5220cd 100644 --- a/StaticFactory/Formatter.php +++ b/StaticFactory/Formatter.php @@ -2,7 +2,10 @@ namespace DesignPatterns\StaticFactory; -interface Formatter +/** + * Class FormatterInterface + */ +interface FormatterInterface { - + } diff --git a/StaticFactory/StaticFactory.php b/StaticFactory/StaticFactory.php index aa32d43..f31d4a1 100644 --- a/StaticFactory/StaticFactory.php +++ b/StaticFactory/StaticFactory.php @@ -14,16 +14,19 @@ namespace DesignPatterns\StaticFactory; * - Zend Framework: Zend_Cache_Backend or _Frontend use a factory method create cache backends or frontends * * Note1: Remember, static => global => evil - * Note2: Cannot be subclassed or mock-uped or have multiple different instances + * Note2: Cannot be subclassed or mock-upped or have multiple different instances */ class StaticFactory { - /** * the parametrized function to get create an instance * + * @param string $type + * * @static - * @return Formatter + * + * @throws \InvalidArgumentException + * @return FormatterInterface */ public static function factory($type) { @@ -35,5 +38,4 @@ class StaticFactory return new $className(); } - } diff --git a/StatusPattern/StatusPattern.php b/StatusPattern/StatusPattern.php deleted file mode 100644 index 6c12937..0000000 --- a/StatusPattern/StatusPattern.php +++ /dev/null @@ -1,131 +0,0 @@ -order = $order; - } - - public function shipOrder() - { - $this->order['status'] = 'shipping'; - $this->order['updatedTime'] = time(); - // Setting the new order status into database; - return $this->updateOrder($order); - } - - public function completeOrder() - { - //Can not complete the order which status is created, throw exception; - throw new \Exception('Can not complete the order which status is created!'); - } -} - -class ShippingOrder implements OrderInterface -{ - - private $order; - - public function __construct(array $order) - { - if (empty($order)) { - throw new \Exception('Order can not be empty!'); - } - $this->order = $order; - } - - public function shipOrder() - { - //Can not ship the order which status is shipping, throw exception; - throw new \Exception('Can not ship the order which status is shipping!'); - } - - public function completeOrder() - { - $this->order['status'] = 'completed'; - $this->order['updatedTime'] = time(); - // Setting the new order status into database; - return $this->updateOrder($order); - } -} - - -class OrderFactory { - - public static function getOrder($id) - { - $order = 'Get Order From Database'; - - switch ($order['status']) { - case 'created': - return new CreateOrder($order); - case 'shipping': - return new ShippingOrder($order); - default: - throw new \Exception('Order status error!'); - break; - } - } - - private function __construct() - { - throw Exception('Can not instance the OrderFactory class!'); - } -} - - -// Client - -class OrderController -{ - /** - * @param int $id - */ - public function shipAction($id) - { - $order = OrderFactory::getOrder($id); - try { - $order->shipOrder($id); - } catch (Exception $e) { - //handle error! - } - // response to browser - } - - public function completeAction($id) - { - $order = OrderFactory::getOrder($id); - try { - $order->completeOrder($id); - } catch (Exception $e) { - //handle error! - } - // response to browser - } -}