Share
Embed
Transcript
48 slides
-
*UFN class Item { public $code; public $price; public
function __construct(string $code, int $price) { $this->code = $code; $this->price = $price; } } -
$BSU class Cart { public $items; public function __construct()
{ $this->items = []; } public function calculateTotalPrice(): int { $totalPrice = 0; foreach ($this->items as $item) { $totalPrice = $totalPrice + $item->price; } return $totalPrice; } } -
$BSU class Cart { private $items; public function __construct()
{ $this->items = []; } public function addItem(Item $item) { $this->items[] = $item; } (snip) } QSJWBUF λΠϓώϯςΟϯά -
$BSU class Cart { (snip) public function addItem(Item $item) {
if (count($this->items) >= 3) { throw new PreconditionException(); } $this->items[] = $item; } (snip) } -
$BSU $cart = new Cart(); $item1 = new Item('code1',
100); $cart->addItem($item1); $item2 = new Item('code2', 200); $cart->addItem($item2); $this->assertEquals(324, $cart->calculateTotalPrice()); $this->assertEquals(324, $cart->calculateTotalPrice()); ʹͳΔʁʁʁ -
$BSU class Cart { (snip) public function calculateTotalPrice(): int {
$totalPrice = 0; foreach ($this->items as $item) { $item->price = (int)($item->price * 1.08); $totalPrice = $totalPrice + $item->price; } return $totalPrice; } (snip) } *UFNQSJDF͕ มߋ͞ΕΔ ճݺͿͱ ͞ΒʹഒʹͳΔ -
*UFN class Item { private $code; private $price; public
function __construct(string $code, int $price) { $this->code = $code; $this->price = $price; } public function price(): int { return $this->price; } } QSJWBUF -
$BSU class Cart { (snip) public function calculateTotalPrice(): int {
$totalPrice = 0; foreach ($this->items as $item) { $price = (int)($item->price() * 1.08); $totalPrice = $totalPrice + $price; } return $totalPrice; } (snip) } *UFNQSJDF มߋͰ͖ͳ͍ -
1SJDF class Price { const TAX_RATE = 1.08; private $price;
public function __construct(int $price) { $this->price = $price; } public function add(self $price): self { return new self($this->price + $price->price); } public function withTax(): self { return new self((int)($this->price * static::TAX_RATE)); } public function toInt(): int { return $this->price; } } -
*UFN class Item { private $code; private $price; public
function __construct(string $code, int $price) { $this->code = $code; $this->price = new Price($price); } public function price(): Price { return $this->price; } } -
$BSU class Cart { (snip) public function calculateTotalPrice(): Price {
$totalPrice = new Price(0); foreach ($this->items as $item) { $price = $item->price()->withTax(); $totalPrice = $totalPrice->add($price); } return $totalPrice; } (snip) } Ձ֨1SJDFͰදݱ ੫ࠐՁ֨ࢉग़ Ձ֨ͷ͠ࢉ -
"QJ$POUSPMMFS class ApiController { public function item() { $item =
new Item('code1', 100); $this->json($item); } private function json(Item $entity) { header('Content-Type: application/json'); echo json_encode($entity->toArray()); } } -
class ApiController { public function item() { $item = new
Item('code1', 100); $this->json($item); } public function cart() { $cart = new Cart(); $cart->addItem(new Item('code1', 100)); $cart->addItem(new Item('code2', 200)); $this->json($cart); } private function json(Item $entity) { header('Content-Type: application/json'); echo json_encode($entity->toArray()); } } DBSU *UFNΫϥεͰͳ͍ -
*UFN class Item implements Arrayable { (snip) public function toArray():
array { return [ 'code' => $this->code, 'price' => $this->price->toInt(), ]; } } -
$BSU class Cart implements Arrayable { (snip) public function toArray():
array { return array_map(function (Item $item) { return $item->toArray(); }, $this->items); } } -
class ApiController { public function item() { $item = new
Item('code1', 100); $this->json($item); } public function cart() { $cart = new Cart(); $cart->addItem(new Item('code1', 100)); $cart->addItem(new Item('code2', 200)); $this->json($cart); } private function json(Arryable $entity) { header('Content-Type: application/json'); echo json_encode($entity->toArray()); } } "SSBZBCMF