-
Notifications
You must be signed in to change notification settings - Fork 4
/
structural-bridge-3.php
113 lines (96 loc) · 2.58 KB
/
structural-bridge-3.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
<?php
/**
* This example just to simulate how Bridge Combine with Builder
* 2nees.com
*/
abstract class Message
{
private string $message;
protected MessageSender $messageSender;
public function getMessage(): string
{
return $this->message;
}
public function setMessage($message): void
{
$this->message = $message;
}
public function setMessageSender(MessageSender $messageSender): void
{
$this->messageSender = $messageSender;
}
public abstract function sendMessage(): void;
}
class HtmlMessage extends Message
{
public function sendMessage(): void
{
if (strip_tags($this->getMessage(), ['p', 'i', 'b']) !== $this->getMessage()) {
echo "Html message Can accept only p, i, and b tags!!" . PHP_EOL;
return;
}
$this->messageSender->send($this->getMessage());
}
}
class PlanTextMessage extends Message
{
public function sendMessage(): void
{
if (strip_tags($this->getMessage()) !== $this->getMessage()) {
echo "Plan Text message cant accept Tags!" . PHP_EOL;
return;
}
$this->messageSender->send($this->getMessage());
}
}
interface MessageSender
{
public function send($message): void;
}
class SMSMessage implements MessageSender
{
public function send($message): void
{
echo "SMS Message: {$message} Sent!" . PHP_EOL;
}
}
class EmailMessage implements MessageSender
{
public function send($message): void
{
echo "to: 2nees.com; Body: {$message}; Subject: New Message From 2nees.com" . PHP_EOL;
}
}
/**
* This is a Director Class which its execute the building steps in particular sequence
* 2nees.com
*/
class Director
{
private Message $message;
public function setBuilder(Message $message): void
{
$this->message = $message;
}
public function sendSMS()
{
$sms = new SMSMessage();
// Send plan text message
$this->message->setMessageSender($sms);
$this->message->setMessage("Verification Code => 123456");
$this->message->sendMessage();
}
public function sendEmail()
{
$email = new EmailMessage();
$this->message->setMessageSender($email);
$this->message->setMessage("<p><b>Verification Code</b> => <i>123456</i></p>");
$this->message->sendMessage();
}
}
$dir = new Director();
$dir->setBuilder(new PlanTextMessage());
$dir->sendSMS();
echo "==================================================" . PHP_EOL;
$dir->setBuilder(new HtmlMessage());
$dir->sendEmail();