|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Qoraiche\MailEclipse\Utils; |
|
4
|
|
|
|
|
5
|
|
|
class Replacer |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* The pattern for the blade syntax to replace. |
|
9
|
|
|
* eg `'/@component/i'`. |
|
10
|
|
|
* |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $bladeMatchPatterns = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* The syntax that replaces the blade files. |
|
17
|
|
|
* eg `'[component]: # '`. |
|
18
|
|
|
* |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $bladeReplacePatterns = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The patterns of the editor to match. |
|
25
|
|
|
* eg `'/\[component]:\s?#\s?/i'`. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $editorMatchPatterns = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The blade syntax to change to from the editor. |
|
33
|
|
|
* eg `'@component'`. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $editorReplacePatterns = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new instance of the replacer. |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->generateRegex(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Take the format of the Blade syntax and convert to one the editor understands. |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $content |
|
53
|
|
|
* @return string|string[]|null |
|
54
|
|
|
*/ |
|
55
|
|
|
public static function toEditor($content) |
|
56
|
|
|
{ |
|
57
|
|
|
$self = new self(); |
|
58
|
|
|
|
|
59
|
|
|
return $self->replace($self->bladeMatchPatterns, $self->bladeReplacePatterns, $content); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Take the data from the editor and return a format that the Blade syntax. |
|
64
|
|
|
* |
|
65
|
|
|
* @param mixed $content |
|
66
|
|
|
* @return string|string[]|null |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function toBlade($content): string |
|
69
|
|
|
{ |
|
70
|
|
|
$self = new self(); |
|
71
|
|
|
|
|
72
|
|
|
return $self->replace($self->editorMatchPatterns, $self->editorReplacePatterns, $content); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Replace the content with the patterns and targeted format. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $patterns |
|
79
|
|
|
* @param array $replacements |
|
80
|
|
|
* @param mixed $content |
|
81
|
|
|
* @return string|string[]|null |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function replace(array $patterns, array $replacements, $content) |
|
84
|
|
|
{ |
|
85
|
|
|
return preg_replace($patterns, $replacements, $content); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Generate the regex patterns from a single list. |
|
90
|
|
|
* |
|
91
|
|
|
* This also ensures that the replace array is the same length as the match array |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
* |
|
95
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function generateRegex() |
|
98
|
|
|
{ |
|
99
|
|
|
$components = config('maileclipse.components'); |
|
100
|
|
|
|
|
101
|
|
|
$this->bladeMatchPatterns = array_map(function ($component) { |
|
102
|
|
|
return "/@${component}/i"; |
|
103
|
|
|
}, $components); |
|
104
|
|
|
|
|
105
|
|
|
$this->bladeReplacePatterns = array_map(function ($component) { |
|
106
|
|
|
return "[${component}]: # "; |
|
107
|
|
|
}, $components); |
|
108
|
|
|
|
|
109
|
|
|
$this->editorMatchPatterns = array_map(function ($component) { |
|
110
|
|
|
return "/\[${component}]:\s?#\s?/i"; |
|
111
|
|
|
}, $components); |
|
112
|
|
|
|
|
113
|
|
|
$this->editorReplacePatterns = array_map(function ($component) { |
|
114
|
|
|
return "@${component}"; |
|
115
|
|
|
}, $components); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|