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

FluidTYPO3 / vhs / 13566190336

27 Feb 2025 12:18PM UTC coverage: 72.127% (-0.6%) from 72.746%
13566190336

push

github

NamelessCoder
[TER] 7.1.0

5649 of 7832 relevant lines covered (72.13%)

20.01 hits per line

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

94.44
/Classes/ViewHelpers/Iterator/RandomViewHelper.php
1
<?php
2
namespace FluidTYPO3\Vhs\ViewHelpers\Iterator;
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\Traits\ArrayConsumingViewHelperTrait;
12
use FluidTYPO3\Vhs\Traits\CompileWithRenderStatic;
13
use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
14
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
15
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
16

17
/**
18
 * Returns random element from array.
19
 */
20
class RandomViewHelper extends AbstractViewHelper
21
{
22
    use TemplateVariableViewHelperTrait;
23
    use ArrayConsumingViewHelperTrait;
24
    use CompileWithRenderStatic;
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(
7✔
39
            'subject',
7✔
40
            'mixed',
7✔
41
            'The subject Traversable/Array instance from which to select a random element'
7✔
42
        );
7✔
43
        $this->registerAsArgument();
7✔
44
    }
45

46
    /**
47
     * @return mixed
48
     */
49
    public static function renderStatic(
50
        array $arguments,
51
        \Closure $renderChildrenClosure,
52
        RenderingContextInterface $renderingContext
53
    ) {
54
        /** @var string|null $as */
55
        $as = $arguments['as'];
21✔
56
        $subject = static::arrayFromArrayOrTraversableOrCSVStatic(
21✔
57
            empty($as) ? ($arguments['subject'] ?? $renderChildrenClosure()) : $arguments['subject']
21✔
58
        );
21✔
59
        if (empty($subject)) {
21✔
60
            return null;
×
61
        }
62
        return static::renderChildrenWithVariableOrReturnInputStatic(
21✔
63
            $subject[array_rand($subject)],
21✔
64
            $as,
21✔
65
            $renderingContext,
21✔
66
            $renderChildrenClosure
21✔
67
        );
21✔
68
    }
69
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc