ICalendarDescriptionViewHelper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeArguments() 0 5 1
A render() 0 29 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\ViewHelpers\Format;
13
14
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
15
16
/**
17
 * ICalendar Description viewHelper. Note, this ViewHelper does not escape output and should only be used
18
 * to process the iCal description field and any other foldable ICal text fields.
19
 */
20
class ICalendarDescriptionViewHelper extends AbstractViewHelper
21
{
22
    /**
23
     * @var bool
24
     */
25
    protected $escapeOutput = false;
26
27
    public function initializeArguments(): void
28
    {
29
        parent::initializeArguments();
30
        $this->registerArgument('description', 'string', 'The description', false);
31
        $this->registerArgument('substractChars', 'integer', 'Amount of chars to substract from 75 in first line', false, 12);
32
    }
33
34
    /**
35
     * Formats the given description according to RFC 2445
36
     */
37
    public function render(): string
38
    {
39
        $description = $this->arguments['description'] ?? null;
40
        $substractChars = $this->arguments['substractChars'] ?? 0;
41
        $firstLineMaxChars = 75 - $substractChars;
42
        if ($description === null) {
43
            $description = $this->renderChildren();
44
        }
45
        $tmpDescription = strip_tags($description);
46
        $tmpDescription = str_replace('&nbsp;', ' ', $tmpDescription);
47
        $tmpDescription = html_entity_decode($tmpDescription);
48
        // Replace carriage return
49
        $tmpDescription = str_replace(chr(13), '\n\n', $tmpDescription);
50
        // Strip new lines
51
        $tmpDescription = str_replace(chr(10), '', $tmpDescription);
52
        // Glue everything together, so every line is max 75 chars respecting max. length of first line
53
        if (mb_strlen($tmpDescription) > $firstLineMaxChars) {
54
            $newDescription = mb_substr($tmpDescription, 0, $firstLineMaxChars) . chr(10);
55
            $tmpDescription = mb_substr($tmpDescription, $firstLineMaxChars);
56
            $arrPieces = mb_str_split($tmpDescription, 75);
57
            foreach ($arrPieces as &$value) {
58
                $value = ' ' . $value;
59
            }
60
            $newDescription .= implode(chr(10), $arrPieces);
61
        } else {
62
            $newDescription = $tmpDescription;
63
        }
64
65
        return $newDescription;
66
    }
67
}
68