forked from FluidTYPO3/vhs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBytesViewHelper.php
More file actions
75 lines (68 loc) · 1.78 KB
/
BytesViewHelper.php
File metadata and controls
75 lines (68 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Count;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use FluidTYPO3\Vhs\Traits\CompileWithContentArgumentAndRenderStatic;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use FluidTYPO3\Vhs\Core\ViewHelper\AbstractViewHelper;
/**
* Counts bytes (multibyte-safe) in a string.
*
* #### Usage examples
*
* ```
* <v:count.bytes>{myString}</v:count.bytes> (output for example `42`
* ```
*
* ```
* {myString -> v:count.bytes()} when used inline
* ```
*
* ```
* <v:count.bytes string="{myString}" />
* ```
*
* ```
* {v:count.bytes(string: myString)}
* ```
*/
class BytesViewHelper extends AbstractViewHelper
{
use CompileWithContentArgumentAndRenderStatic;
/**
* @var boolean
*/
protected $escapeChildren = false;
/**
* @var boolean
*/
protected $escapeOutput = false;
public function initializeArguments(): void
{
parent::initializeArguments();
$this->registerArgument('string', 'string', 'String to count, if not provided as tag content');
$this->registerArgument(
'encoding',
'string',
'Character set encoding of string, e.g. UTF-8 or ISO-8859-1',
false,
'UTF-8'
);
}
/**
* @return integer
*/
public static function renderStatic(
array $arguments,
\Closure $renderChildrenClosure,
RenderingContextInterface $renderingContext
) {
/** @var string $encoding */
$encoding = $arguments['encoding'];
return (int) mb_strlen($renderChildrenClosure(), $encoding);
}
}