Skip to content
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# IntelliJ project files
Copy link
Contributor

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

Copy link
Author

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

.idea
*.iml
out
gen
23 changes: 23 additions & 0 deletions examples-per-language/php/behavioral/Chain_Of_Responsibility.php
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add newlines at the end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, actually never knew about that one :) Tnx. I'll update when I have the chance :

http://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline

17 changes: 17 additions & 0 deletions examples-per-language/php/behavioral/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

require_once __DIR__ . '/autoload.php';
Copy link

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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);
32 changes: 32 additions & 0 deletions examples-per-language/php/behavioral/Iterator.php
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;
}

7 changes: 7 additions & 0 deletions examples-per-language/php/behavioral/autoload.php
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;
}
}
19 changes: 19 additions & 0 deletions examples-per-language/php/behavioral/src/Command/Bulb.php
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;
}

}
12 changes: 12 additions & 0 deletions examples-per-language/php/behavioral/src/Command/Command.php
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();

}
13 changes: 13 additions & 0 deletions examples-per-language/php/behavioral/src/Command/RemoteControl.php
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();
}

}
30 changes: 30 additions & 0 deletions examples-per-language/php/behavioral/src/Command/TurnOff.php
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();
}
}
30 changes: 30 additions & 0 deletions examples-per-language/php/behavioral/src/Command/TurnOn.php
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();
}
}
19 changes: 19 additions & 0 deletions examples-per-language/php/behavioral/src/Iterator/RadioStation.php
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;
}
}
61 changes: 61 additions & 0 deletions examples-per-language/php/behavioral/src/Iterator/StationList.php
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);
}
}
Loading