1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Da\Mailer\Model; |
4
|
|
|
|
5
|
|
|
use Da\Mailer\Exception\InvalidCallException; |
6
|
|
|
use Da\Mailer\Exception\UnknownPropertyException; |
7
|
|
|
use Exception; |
8
|
|
|
|
9
|
|
|
abstract class AbstractMailObject |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Constructor. |
13
|
|
|
* |
14
|
|
|
* Takes an array of name-value pairs and sets the attribute of the object. |
15
|
|
|
* |
16
|
|
|
* @param array $config |
17
|
33 |
|
*/ |
18
|
|
|
protected function __construct(array $config = []) |
19
|
33 |
|
{ |
20
|
33 |
|
foreach ($config as $name => $value) { |
21
|
33 |
|
if (is_string($name) && $value !== null) { |
22
|
33 |
|
$this->{$name} = $value; |
23
|
33 |
|
} |
24
|
33 |
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Helper function to work as a builder for collection items iterations. |
29
|
|
|
* |
30
|
|
|
* @param $array |
31
|
|
|
* |
32
|
|
|
* @return static |
33
|
9 |
|
*/ |
34
|
|
|
public static function fromArray(array $array) |
35
|
9 |
|
{ |
36
|
|
|
return new static($array); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Sets value of an object property. |
41
|
|
|
* |
42
|
|
|
* Do not call this method directly as it is a PHP magic method that |
43
|
|
|
* will be implicitly called when executing `$object->property = $value;`. |
44
|
|
|
* |
45
|
|
|
* @param string $name the property name or the event name |
46
|
|
|
* @param mixed $value the property value |
47
|
|
|
* |
48
|
|
|
* @throws Exception if the property is not defined or read-only |
49
|
26 |
|
*/ |
50
|
|
|
public function __set($name, $value) |
51
|
26 |
|
{ |
52
|
26 |
|
$setter = 'set' . $name; |
53
|
26 |
|
if (method_exists($this, $setter)) { |
54
|
26 |
|
$this->$setter($value); |
55
|
1 |
|
} elseif (method_exists($this, 'get' . $name)) { |
56
|
|
|
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name); |
57
|
1 |
|
} else { |
58
|
|
|
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); |
59
|
26 |
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns the value of an object property. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @throws Exception if the property is not defined or write-only |
68
|
|
|
* |
69
|
|
|
* @return mixed the property value |
70
|
|
|
* |
71
|
3 |
|
*/ |
72
|
|
|
public function __get($name) |
73
|
3 |
|
{ |
74
|
3 |
|
$getter = 'get' . $name; |
75
|
1 |
|
if (method_exists($this, $getter)) { |
76
|
2 |
|
return $this->$getter(); |
77
|
1 |
|
} elseif (method_exists($this, 'set' . $name)) { |
78
|
|
|
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name); |
79
|
1 |
|
} else { |
80
|
|
|
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Checks if a property is set, i.e. defined and not null. |
86
|
|
|
* |
87
|
|
|
* Do not call this method directly as it is a PHP magic method that |
88
|
|
|
* will be implicitly called when executing `isset($object->property)`. |
89
|
|
|
* |
90
|
|
|
* Note that if the property is not defined, false will be returned. |
91
|
|
|
* |
92
|
|
|
* @param string $name the property name or the event name |
93
|
|
|
* |
94
|
|
|
* @return bool whether the named property is set (not null). |
95
|
|
|
* |
96
|
|
|
* @see http://php.net/manual/en/function.isset.php |
97
|
2 |
|
*/ |
98
|
|
|
public function __isset($name) |
99
|
2 |
|
{ |
100
|
2 |
|
$getter = 'get' . $name; |
101
|
1 |
|
if (method_exists($this, $getter)) { |
102
|
|
|
return $this->$getter() !== null; |
103
|
2 |
|
} else { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Sets an object property to null. |
110
|
|
|
* |
111
|
|
|
* Do not call this method directly as it is a PHP magic method that |
112
|
|
|
* will be implicitly called when executing `unset($object->property)`. |
113
|
|
|
* |
114
|
|
|
* Note that if the property is not defined, this method will do nothing. |
115
|
|
|
* If the property is read-only, it will throw an exception. |
116
|
|
|
* |
117
|
|
|
* @param string $name the property name |
118
|
|
|
* |
119
|
|
|
* @throws InvalidCallException if the property is read only. |
120
|
|
|
* |
121
|
|
|
* @see http://php.net/manual/en/function.unset.php |
122
|
2 |
|
*/ |
123
|
|
|
public function __unset($name) |
124
|
2 |
|
{ |
125
|
2 |
|
$setter = 'set' . $name; |
126
|
1 |
|
if (method_exists($this, $setter)) { |
127
|
2 |
|
$this->$setter(null); |
128
|
1 |
|
} elseif (method_exists($this, 'get' . $name)) { |
129
|
|
|
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name); |
130
|
1 |
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|