Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add examples folder and php implementation with unit tests #135

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
vendor
composer.lock
.phpunit.result.cache
50 changes: 50 additions & 0 deletions examples/php/behavioral/chain_of_responsibility/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace design_patterns\behavioral\chain_of_responsibility;

abstract class Account
{
protected ?Account $next = null;
protected float $balance;

public function setNext(Account $account): void
{
$this->next = $account;
}

public function pay(float $amountToPay): string
{
if ($this->canPay($amountToPay)) {
return $this->payUsing($amountToPay);
}

if ($this->next) {
return $this->cantPayUsing() . $this->next->pay($amountToPay);
}

return 'Cannot pay, no more accounts in the chain';
}

private function canPay(float $amount): bool
{
return $this->balance >= $amount;
}

private function payUsing(float $amountToPay): string
{
$this->balance -= $amountToPay;

return sprintf('Paid %s using %s', $amountToPay, $this->getClassName());
}

private function cantPayUsing(): string
{
return sprintf('Cannot pay using %s. Proceeding ..' . PHP_EOL, $this->getClassName());
}

private function getClassName(): string
{
$className = explode('\\', static::class);
return array_pop($className);
}
}
13 changes: 13 additions & 0 deletions examples/php/behavioral/chain_of_responsibility/Bank.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace design_patterns\behavioral\chain_of_responsibility;

class Bank extends Account
{
protected float $balance;

public function __construct(float $balance)
{
$this->balance = $balance;
}
}
13 changes: 13 additions & 0 deletions examples/php/behavioral/chain_of_responsibility/Bitcoin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace design_patterns\behavioral\chain_of_responsibility;

class Bitcoin extends Account
{
protected float $balance;

public function __construct(float $balance)
{
$this->balance = $balance;
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace design_patterns\behavioral\chain_of_responsibility;

use PHPUnit\Framework\TestCase;


/*
// Let's prepare a chain like below
// $bank->$paypal->$bitcoin
//
// First priority bank
// If bank can't pay then paypal
// If paypal can't pay then bit coin

$bank = new Bank(100); // Bank with balance 100
$paypal = new Paypal(200); // Paypal with balance 200
$bitcoin = new Bitcoin(300); // Bitcoin with balance 300

$bank->setNext($paypal);
$paypal->setNext($bitcoin);

// Let's try to pay using the first priority i.e. bank
$bank->pay(259);

// Output will be
// ==============
// Cannot pay using bank. Proceeding ..
// Cannot pay using paypal. Proceeding ..:
// Paid 259 using Bitcoin!
*/

class ChainOfResponsibilityTest extends TestCase
{
public function test01(): void
{
$bank = new Bank(100);
$paypal = new Paypal(200);
$bitcoin = new Bitcoin(300);

$bank->setNext($paypal);
$paypal->setNext($bitcoin);

self::assertEquals("Cannot pay using Bank. Proceeding ..\nCannot pay using Paypal. Proceeding ..\nPaid 259 using Bitcoin", $bank->pay(259));
}

public function test02(): void
{
$bank = new Bank(100);
$paypal = new Paypal(200);
$bitcoin = new Bitcoin(100);

$bank->setNext($paypal);
$paypal->setNext($bitcoin);

self::assertEquals("Cannot pay using Bank. Proceeding ..\nCannot pay using Paypal. Proceeding ..\nCannot pay, no more accounts in the chain", $bank->pay(259));
}
}
13 changes: 13 additions & 0 deletions examples/php/behavioral/chain_of_responsibility/Paypal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace design_patterns\behavioral\chain_of_responsibility;

class Paypal extends Account
{
protected float $balance;

public function __construct(float $balance)
{
$this->balance = $balance;
}
}
16 changes: 16 additions & 0 deletions examples/php/behavioral/command/Bulb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace design_patterns\behavioral\command;

class Bulb
{
public function turnOn(): string
{
return "Bulb has been lit!";
}

public function turnOff(): string
{
return "Darkness!";
}
}
10 changes: 10 additions & 0 deletions examples/php/behavioral/command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace design_patterns\behavioral\command;

interface Command
{
public function execute(): string;
public function undo(): string;
public function redo(): string;
}
Binary file added examples/php/behavioral/command/Command.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions examples/php/behavioral/command/CommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace design_patterns\behavioral\command;

use PHPUnit\Framework\TestCase;

/*
$bulb = new Bulb();

$turnOn = new TurnOn($bulb);
$turnOff = new TurnOff($bulb);

$remote = new RemoteControl();
$remote->submit($turnOn); // Bulb has been lit!
$remote->submit($turnOff); // Darkness!
*/

class CommandTest extends TestCase
{
public function test01(): void
{
$bulb = new Bulb();

$turnOn = new TurnOn($bulb);
$turnOff = new TurnOff($bulb);

$remote = new RemoteControl();
self::assertEquals('Bulb has been lit!', $remote->submit($turnOn));
self::assertEquals('Darkness!', $remote->submit($turnOff));
}
}
11 changes: 11 additions & 0 deletions examples/php/behavioral/command/RemoteControl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace design_patterns\behavioral\command;

class RemoteControl
{
public function submit(Command $command): string
{
return $command->execute();
}
}
28 changes: 28 additions & 0 deletions examples/php/behavioral/command/TurnOff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace design_patterns\behavioral\command;

class TurnOff implements Command
{
private Bulb $bulb;

public function __construct(Bulb $bulb)
{
$this->bulb = $bulb;
}

public function execute(): string
{
return $this->bulb->turnOff();
}

public function undo(): string
{
return $this->bulb->turnOn();
}

public function redo(): string
{
return $this->execute();
}
}
28 changes: 28 additions & 0 deletions examples/php/behavioral/command/TurnOn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace design_patterns\behavioral\command;

class TurnOn implements Command
{
private Bulb $bulb;

public function __construct(Bulb $bulb)
{
$this->bulb = $bulb;
}

public function execute(): string
{
return $this->bulb->turnOn();
}

public function undo(): string
{
return $this->bulb->turnOff();
}

public function redo(): string
{
return $this->execute();
}
}
1 change: 1 addition & 0 deletions examples/php/behavioral/interpreter/Interpreter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Binary file added examples/php/behavioral/iterator/Iterator.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions examples/php/behavioral/iterator/IteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace design_patterns\behavioral\iterator;

use PHPUnit\Framework\TestCase;

/*
$stationList = new StationList();

$stationList->addStation(new RadioStation(89));
$stationList->addStation(new RadioStation(101));
$stationList->addStation(new RadioStation(102));
$stationList->addStation(new RadioStation(103.2));

foreach($stationList as $station) {
echo $station->getFrequency() . PHP_EOL;
}

$stationList->removeStation(new RadioStation(89)); // Will remove station 89
*/

class IteratorTest extends TestCase
{
public function test01(): void
{
$stationList = new StationList();

$stationList->addStation(new RadioStation(89));
$stationList->addStation(new RadioStation(101));
$stationList->addStation(new RadioStation(102));
$stationList->addStation(new RadioStation(103.2));

self::assertEquals(4, $stationList->count());

$expect = [89, 101, 102, 103.2];
foreach($stationList as $key => $station) {
self::assertEquals($expect[$key], $station->getFrequency());
}

$stationList->removeStation(new RadioStation(89));
self::assertEquals(3, $stationList->count());
}
}
18 changes: 18 additions & 0 deletions examples/php/behavioral/iterator/RadioStation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace design_patterns\behavioral\iterator;

class RadioStation
{
private float $frequency;

public function __construct(float $frequency)
{
$this->frequency = $frequency;
}

public function getFrequency(): float
{
return $this->frequency;
}
}