-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRemoteControl.php
64 lines (55 loc) · 1.45 KB
/
RemoteControl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types = 1);
namespace Maksi\HeadFirstDesignPattern\Command\RemoteControl;
/**
* Class RemoteControl
*
* @package Maksi\HeadFirstDesignPattern\Command\RemoteControl
*/
class RemoteControl
{
/**
* @var Command[]
*/
protected $onCommands;
/**
* @var Command[]
*/
protected $offCommands;
/**
* RemoteControl constructor.
*/
public function __construct()
{
$noCommand = new NoCommand();
for ($i = 0; $i < 7; $i++) {
$this->onCommands[$i] = $noCommand;
$this->offCommands[$i] = $noCommand;
}
}
public function setCommand(int $slot, Command $onCommand, Command $offCommand): void
{
$this->onCommands[$slot] = $onCommand;
$this->offCommands[$slot] = $offCommand;
}
public function onButtonWasPushed(int $slot): void
{
$this->onCommands[$slot]->execute();
}
public function offButtonWasPushed(int $slot): void
{
$this->offCommands[$slot]->execute();
}
/**
* @return string
*/
public function __toString(): string
{
$stringBuff = '';
$stringBuff .= "\n------ Remote Control -------\n";
for ($i = 0, $iMax = \count($this->onCommands); $i < $iMax; $i++) {
$stringBuff .= '[slot ' . $i . '] ' . \get_class($this->onCommands[$i]) . ' ' . \get_class($this->offCommands[$i]) . "\n";
}
return $stringBuff;
}
}