Completed
Push — master ( 9b3fc3...96b827 )
by ARCANEDEV
06:21
created

TextReplacer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 113
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0

7 Methods

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