-
Notifications
You must be signed in to change notification settings - Fork 4
/
behavioral-observer-2.php
195 lines (172 loc) · 5.29 KB
/
behavioral-observer-2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* This example just to simulate how Observer can be work And how we can build a group of events
* 2nees.com
*/
const COMMON_EVENTS_GROUP = "*";
const CUSTOMER_SET_DOWN = "customer:set";
const INVOICE_GROUP = "accountant";
/**
* Class Restaurant - Publisher (Subject) - subscription infrastructure that lets new subscribers join/left
*/
class Restaurant implements SplSubject {
private array $observers;
private int $tableNumber = 0;
private int $numberOfCustomer = 0;
/**
* Restaurant constructor.
*/
public function __construct()
{
$this->observers['*'] = [];
}
/**
* @param string $event
*/
private function initEventGroup(string $event = COMMON_EVENTS_GROUP): void
{
if (!isset($this->observers[$event])) {
$this->observers[$event] = [];
}
}
/**
* Merge Common events with spesfic events
*
* @param string $event
* @return array
*/
private function getEventObservers(string $event = COMMON_EVENTS_GROUP): array
{
$this->initEventGroup($event);
$group = $this->observers[$event];
$all = $this->observers[COMMON_EVENTS_GROUP];
return array_merge($group, $all);
}
/**
* @param SplObserver $observer
* @param string $event
*/
public function attach(SplObserver $observer, string $event = COMMON_EVENTS_GROUP)
{
// Init event group if not exsits
$this->initEventGroup($event);
// Build key
$key = spl_object_hash($observer);
// Assign new object
$this->observers[$event][$key] = $observer;
}
public function detach(SplObserver $observer, string $event = COMMON_EVENTS_GROUP)
{
foreach ($this->getEventObservers($event) as $key => $ob){
if($key === spl_object_hash($ob)){
unset($this->observers[$event][$key]);
}
}
}
public function notify(string $event = COMMON_EVENTS_GROUP)
{
foreach ($this->getEventObservers($event) as $observer) {
$observer->update($this, $event);
}
}
/**
* Logic set here, we add notify if customer set down on table
*/
public function customerSetOnTable(){
$this->numberOfCustomer = rand(1, 5);
$this->tableNumber += 1;
$this->notify(CUSTOMER_SET_DOWN);
// Call another logic...like step 2
$this->createInvoice();
}
/**
* Logic set here, we add notify if customer set down on table
*/
public function createInvoice(){
// do your logic...
echo "We are doing some actions..." . PHP_EOL;
// Notify
$this->notify(INVOICE_GROUP);
}
public function getTableNumber(): int
{
return $this->tableNumber;
}
public function getNumberOfCustomer(): int
{
return $this->numberOfCustomer;
}
}
/**
* Class Waiter - Concrete Subscribers - implement some action when notify received...
*/
class Waiter implements SplObserver {
public function update(SplSubject $subject, string $event = null)
{
/**
* @var Restaurant $subject
*/
echo "Hay!, We have a new customer here, check Table Number #{$subject->getTableNumber()}" . PHP_EOL;
}
}
/**
* Class Kitchen - Concrete Subscribers - implement some action when notify received...
*/
class Kitchen implements SplObserver {
public function update(SplSubject $subject, string $event = null)
{
/**
* @var Restaurant $subject
*/
echo "Hay!, We have an order for
Table Number #{$subject->getTableNumber()}
And We have {$subject->getNumberOfCustomer()} Customer" . PHP_EOL;
}
}
/**
* Class Accountant - Concrete Subscribers - implement some action when notify received...
*/
class Accountant implements SplObserver {
public function update(SplSubject $subject, string $event = null)
{
/**
* @var Restaurant $subject
*/
$numberOfCustomer = $subject->getNumberOfCustomer();
$initPrice = $numberOfCustomer * 0.5;// Number of user * half JOD
echo PHP_EOL. "Hay!, Invoice Start for
Table Number #{$subject->getTableNumber()}
And We have {$numberOfCustomer} Customer
And total init price is {$initPrice} JOD
" . PHP_EOL;
}
}
/**
* Class Accountant - Concrete Subscribers - implement some action when notify received...
*/
class Logger implements SplObserver {
public function update(SplSubject $subject, string $event = null)
{
/**
* @var Restaurant $subject
*/
echo "TrackHistory: {$event} - {$subject->getTableNumber()}" . PHP_EOL;
}
}
// Client
$restaurant = new Restaurant();
$kitchen = new Kitchen();
$waiter= new Waiter();
$accountant = new Accountant();
$logger = new Logger();
$restaurant->attach($logger);
$restaurant->attach($waiter, CUSTOMER_SET_DOWN);
$restaurant->attach($kitchen, CUSTOMER_SET_DOWN);
$restaurant->attach($accountant, INVOICE_GROUP);
$restaurant->customerSetOnTable();
echo "=======================================" . PHP_EOL;
$restaurant->customerSetOnTable();
echo "=======================================" . PHP_EOL;
$restaurant->detach($logger);
$restaurant->detach($accountant, INVOICE_GROUP);
$restaurant->customerSetOnTable();