HtmlTagReplace::replaceTag()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 47
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 4
eloc 25
c 2
b 0
f 0
nc 8
nop 7
dl 0
loc 47
rs 9.52
1
<?php
2
3
namespace HtmlTagReplace;
4
5
/**
6
 * Class HtmlTagReplace
7
 * @package HtmlTagReplace
8
 */
9
class HtmlTagReplace
10
{
11
    /**
12
     * @var string
13
     */
14
    private $markup = '';
15
16
    /**
17
     * HtmlTagReplace constructor.
18
     * @param $markup
19
     */
20
    public function __construct($markup)
21
    {
22
        $this->markup = $markup;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getMarkup()
29
    {
30
        return $this->markup;
31
    }
32
33
    /**
34
     * @param string $markup
35
     */
36
    public function setMarkup($markup)
37
    {
38
        $this->markup = $markup;
39
    }
40
41
    /**
42
     * @param $search
43
     * @param $replace
44
     * @param bool $closingTag
45
     * @param array $argumentsReplace
46
     * @param string $arguments
47
     * @param string $append
48
     * @param string $prepend
49
     * @return $this
50
     */
51
    public function replaceTag(
52
        $search,
53
        $replace,
54
        $closingTag = false,
55
        $argumentsReplace = [],
56
        $arguments = '',
57
        $append = '',
58
        $prepend = ''
59
    )
60
    {
61
        $arguments      = !empty($arguments) ? ' ' . $arguments : '';
62
        $pattern        = '/<' . $search . '(.*?)>';
63
        $replacement    = '<' . $replace . $arguments . '$1>';
64
65
        if ($closingTag) {
66
            $pattern        .= '(.*?)<\/' . $search . '>';
67
            $replacement    .= '$2</' . $replace . '>';
68
        }
69
70
        $pattern .= '/is';
71
        $replacement = $prepend . $replacement . $append;
72
73
        if (empty($argumentsReplace)) {
74
            $this->setMarkup(
75
                preg_replace(
76
                    $pattern,
77
                    $replacement,
78
                    $this->getMarkup()
79
                )
80
            );
81
        } else {
82
            $this->setMarkup(
83
                preg_replace_callback(
84
                    $pattern,
85
                    function($matches) use ($replacement, $argumentsReplace) {
86
                        return $this->replaceArguments(
87
                            $matches,
88
                            $replacement,
89
                            $argumentsReplace
90
                        );
91
                    },
92
                    $this->getMarkup()
93
                )
94
            );
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * @param array $matches
102
     * @param string $replacement
103
     * @param array $argumentsReplace
104
     * @return string
105
     */
106
    private function replaceArguments(
107
        $matches,
108
        $replacement,
109
        $argumentsReplace
110
    )
111
    {
112
        $replacement = str_replace(
113
            ['$1', '$2'],
114
            '%s',
115
            $replacement
116
        );
117
118
        if (isset($matches[0])) {
119
            unset($matches[0]);
120
        }
121
122
        if (isset($matches[1])) {
123
            $arguments = preg_split('/ (?=\w+=)/', $matches[1]);
124
125
            if (is_array($arguments)) {
126
                $arguments = $this->resetArguments($arguments, $argumentsReplace);
127
128
                $matches[1] = join(' ', $arguments);
129
            }
130
        }
131
132
        return vsprintf($replacement, $matches);
133
    }
134
135
    /**
136
     * @param array $arguments
137
     * @param array $argumentsReplace
138
     * @return array
139
     */
140
    private function resetArguments($arguments, $argumentsReplace)
141
    {
142
        foreach ($arguments as $key => $argument) {
143
            $pair = explode('=', $argument);
144
145
            if (!isset($argumentsReplace[trim($pair[0])])) {
146
                continue;
147
            }
148
149
            $newTag = $argumentsReplace[trim($pair[0])];
150
            if (!is_array($newTag)) {
151
                if ($newTag === false) {
152
                    unset($arguments[$key]);
153
                    continue;
154
                }
155
                $pair[0] = $newTag;
156
                $arguments[$key] = join('=', $pair);
157
            } else {
158
                $clones = [];
159
                foreach ($newTag as $clone) {
160
                    $pair[0] = $clone;
161
                    $clones[] = join('=', $pair);
162
                }
163
                $arguments[$key] = join(' ', $clones);
164
            }
165
        }
166
167
        return $arguments;
168
    }
169
170
    /**
171
     * @return $this
172
     */
173
    public function compress()
174
    {
175
        $this->setMarkup(
176
            preg_replace(
177
                ['/\n/', '/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'],
178
                [' ', '>', '<', '\\1'],
179
                $this->getMarkup()
180
            )
181
        );
182
183
        return $this;
184
    }
185
}