Skip to content
This repository was archived by the owner on Oct 17, 2019. It is now read-only.
Open
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
31 changes: 3 additions & 28 deletions Prototype/exercise.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace ShopingCartFramework {
class Shop {
protected $productPrototype;
Expand All @@ -9,8 +8,7 @@ public function __construct(ProductInterface $productPrototype) {
public function listProducts(array $codes) {
$output = [];
foreach ($codes as $code) {
$product = clone $this->productPrototype;
$product->initialize($code);
// @TODO create an actual $product, and initialize it
$output[] = $product->getShopProductCode() . ' - ' . $product->getShopDescription();
}
return implode(PHP_EOL, $output);
Expand All @@ -22,44 +20,21 @@ public function getShopProductCode();
public function getShopDescription();
}
}

namespace MyCompanyShop {
use ShopingCartFramework\ProductInterface;
class MyShopProduct implements ProductInterface {
protected $productService = array();
protected $code, $description;
public function __construct(\Closure $productService) {
$this->productService = $productService;
}
public function initialize($code) {
$this->code = $code;
$this->description = call_user_func($this->productService, $code);
}
public function getShopProductCode() {
return $this->code;
}
public function getShopDescription() {
return $this->description;
}
}

// @TODO implement MyShopProduct prototype
}

namespace {
use ShopingCartFramework\Shop;
use MyCompanyShop\MyShopProduct;

$mockWebService = function($code) {
static $data = [
'BumperSticker1' => 'Cool bumper sticker',
'CoffeeTableBook5' => 'Coffee Table book',
];
return $data[$code];
};

$shop = new Shop(new MyShopProduct($mockWebService));

$productsToList = ['BumperSticker1', 'CoffeeTableBook5'];
header('content-type: plain/text');
echo $shop->listProducts($productsToList); // simulation of Shopping Cart Listings Page
}
}