-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSubject.php
More file actions
45 lines (38 loc) · 1008 Bytes
/
Subject.php
File metadata and controls
45 lines (38 loc) · 1008 Bytes
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
<?php
namespace Imaarov\Patterns\Behavioral\Observer;
use Imaarov\Patterns\Behavioral\Observer\Interface\ObserverInterface;
class Subject {
private array $observers;
/**
* Add Subscriber (Observers) to subscribers array to use it for notification
*
* @param ObserverInterface $observer
* @return void
*/
public function addObserver(ObserverInterface $observer) : void
{
array_push($this->observers, $observer);
}
/**
* Remove an Subscriber(Observer) from array
*
* @param ObserverInterface $observer
* @return void
*/
public function removeObserver(ObserverInterface $observer) : void
{
unset($this->observers[$observer]);
}
/**
* Notify all of the subscriber(Observers)
*
* @param mixed $value
* @return void
*/
public function notify($value)
{
foreach ($this->observers as $key => $observer) {
$observer->update($value);
}
}
}