1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Parsers; |
4
|
|
|
|
5
|
|
|
use Fenos\Notifynder\Models\NotificationCategory; |
6
|
|
|
use Fenos\Notifynder\Exceptions\ExtraParamsException; |
7
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
8
|
|
|
use Fenos\Notifynder\Models\Notification as ModelNotification; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class NotificationParser. |
12
|
|
|
*/ |
13
|
|
|
class NotificationParser |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Regex-search-rule. |
17
|
|
|
*/ |
18
|
|
|
const RULE = '/\{([a-zA-Z0-9_\.]+)\}/m'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parse a notification and return the body text. |
22
|
|
|
* |
23
|
|
|
* @param ModelNotification $notification |
24
|
|
|
* @return string |
25
|
|
|
* @throws ExtraParamsException |
26
|
|
|
*/ |
27
|
|
|
public function parse($notification) |
28
|
|
|
{ |
29
|
|
|
$category = $notification->category; |
|
|
|
|
30
|
|
|
if (is_null($category)) { |
31
|
|
|
throw (new ModelNotFoundException)->setModel( |
32
|
|
|
NotificationCategory::class, $notification->category_id |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
$text = $category->template_body; |
36
|
|
|
|
37
|
|
|
$specialValues = $this->getValues($text); |
38
|
|
|
if (count($specialValues) > 0) { |
39
|
|
|
$specialValues = array_filter($specialValues, function ($value) use ($notification) { |
40
|
|
|
return ((is_array($notification) && isset($notification[$value])) || (is_object($notification) && isset($notification->$value))) || starts_with($value, ['extra.', 'to.', 'from.']); |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
foreach ($specialValues as $replacer) { |
44
|
|
|
$replace = $this->mixedGet($notification, $replacer); |
45
|
|
|
if (empty($replace) && notifynder_config()->isStrict()) { |
46
|
|
|
throw new ExtraParamsException("The following [$replacer] param required from your category is missing."); |
47
|
|
|
} |
48
|
|
|
$text = $this->replace($text, $replace, $replacer); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $text; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get an array of all placehodlers. |
57
|
|
|
* |
58
|
|
|
* @param string $body |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
|
protected function getValues($body) |
62
|
|
|
{ |
63
|
|
|
$values = []; |
64
|
|
|
preg_match_all(self::RULE, $body, $values); |
65
|
|
|
|
66
|
|
|
return $values[1]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Replace a single placeholder. |
71
|
|
|
* |
72
|
|
|
* @param string $body |
73
|
|
|
* @param string $valueMatch |
74
|
|
|
* @param string $replacer |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function replace($body, $valueMatch, $replacer) |
78
|
|
|
{ |
79
|
|
|
$body = str_replace('{'.$replacer.'}', $valueMatch, $body); |
80
|
|
|
|
81
|
|
|
return $body; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array|object $object |
86
|
|
|
* @param string $key |
87
|
|
|
* @param null|mixed $default |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
|
|
protected function mixedGet($object, $key, $default = null) |
91
|
|
|
{ |
92
|
|
|
if (is_null($key) || trim($key) == '') { |
93
|
|
|
return ''; |
94
|
|
|
} |
95
|
|
|
foreach (explode('.', $key) as $segment) { |
96
|
|
|
if (is_object($object) && isset($object->{$segment})) { |
97
|
|
|
$object = $object->{$segment}; |
98
|
|
|
continue; |
99
|
|
|
} |
100
|
|
View Code Duplication |
if (is_object($object) && method_exists($object, '__get') && ! is_null($object->__get($segment))) { |
|
|
|
|
101
|
|
|
$object = $object->__get($segment); |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
View Code Duplication |
if (is_object($object) && method_exists($object, 'getAttribute') && ! is_null($object->getAttribute($segment))) { |
|
|
|
|
105
|
|
|
$object = $object->getAttribute($segment); |
106
|
|
|
continue; |
107
|
|
|
} |
108
|
|
|
if (is_array($object) && array_key_exists($segment, $object)) { |
109
|
|
|
$object = array_get($object, $segment, $default); |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return value($default); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $object; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.