1
|
|
|
<?php namespace Arcanesoft\Seo\Helpers; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Arr; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class TextReplacer |
7
|
|
|
* |
8
|
|
|
* @package Arcanesoft\Seo\Helpers |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class TextReplacer |
12
|
|
|
{ |
13
|
|
|
/* ----------------------------------------------------------------- |
14
|
|
|
| Properties |
15
|
|
|
| ----------------------------------------------------------------- |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $replacements; |
20
|
|
|
|
21
|
|
|
/* ----------------------------------------------------------------- |
22
|
|
|
| Constructor |
23
|
|
|
| ----------------------------------------------------------------- |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* TextReplacer constructor. |
28
|
|
|
* |
29
|
|
|
* @param array $replacements |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $replacements = []) |
32
|
|
|
{ |
33
|
|
|
$this->replacements = $replacements; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/* ----------------------------------------------------------------- |
37
|
|
|
| Getters & Setters |
38
|
|
|
| ----------------------------------------------------------------- |
39
|
|
|
*/ |
40
|
|
|
/** |
41
|
|
|
* Get the replacements shortcodes. |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Support\Collection |
44
|
|
|
*/ |
45
|
|
|
public function getShortcodes() |
46
|
|
|
{ |
47
|
|
|
$shortcodes = array_map(function ($replacement) { |
48
|
|
|
return "[{$replacement}]"; |
49
|
|
|
}, $this->replacements); |
50
|
|
|
|
51
|
|
|
return collect(array_combine($shortcodes, $this->replacements)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the replacements shortcodes keys. |
56
|
|
|
* |
57
|
|
|
* @return \Illuminate\Support\Collection |
58
|
|
|
*/ |
59
|
|
|
public function getShortcodesKeys() |
60
|
|
|
{ |
61
|
|
|
return $this->getShortcodes()->keys(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Merge replacements. |
66
|
|
|
* |
67
|
|
|
* @param array $replacements |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Support\Collection |
70
|
|
|
*/ |
71
|
|
|
protected function mergeReplacements(array $replacements) |
72
|
|
|
{ |
73
|
|
|
return $this->getShortcodes() |
74
|
|
|
->transform(function ($key) use ($replacements) { |
75
|
|
|
return Arr::get($replacements, $key); |
76
|
|
|
}) |
77
|
|
|
->filter(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* ----------------------------------------------------------------- |
81
|
|
|
| Main Methods |
82
|
|
|
| ----------------------------------------------------------------- |
83
|
|
|
*/ |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Make the instance. |
87
|
|
|
* |
88
|
|
|
* @param array $replacements |
89
|
|
|
* |
90
|
|
|
* @return static |
91
|
|
|
*/ |
92
|
|
|
public static function make(array $replacements = []) |
93
|
|
|
{ |
94
|
|
|
return new static($replacements); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Replace the content. |
99
|
|
|
* |
100
|
|
|
* @param string $content |
101
|
|
|
* @param array $replacements |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function replace($content, array $replacements = []) |
106
|
|
|
{ |
107
|
|
|
return strtr($content, $this->mergeReplacements($replacements)->toArray()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Highlight the replacement. |
112
|
|
|
* |
113
|
|
|
* @param string $content |
114
|
|
|
* @param string $replacement |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function highlight($content, $replacement = '<code>[\1]</code>') |
119
|
|
|
{ |
120
|
|
|
return preg_replace( |
121
|
|
|
'/\[('.$this->getShortcodes()->values()->implode('|').')\]/', |
122
|
|
|
$replacement, |
123
|
|
|
$content |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|