-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Php examples #66
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
base: master
Are you sure you want to change the base?
Php examples #66
Changes from 18 commits
10fc66b
369e83a
c1ebc44
1c9423f
2c1f113
e85ad07
6062aa2
6fa88a8
f590f14
316fcd2
444b2f5
404217e
7ed5d2d
ca96959
b777cc1
0cd70fe
a52e9ca
f7c1310
47c5081
c56150b
e1f470a
2cf6864
27d9a76
b9b1fa6
1eb2fab
4190967
668d73d
ce83c6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# IntelliJ project files | ||
.idea | ||
*.iml | ||
out | ||
gen |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\Bank; | ||
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\BitCoin; | ||
use designPatternsForHumans\behavioral\Chain_Of_Responsibility\Paypal; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
// Let's prepare a chain like below | ||
// $bank->$paypal->$bitcoin | ||
|
||
$bank = new Bank(100); | ||
$paypal = new Paypal(200); | ||
$bitcoin = new BitCoin(300); | ||
|
||
// First priority bank | ||
// If bank can't pay then paypal | ||
// If paypal can't pay then bit coin. | ||
$bank->setNext($paypal); | ||
$paypal->setNext($bitcoin); | ||
|
||
// Let's try and pay our bill. | ||
$bank->pay(259); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use composer to set psr-4 and avoid put this line everywhere There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is true, but as far as I'm concerned, I tried to make these examples as such that they are not dependent on Composer or any such 'mechanism', hence why I wrote my own little autoloader. I might rework this to only have one autoload.php, but at the time I was thinking that maybe people wanted to test one set of examples, that is why I used the require_once on each of the examples. |
||
|
||
use designPatternsForHumans\behavioral\Command\Bulb; | ||
use designPatternsForHumans\behavioral\Command\RemoteControl; | ||
use designPatternsForHumans\behavioral\Command\TurnOff; | ||
use designPatternsForHumans\behavioral\Command\TurnOn; | ||
|
||
$bulb = new Bulb(); | ||
|
||
$turnOn = new TurnOn($bulb); | ||
$turnOff = new TurnOff($bulb); | ||
|
||
$remote = new RemoteControl(); | ||
$remote->submit($turnOn); | ||
$remote->submit($turnOff); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use designPatternsForHumans\behavioral\Iterator\RadioStation; | ||
use designPatternsForHumans\behavioral\Iterator\StationList; | ||
|
||
require_once __DIR__ . '/autoload.php'; | ||
|
||
$stationList = new StationList(); | ||
|
||
$stationList->addStation(new RadioStation(89)); | ||
$stationList->addStation(new RadioStation(101)); | ||
$stationList->addStation(new RadioStation(102)); | ||
$stationList->addStation(new RadioStation(103.2)); | ||
|
||
/** @var RadioStation $station */ | ||
echo 'These are the known Radio Stations' . PHP_EOL; | ||
foreach ($stationList as $station) { | ||
echo $station->getFrequency(). PHP_EOL; | ||
} | ||
|
||
$stationList->removeStation(new RadioStation(89)); | ||
|
||
// To prove the station was indeed removed, we iterate through the list again. | ||
// Small caveat : Iterators need to be rewinded after beeing Iterated! | ||
$stationList->rewind(); | ||
|
||
/** @var RadioStation $station */ | ||
echo 'These are the known Radio Stations' . PHP_EOL; | ||
foreach ($stationList as $station) { | ||
echo $station->getFrequency(). PHP_EOL; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
spl_autoload_register(function($className) { | ||
$explode = explode("\\", $className); | ||
$slice = array_slice($explode, -2, 2); | ||
require_once 'src' . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $slice) . '.php'; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
abstract class Account | ||
{ | ||
protected $balance; | ||
/** @var Account */ | ||
protected $successor; | ||
|
||
public function setNext(Account $account) | ||
{ | ||
$this->successor = $account; | ||
} | ||
|
||
public function pay($amountToPay) | ||
{ | ||
if ($this->canPay($amountToPay)) { | ||
echo sprintf('Paid %s using %s' . PHP_EOL, $amountToPay, | ||
get_called_class()); | ||
} elseif ($this->successor) { | ||
echo sprintf('Cannot pay using %s. Proceeding ...' . PHP_EOL, | ||
get_called_class()); | ||
$this->successor->pay($amountToPay); | ||
} else { | ||
throw new \Exception('None of the accounts have enough balance'); | ||
} | ||
} | ||
|
||
public function canPay($amount) | ||
{ | ||
return $this->balance >= $amount; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
|
||
class Bank extends Account | ||
{ | ||
// In the example the balance property is redefined, but that is not | ||
// necessary, since we extend Account, which has that property. | ||
|
||
|
||
public function __construct($balance) | ||
{ | ||
$this->balance = $balance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
|
||
class BitCoin extends Account | ||
{ | ||
// In the example the balance property is redefined, but that is not | ||
// necessary, since we extend Account, which has that property. | ||
|
||
|
||
public function __construct($balance) | ||
{ | ||
$this->balance = $balance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Chain_Of_Responsibility; | ||
|
||
|
||
class Paypal extends Account | ||
{ | ||
// In the example the balance property is redefined, but that is not | ||
// necessary, since we extend Account, which has that property. | ||
|
||
|
||
public function __construct($balance) | ||
{ | ||
$this->balance = $balance; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
|
||
// This is the receiver, it has the implementation of the actions we want to perform. | ||
class Bulb | ||
{ | ||
public function turnOn() | ||
{ | ||
echo 'Bulb has been lit!' . PHP_EOL; | ||
} | ||
|
||
public function turnOff() | ||
{ | ||
echo 'Darkness!' . PHP_EOL; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
|
||
interface Command | ||
{ | ||
public function execute(); | ||
public function undo(); | ||
public function redo(); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
// This is the invoker, this will be used by client to interact. | ||
class RemoteControl | ||
{ | ||
public function submit(Command $command) | ||
{ | ||
$command->execute(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
|
||
class TurnOff implements Command | ||
{ | ||
|
||
protected $bulb; | ||
|
||
public function __construct(Bulb $bulb) | ||
{ | ||
$this->bulb = $bulb; | ||
} | ||
|
||
public function execute() | ||
{ | ||
$this->bulb->turnOff(); | ||
} | ||
|
||
public function undo() | ||
{ | ||
$this->bulb->turnOn(); | ||
} | ||
|
||
public function redo() | ||
{ | ||
$this->execute(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Command; | ||
|
||
|
||
class TurnOn implements Command | ||
{ | ||
|
||
protected $bulb; | ||
|
||
public function __construct(Bulb $bulb) | ||
{ | ||
$this->bulb = $bulb; | ||
} | ||
|
||
public function execute() | ||
{ | ||
$this->bulb->turnOn(); | ||
} | ||
|
||
public function undo() | ||
{ | ||
$this->bulb->turnOff(); | ||
} | ||
|
||
public function redo() | ||
{ | ||
$this->execute(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Iterator; | ||
|
||
|
||
class RadioStation | ||
{ | ||
protected $frequency; | ||
|
||
public function __construct($frequency) | ||
{ | ||
$this->frequency = $frequency; | ||
} | ||
|
||
public function getFrequency() | ||
{ | ||
return $this->frequency; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace designPatternsForHumans\behavioral\Iterator; | ||
|
||
use Countable; | ||
use Iterator; | ||
|
||
class StationList implements Countable, Iterator | ||
{ | ||
|
||
protected $stations = []; | ||
protected $counter; | ||
|
||
public function addStation(RadioStation $station) | ||
{ | ||
$this->stations[] = $station; | ||
} | ||
|
||
public function removeStation(RadioStation $toRemove) | ||
{ | ||
$toRemoveFrequency = $toRemove->getFrequency(); | ||
$this->stations = array_filter($this->stations, function (RadioStation $station) use ($toRemoveFrequency) { | ||
return $station->getFrequency() !== $toRemoveFrequency; | ||
}); | ||
} | ||
|
||
public function current() | ||
{ | ||
return $this->stations[$this->counter]; | ||
} | ||
|
||
public function next() | ||
{ | ||
$this->counter++; | ||
} | ||
|
||
public function key() | ||
{ | ||
return $this->counter; | ||
} | ||
|
||
public function valid() | ||
{ | ||
return isset($this->stations[$this->counter]); | ||
} | ||
|
||
public function rewind() | ||
{ | ||
// When using a numbered array like we do with $counter, we need to reset | ||
// the array as well, otherwise the removed station will wreak havoc with | ||
// our iteration, because the value of removed station at position X will | ||
// be NULL; | ||
$this->stations = array_values($this->stations); | ||
return $this->counter = 0; | ||
} | ||
|
||
public function count() | ||
{ | ||
return count($this->stations); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IntelliJ files dont belong here, They should be in you global gitignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically speaking, also correct. The file can be deleted as such, since it really doesn't serve any purpose in the repo anyway. Tnx