|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/easy-sms. |
|
5
|
|
|
* (c) overtrue <[email protected]> |
|
6
|
|
|
* This source file is subject to the MIT license that is bundled |
|
7
|
|
|
* with this source code in the file LICENSE. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Overtrue\EasySms; |
|
11
|
|
|
|
|
12
|
|
|
use Overtrue\EasySms\Contracts\MessageInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Message. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Message implements MessageInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $gateways = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $type; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $content; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $template; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $data = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Message constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $attributes |
|
48
|
|
|
* @param string $type |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(array $attributes = [], $type = MessageInterface::TEXT_MESSAGE) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->type = $type; |
|
53
|
|
|
|
|
54
|
|
|
foreach ($attributes as $property => $value) { |
|
55
|
|
|
if (property_exists($this, $property)) { |
|
56
|
|
|
$this->$property = $value; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return the message type. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getMessageType() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->type; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Return message content. |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getContent() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->content; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return the template id of message. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getTemplate() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->template; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $type |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setType(string $type) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->type = $type; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param mixed $content |
|
105
|
|
|
* |
|
106
|
|
|
* @return $this |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setContent($content) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->content = $content; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param mixed $template |
|
117
|
|
|
* |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setTemplate($template) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->template = $template; |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getData() |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->data; |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param array $data |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setData(array $data) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->data = $data; |
|
143
|
|
|
|
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getGateways() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->gateways; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param array $gateways |
|
157
|
|
|
* |
|
158
|
|
|
* @return $this |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setGateways(array $gateways) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->gateways = $gateways; |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param $property |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public function __get($property) |
|
173
|
|
|
{ |
|
174
|
|
|
if (property_exists($this, $property)) { |
|
175
|
|
|
return $this->$property; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.