Completed
Push — master ( 525186...0596c3 )
by Frederic
03:07
created

Shortcoder::formatPattern()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4285
cc 2
eloc 13
nc 2
nop 2
1
<?php
2
3
namespace Seiler\Shortcoder;
4
5
class Shortcoder
6
{
7
    /**
8
     * The shortcodes stack
9
     *
10
     * @var array
11
     */
12
    protected $shortcodes = [];
13
14
    /**
15
     * Create a new Shortcoder instance
16
     *
17
     * @param array|null|string $pattern
18
     * @param null|string       $replacement
19
     * @param mixed             $regex
20
     */
21
    public function __construct($pattern = null, $replacement = null, $regex = null)
22
    {
23
        $this->set($pattern, $replacement, $regex);
24
    }
25
26
    /**
27
     * Add shortcodes to the stack
28
     *
29
     * @param  array|null|string $pattern
30
     * @param  null|string       $replacement
31
     * @param  mixed             $regex
32
     *
33
     * @return \Seiler\Shortcoder\Shortcoder
34
     */
35
    public function add($pattern = null, $replacement = null, $regex = null)
36
    {
37
        // Called with arguments
38
        if (!is_array($pattern)) {
39
            $pattern = compact('pattern', 'replacement', 'regex');
40
        }
41
42
        // Called with only one shortcode
43
        if (!is_array(reset($pattern))) {
44
            $pattern = [$pattern];
45
        }
46
47
        foreach ($pattern as $attributes) {
48
            $pattern = array_key_exists('pattern', $attributes) ?
49
                $attributes['pattern'] : key($attributes);
50
51
            if (empty($pattern)) {
52
                continue;
53
            }
54
55
            $replacement = array_key_exists('replacement', $attributes) ?
56
                $attributes['replacement'] : reset($attributes);
57
58
            $regex = array_key_exists('regex', $attributes) ?
59
                $attributes['regex'] : null;
60
61
            $shortcode = [
62
                'pattern'     => $this->formatPattern($pattern, $regex),
63
                'replacement' => $this->formatReplacement($replacement, $regex)
64
            ];
65
66
            if (!in_array($shortcode, $this->shortcodes, true)) {
67
                $this->shortcodes[] = $shortcode;
68
            }
69
        }
70
71
        return $this;
72
    }
73
74
    /**
75
     * Format the given pattern
76
     *
77
     * @param  string $pattern
78
     * @param  mixed  $regex
79
     *
80
     * @return string
81
     */
82
    protected function formatPattern($pattern, $regex = null)
83
    {
84
        if (!empty($regex)) {
85
            return $pattern;
86
        }
87
88
        $pattern = preg_quote($pattern, '/');
89
90
        $pattern = preg_replace([
91
            '/\\\\\*/',        // 1 Replace all '*'...
92
            '/\s+/',           // 2 Replace all white-spaces...
93
            '/(\(\.\*\?\))$/', // 3 Replace any closing lazy catch-all...
94
        ], [
95
            '(.*?)',           // 1 ...with a lazy catch-all
96
            '\s',              // 2 ...with a 'white-space' regex pattern
97
            '(.*)'             // 3 ...with a greedy one
98
        ], $pattern);
99
100
        return "/$pattern/s";
101
    }
102
103
    /**
104
     * Format the given replacement
105
     *
106
     * @param  string $replacement
107
     * @param  mixed  $regex
108
     *
109
     * @return string
110
     */
111
    protected function formatReplacement($replacement = '', $regex = null)
112
    {
113
        if (!empty($regex)) {
114
            return $replacement;
115
        }
116
117
        $backReferences = [];
118
119
        if (preg_match_all('/\$(\d){1,2}/', $replacement, $matches)) {
120
            $backReferences = $matches[1];
121
        }
122
123
        while (strpos($replacement, '*') !== false) {
124
            $index = 1;
125
126
            while (in_array((string)$index, $backReferences, true)) {
127
                $index++;
128
            }
129
130
            $backReferences[] = (string)$index;
131
            $replacement = preg_replace('/\*/', '\$' . $index, $replacement, 1);
132
        }
133
134
        return $replacement;
135
    }
136
137
    /**
138
     * Parse the given text with stacked shortcodes
139
     *
140
     * @param  string $text
141
     *
142
     * @return string
143
     */
144
    public function parse($text = '')
145
    {
146
        return preg_replace(
147
            array_column($this->shortcodes, 'pattern'),
148
            array_column($this->shortcodes, 'replacement'),
149
            $text
150
        );
151
    }
152
153
    /**
154
     * Replace the stack with new shortcodes
155
     *
156
     * @param  array|null|string $pattern
157
     * @param  null|string       $replacement
158
     * @param  mixed             $regex
159
     *
160
     * @return \Seiler\Shortcoder\Shortcoder
161
     */
162
    public function set($pattern = null, $replacement = null, $regex = null)
163
    {
164
        return $this->flush()->add($pattern, $replacement, $regex);
165
    }
166
167
    /**
168
     * Flush the stack
169
     *
170
     * @return \Seiler\Shortcoder\Shortcoder
171
     */
172
    public function flush()
173
    {
174
        $this->shortcodes = [];
175
176
        return $this;
177
    }
178
}
179