• 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

97.67
/Classes/ViewHelpers/Iterator/ChunkViewHelper.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\Core\ViewHelper\AbstractViewHelper;
12
use FluidTYPO3\Vhs\Traits\ArrayConsumingViewHelperTrait;
13
use FluidTYPO3\Vhs\Traits\TemplateVariableViewHelperTrait;
14
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
15

16
/**
17
 * Creates chunks from an input Array/Traversable with option to allocate items to a fixed number of chunks
18
 */
19
class ChunkViewHelper extends AbstractViewHelper
20
{
21
    use TemplateVariableViewHelperTrait;
22
    use ArrayConsumingViewHelperTrait;
23

24
    /**
25
     * @var boolean
26
     */
27
    protected $escapeChildren = false;
28

29
    /**
30
     * @var boolean
31
     */
32
    protected $escapeOutput = false;
33

34
    public function initializeArguments(): void
35
    {
36
        $this->registerArgument('subject', 'mixed', 'The subject Traversable/Array instance to shift');
3✔
37
        $this->registerArgument('count', 'integer', 'Number of items/chunk or if fixed then number of chunks', true);
3✔
38
        $this->registerAsArgument();
3✔
39
        $this->registerArgument(
3✔
40
            'fixed',
3✔
41
            'boolean',
3✔
42
            'If true, creates $count chunks instead of $count values per chunk',
3✔
43
            false,
3✔
44
            false
3✔
45
        );
3✔
46
        $this->registerArgument(
3✔
47
            'preserveKeys',
3✔
48
            'boolean',
3✔
49
            'If set to true, the original array keys will be preserved',
3✔
50
            false,
3✔
51
            false
3✔
52
        );
3✔
53
    }
54

55
    /**
56
     * @return array|mixed
57
     */
58
    public static function renderStatic(
59
        array $arguments,
60
        \Closure $renderChildrenClosure,
61
        RenderingContextInterface $renderingContext
62
    ) {
63
        /** @var string|null $as */
64
        $as = $arguments['as'];
6✔
65
        /** @var int $count */
66
        $count = $arguments['count'];
6✔
67
        $fixed = (bool) $arguments['fixed'];
6✔
68
        $preserveKeys = (bool) $arguments['preserveKeys'];
6✔
69
        $subject = static::arrayFromArrayOrTraversableOrCSVStatic(
6✔
70
            empty($as) ? ($arguments['subject'] ?? $renderChildrenClosure()) : $arguments['subject'],
6✔
71
            $preserveKeys
6✔
72
        );
6✔
73
        $output = [];
6✔
74
        if (0 >= $count) {
6✔
75
            return $output;
×
76
        }
77
        if ($fixed) {
6✔
78
            $subjectSize = count($subject);
3✔
79
            if (0 < $subjectSize) {
3✔
80
                /** @var int<1, max> $chunkSize */
81
                $chunkSize = (int) ceil($subjectSize / $count);
3✔
82
                $output = array_chunk($subject, $chunkSize, $preserveKeys);
3✔
83
            }
84
            // Fill the resulting array with empty items to get the desired element count
85
            $elementCount = count($output);
3✔
86
            if ($elementCount < $count) {
3✔
87
                $output += array_fill($elementCount, $count - $elementCount, null);
3✔
88
            }
89
        } else {
90
            $output = array_chunk($subject, $count, $preserveKeys);
3✔
91
        }
92

93
        return static::renderChildrenWithVariableOrReturnInputStatic(
6✔
94
            $output,
6✔
95
            $as,
6✔
96
            $renderingContext,
6✔
97
            $renderChildrenClosure
6✔
98
        );
6✔
99
    }
100
}
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