• 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

29.82
/Classes/ViewHelpers/Variable/ConvertViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Variable;
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 TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
14
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
15

16
/**
17
 * ### Convert ViewHelper
18
 *
19
 * Converts $value to $type which can be one of 'string', 'integer',
20
 * 'float', 'boolean', 'array' or 'ObjectStorage'. If $value is NULL
21
 * sensible defaults are assigned or $default which obviously has to
22
 * be of $type as well.
23
 */
24
class ConvertViewHelper extends AbstractViewHelper
25
{
26
    /**
27
     * @var boolean
28
     */
29
    protected $escapeChildren = false;
30

31
    /**
32
     * @var boolean
33
     */
34
    protected $escapeOutput = false;
35

36
    public function initializeArguments(): void
37
    {
38
        $this->registerArgument('value', 'mixed', 'Value to convert into a different type');
3✔
39
        $this->registerArgument(
3✔
40
            'type',
3✔
41
            'string',
3✔
42
            'Data type to convert the value into. Can be one of "string", "integer", "float", "boolean", "array" ' .
3✔
43
            'or "ObjectStorage".',
3✔
44
            true
3✔
45
        );
3✔
46
        $this->registerArgument(
3✔
47
            'default',
3✔
48
            'mixed',
3✔
49
            'Optional default value to assign to the converted variable in case it is NULL.'
3✔
50
        );
3✔
51
    }
52

53
    /**
54
     * @return mixed
55
     */
56
    public static function renderStatic(
57
        array $arguments,
58
        \Closure $renderChildrenClosure,
59
        RenderingContextInterface $renderingContext
60
    ) {
61
        /** @var mixed $value */
62
        $value = $arguments['value'] ?? $renderChildrenClosure();
3✔
63
        $type = is_scalar($arguments['type']) ? (string) $arguments['type'] : null;
3✔
64
        if (gettype($value) === $type) {
3✔
65
            return $value;
3✔
66
        }
67
        if (null !== $value) {
×
68
            if ('ObjectStorage' === $type && 'array' === gettype($value)) {
×
69
                /** @var ObjectStorage $storage */
70
                $storage = GeneralUtility::makeInstance(ObjectStorage::class);
×
71
                foreach ($value as $item) {
×
72
                    $storage->attach($item);
×
73
                }
74
                $value = $storage;
×
75
            } elseif ('array' === $type && $value instanceof \Traversable) {
×
76
                $value = iterator_to_array($value, false);
×
77
            } elseif ('array' === $type) {
×
78
                $value = [$value];
×
79
            } elseif (is_string($type)) {
×
80
                settype($value, $type);
×
81
            }
82
        } else {
83
            if (isset($arguments['default'])) {
×
84
                $default = $arguments['default'];
×
85
                if (gettype($default) !== $type) {
×
86
                    throw new \RuntimeException(
×
87
                        'Supplied argument "default" is not of the type "' . $type .'"',
×
88
                        1364542576
×
89
                    );
×
90
                }
91
                $value = $default;
×
92
            } else {
93
                switch ($type) {
94
                    case 'string':
×
95
                        $value = '';
×
96
                        break;
×
97
                    case 'integer':
×
98
                        $value = 0;
×
99
                        break;
×
100
                    case 'boolean':
×
101
                        $value = false;
×
102
                        break;
×
103
                    case 'float':
×
104
                        $value = 0.0;
×
105
                        break;
×
106
                    case 'array':
×
107
                        $value = [];
×
108
                        break;
×
109
                    case 'ObjectStorage':
×
110
                        $value = GeneralUtility::makeInstance(ObjectStorage::class);
×
111
                        break;
×
112
                    default:
113
                        throw new \RuntimeException('Provided argument "type" is not valid', 1364542884);
×
114
                }
115
            }
116
        }
117
        return $value;
×
118
    }
119
}
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