[TASK] Move ViewHelper arguments to initializeArguments() in ext:install
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / ViewHelpers / Format / CropViewHelper.php
1 <?php
2 namespace TYPO3\CMS\Install\ViewHelpers\Format;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper;
18 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
19
20 /**
21 * Simplified crop view helper that does not need a frontend environment
22 *
23 * = Examples =
24 *
25 * <code title="Defaults">
26 * <f:format.crop maxCharacters="10">This is some very long text</f:format.crop>
27 * </code>
28 * <output>
29 * This is...
30 * </output>
31 *
32 * <code title="Inline notation">
33 * {someLongText -> f:format.crop(maxCharacters: 10)}
34 * </code>
35 * <output>
36 * someLongText cropped after 10 characters...
37 * (depending on the value of {someLongText})
38 * </output>
39 *
40 * @internal
41 */
42 class CropViewHelper extends AbstractViewHelper
43 {
44 /**
45 * Output is escaped already. We must not escape children, to avoid double encoding.
46 *
47 * @var bool
48 */
49 protected $escapeChildren = false;
50
51 /**
52 * Initialize arguments
53 */
54 public function initializeArguments()
55 {
56 parent::initializeArguments();
57 $this->registerArgument('maxCharacters', 'int', '', true);
58 }
59
60 /**
61 * Render the cropped text
62 *
63 * @throws \TYPO3\CMS\Install\ViewHelpers\Exception
64 * @return string cropped text
65 */
66 public function render()
67 {
68 return static::renderStatic(
69 $this->arguments,
70 $this->buildRenderChildrenClosure(),
71 $this->renderingContext
72 );
73 }
74
75 /**
76 * @param array $arguments
77 * @param \Closure $renderChildrenClosure
78 * @param RenderingContextInterface $renderingContext
79 * @throws \TYPO3\CMS\Install\ViewHelpers\Exception
80 * @return string
81 */
82 public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
83 {
84 $maxCharacters = $arguments['maxCharacters'];
85 if (empty($maxCharacters) || $maxCharacters < 1) {
86 throw new \TYPO3\CMS\Install\ViewHelpers\Exception(
87 'maxCharacters must be a positive integer',
88 1371410113
89 );
90 }
91 $stringToTruncate = $renderChildrenClosure();
92 return substr($stringToTruncate, 0, $maxCharacters);
93 }
94 }