• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FluidTYPO3 / vhs / 30001290505

23 Jul 2026 10:57AM UTC coverage: 70.309% (-1.7%) from 72.022%
30001290505

push

github

NamelessCoder
[TER] 8.0.0

4819 of 6854 relevant lines covered (70.31%)

6.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

77.57
/Classes/ViewHelpers/Format/EliminateViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Format;
3

4
/*
5
 * This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10

11
use FluidTYPO3\Vhs\Core\ViewHelper\AbstractViewHelper;
12
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
13

14
/**
15
 * Character/string/whitespace elimination ViewHelper
16
 *
17
 * There is no example - each argument describes how it should be
18
 * used and arguments can be used individually or in any combination.
19
 */
20
class EliminateViewHelper extends AbstractViewHelper
21
{
22
    public function initializeArguments(): void
23
    {
24
        $this->registerArgument('content', 'string', 'String in which to perform replacement');
3✔
25
        $this->registerArgument(
3✔
26
            'caseSensitive',
3✔
27
            'boolean',
3✔
28
            'Wether or not to perform case sensitive replacement',
3✔
29
            false,
3✔
30
            true
3✔
31
        );
3✔
32
        $this->registerArgument(
3✔
33
            'characters',
3✔
34
            'mixed',
3✔
35
            "Characters to remove. Array or string, i.e. {0: 'a', 1: 'b', 2: 'c'} or 'abc' to remove all " .
3✔
36
            'occurrences of a, b and c'
3✔
37
        );
3✔
38
        $this->registerArgument(
3✔
39
            'strings',
3✔
40
            'mixed',
3✔
41
            "Strings to remove. Array or CSV, i.e. {0: 'foo', 1: 'bar'} or 'foo,bar' to remove all occorrences " .
3✔
42
            'of foo and bar. If your strings overlap then place the longest match first'
3✔
43
        );
3✔
44
        $this->registerArgument('whitespace', 'boolean', 'Eliminate ALL whitespace characters', false, false);
3✔
45
        $this->registerArgument(
3✔
46
            'whitespaceBetweenHtmlTags',
3✔
47
            'boolean',
3✔
48
            'Eliminate ALL whitespace characters between HTML tags. Use this together with <f:format.raw>',
3✔
49
            false,
3✔
50
            false
3✔
51
        );
3✔
52
        $this->registerArgument('tabs', 'boolean', 'Eliminate only tab whitespaces', false, false);
3✔
53
        $this->registerArgument('unixBreaks', 'boolean', 'Eliminate only UNIX line breaks', false, false);
3✔
54
        $this->registerArgument('windowsBreaks', 'boolean', 'Eliminates only Windows carriage returns', false, false);
3✔
55
        $this->registerArgument(
3✔
56
            'digits',
3✔
57
            'boolean',
3✔
58
            'Eliminates all number characters (but not the dividers between floats converted to strings)',
3✔
59
            false,
3✔
60
            false
3✔
61
        );
3✔
62
        $this->registerArgument(
3✔
63
            'letters',
3✔
64
            'boolean',
3✔
65
            'Eliminates all letters (non-numbers, non-whitespace, non-syntactical)',
3✔
66
            false,
3✔
67
            false
3✔
68
        );
3✔
69
        $this->registerArgument('nonAscii', 'boolean', 'Eliminates any ASCII char', false, false);
3✔
70
    }
71

72
    /**
73
     * @param array $arguments
74
     * @param \Closure $renderChildrenClosure
75
     * @param RenderingContextInterface $renderingContext
76
     * @return string
77
     */
78
    public static function renderStatic(
79
        array $arguments,
80
        \Closure $renderChildrenClosure,
81
        RenderingContextInterface $renderingContext
82
    ) {
83
        /** @var string $content */
84
        $content = $arguments['content'] ?? $renderChildrenClosure();
15✔
85
        if (isset($arguments['characters'])) {
15✔
86
            $content = static::eliminateCharacters(
3✔
87
                $content,
3✔
88
                $arguments['characters'],
3✔
89
                (bool) $arguments['caseSensitive']
3✔
90
            );
3✔
91
        }
92
        if (isset($arguments['strings'])) {
15✔
93
            $content = static::eliminateStrings($content, $arguments['strings'], (bool) $arguments['caseSensitive']);
×
94
        }
95
        if ($arguments['whitespace']) {
15✔
96
            $content = static::eliminateWhitespace($content);
3✔
97
        }
98
        if ($arguments['whitespaceBetweenHtmlTags']) {
15✔
99
            $content = static::eliminateWhitespaceBetweenHtmlTags($content);
×
100
        }
101
        if ($arguments['tabs']) {
15✔
102
            $content = static::eliminateTabs($content);
×
103
        }
104
        if ($arguments['unixBreaks']) {
15✔
105
            $content = static::eliminateUnixBreaks($content);
×
106
        }
107
        if ($arguments['windowsBreaks']) {
15✔
108
            $content = static::eliminateWindowsCarriageReturns($content);
×
109
        }
110
        if ($arguments['digits']) {
15✔
111
            $content = static::eliminateDigits($content);
3✔
112
        }
113
        if ($arguments['letters']) {
15✔
114
            $content = static::eliminateLetters($content, (bool) $arguments['caseSensitive']);
3✔
115
        }
116
        if ($arguments['nonAscii']) {
15✔
117
            $content = static::eliminateNonAscii($content, (bool) $arguments['caseSensitive']);
3✔
118
        }
119
        return $content;
15✔
120
    }
121

122
    /**
123
     * @param string $content
124
     * @param string|array $characters
125
     * @param boolean $caseSensitive
126
     * @return string
127
     */
128
    protected static function eliminateCharacters($content, $characters, $caseSensitive)
129
    {
130
        if (is_array($characters)) {
3✔
131
            $subjects = $characters;
×
132
        } else {
133
            $subjects = (array) preg_split('//u', $characters, 0, PREG_SPLIT_NO_EMPTY);
3✔
134
        }
135
        foreach ($subjects as $subject) {
3✔
136
            if ($caseSensitive) {
3✔
137
                $content = str_replace($subject, '', $content);
3✔
138
            } else {
139
                $content = str_ireplace($subject, '', $content);
×
140
            }
141
        }
142
        return $content;
3✔
143
    }
144

145
    /**
146
     * @param string $content
147
     * @param string|array $strings
148
     * @param boolean $caseSensitive
149
     * @return string
150
     */
151
    protected static function eliminateStrings($content, $strings, $caseSensitive)
152
    {
153
        if (is_array($strings)) {
×
154
            $subjects = $strings;
×
155
        } else {
156
            $subjects = explode(',', $strings);
×
157
        }
158
        foreach ($subjects as $subject) {
×
159
            if ($caseSensitive) {
×
160
                $content = str_replace($subject, '', $content);
×
161
            } else {
162
                $content = str_ireplace($subject, '', $content);
×
163
            }
164
        }
165
        return $content;
×
166
    }
167

168
    /**
169
     * @param string $content
170
     * @return string
171
     */
172
    protected static function eliminateWhitespace($content)
173
    {
174
        $content = preg_replace('/\s+/', '', $content);
3✔
175
        return (string) $content;
3✔
176
    }
177

178
    /**
179
     * @param string $content
180
     * @return string
181
     */
182
    protected static function eliminateWhitespaceBetweenHtmlTags($content)
183
    {
184
        $content = trim((string) preg_replace('/>\s+</', '><', $content));
×
185
        return $content;
×
186
    }
187

188
    /**
189
     * @param string $content
190
     * @return string
191
     */
192
    protected static function eliminateTabs($content)
193
    {
194
        $content = str_replace("\t", '', $content);
×
195
        return $content;
×
196
    }
197

198
    /**
199
     * @param string $content
200
     * @return string
201
     */
202
    protected static function eliminateUnixBreaks($content)
203
    {
204
        $content = str_replace("\n", '', $content);
×
205
        return $content;
×
206
    }
207

208
    /**
209
     * @param string $content
210
     * @return string
211
     */
212
    protected static function eliminateWindowsCarriageReturns($content)
213
    {
214
        $content = str_replace("\r", '', $content);
×
215
        return $content;
×
216
    }
217

218
    /**
219
     * @param string $content
220
     * @return string
221
     */
222
    protected static function eliminateDigits($content)
223
    {
224
        $content = preg_replace('#[0-9]#', '', $content);
3✔
225
        return (string) $content;
3✔
226
    }
227

228
    /**
229
     * @param string $content
230
     * @param boolean $caseSensitive
231
     * @return string
232
     */
233
    protected static function eliminateLetters($content, $caseSensitive)
234
    {
235
        if ($caseSensitive) {
3✔
236
            $content = preg_replace('#[a-z]#', '', $content);
3✔
237
        } else {
238
            $content = preg_replace('/[a-z]/i', '', $content);
×
239
        }
240
        return (string) $content;
3✔
241
    }
242

243
    /**
244
     * @param string $content
245
     * @param boolean $caseSensitive
246
     * @return string
247
     */
248
    protected static function eliminateNonAscii($content, $caseSensitive)
249
    {
250
        $caseSensitiveIndicator = $caseSensitive ? 'i' : '';
3✔
251
        $content = preg_replace('/[^(\x20-\x7F)]*/' . $caseSensitiveIndicator, '', $content);
3✔
252
        return (string) $content;
3✔
253
    }
254
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc