|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Parsers; |
|
4
|
|
|
|
|
5
|
|
|
use Fenos\Notifynder\Models\NotificationCategory; |
|
6
|
|
|
use Fenos\Notifynder\Exceptions\ExtraParamsException; |
|
7
|
|
|
use Fenos\Notifynder\Models\Notification as ModelNotification; |
|
8
|
|
|
use Fenos\Notifynder\Builder\Notification as BuilderNotification; |
|
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 array|ModelNotification|BuilderNotification $notification |
|
24
|
|
|
* @param int $categoryId |
|
25
|
|
|
* @return string |
|
26
|
|
|
* @throws ExtraParamsException |
|
27
|
|
|
*/ |
|
28
|
|
|
public function parse($notification, $categoryId) |
|
29
|
|
|
{ |
|
30
|
|
|
$category = NotificationCategory::findOrFail($categoryId); |
|
31
|
|
|
$text = $category->template_body; |
|
32
|
|
|
|
|
33
|
|
|
$specialValues = $this->getValues($text); |
|
34
|
|
|
if (count($specialValues) > 0) { |
|
35
|
|
|
$specialValues = array_filter($specialValues, function ($value) use ($notification) { |
|
36
|
|
|
return ((is_array($notification) && isset($notification[$value])) || (is_object($notification) && isset($notification->$value))) || starts_with($value, ['extra.', 'to.', 'from.']); |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
foreach ($specialValues as $replacer) { |
|
40
|
|
|
$replace = $this->mixedGet($notification, $replacer); |
|
41
|
|
|
if (empty($replace) && notifynder_config()->isStrict()) { |
|
42
|
|
|
throw new ExtraParamsException("The following [$replacer] param required from your category is missing."); |
|
43
|
|
|
} |
|
44
|
|
|
$text = $this->replace($text, $replace, $replacer); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $text; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get an array of all placehodlers. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $body |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function getValues($body) |
|
58
|
|
|
{ |
|
59
|
|
|
$values = []; |
|
60
|
|
|
preg_match_all(self::RULE, $body, $values); |
|
61
|
|
|
|
|
62
|
|
|
return $values[1]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Replace a single placeholder. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $body |
|
69
|
|
|
* @param string $valueMatch |
|
70
|
|
|
* @param string $replacer |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function replace($body, $valueMatch, $replacer) |
|
74
|
|
|
{ |
|
75
|
|
|
$body = str_replace('{'.$replacer.'}', $valueMatch, $body); |
|
76
|
|
|
|
|
77
|
|
|
return $body; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array|object $object |
|
82
|
|
|
* @param string $key |
|
83
|
|
|
* @param null|mixed $default |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function mixedGet($object, $key, $default = null) |
|
87
|
|
|
{ |
|
88
|
|
|
if (is_null($key) || trim($key) == '') { |
|
89
|
|
|
return ''; |
|
90
|
|
|
} |
|
91
|
|
|
foreach (explode('.', $key) as $segment) { |
|
92
|
|
|
if (is_object($object) && isset($object->{$segment})) { |
|
93
|
|
|
$object = $object->{$segment}; |
|
94
|
|
|
} elseif (is_object($object) && method_exists($object, '__get') && ! is_null($object->__get($segment))) { |
|
95
|
|
|
$object = $object->__get($segment); |
|
96
|
|
|
} elseif (is_object($object) && method_exists($object, 'getAttribute') && ! is_null($object->getAttribute($segment))) { |
|
97
|
|
|
$object = $object->getAttribute($segment); |
|
98
|
|
|
} elseif (is_array($object) && array_key_exists($segment, $object)) { |
|
99
|
|
|
$object = array_get($object, $segment, $default); |
|
100
|
|
|
} else { |
|
101
|
|
|
return value($default); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $object; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|