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

CPS-IT / handlebars-forms / 24389559754

14 Apr 2026 08:42AM UTC coverage: 0.578% (-0.002%) from 0.58%
24389559754

Pull #31

github

web-flow
Merge 17a74cda5 into 26880b9be
Pull Request #31: [TASK] Call `$context->process()` to process "generic" value in cObjects

0 of 13 new or added lines in 3 files covered. (0.0%)

1 existing line in 1 file now uncovered.

7 of 1212 relevant lines covered (0.58%)

0.02 hits per line

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

0.0
/Classes/ContentObject/AbstractHandlebarsFormsContentObject.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars_forms".
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17

18
namespace CPSIT\Typo3HandlebarsForms\ContentObject;
19

20
use CPSIT\Typo3HandlebarsForms\Utility;
21
use Psr\Log;
22
use TYPO3\CMS\Frontend;
23

24
/**
25
 * AbstractHandlebarsFormsContentObject
26
 *
27
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
28
 * @license GPL-2.0-or-later
29
 */
30
abstract class AbstractHandlebarsFormsContentObject extends Frontend\ContentObject\AbstractContentObject implements Log\LoggerAwareInterface
31
{
32
    use Log\LoggerAwareTrait;
33

34
    private Context\ContextStack $contextStack;
35
    private Context\ValueCollector $valueCollector;
36

37
    public function injectContextStack(Context\ContextStack $contextStack): void
×
38
    {
39
        $this->contextStack = $contextStack;
×
40
    }
41

42
    public function injectValueCollector(Context\ValueCollector $valueCollector): void
×
43
    {
44
        $this->valueCollector = $valueCollector;
×
45
    }
46

47
    /**
48
     * @param array<string|int, mixed> $conf
49
     */
50
    final public function render($conf = []): string
×
51
    {
52
        $context = $this->contextStack->current();
×
53

54
        if ($context === null) {
×
55
            $this->logger?->warning(
×
56
                'Using a HBS_* content object in other contexts than "process-form" data processor is not supported.',
×
57
            );
×
58

59
            return '';
×
60
        }
61

62
        // Resolve value and apply stdWrap (either on stringable value or by using value as currentValue in COR)
63
        $resolvedValue = $this->resolve($conf, $context);
×
64
        $value = $this->applyStdWrap($resolvedValue, $conf);
×
65

66
        // Strings can be returned without additional caching using ValueCollector,
67
        // since content objects are already designed to return string values
68
        if (is_string($value)) {
×
69
            return $value;
×
70
        }
71

72
        return $this->valueCollector->save($this, $value);
×
73
    }
74

75
    /**
76
     * @param array<string|int, mixed> $configuration
77
     */
78
    abstract protected function resolve(array $configuration, Context\ValueResolutionContext $context): mixed;
×
79

80
    /**
81
     * @param array<string|int, mixed> $configuration
82
     */
NEW
83
    protected function processGenericValue(
×
84
        string $value,
85
        array $configuration,
86
        Context\ValueResolutionContext $context,
87
    ): mixed {
NEW
88
        $result = $context->process(
×
NEW
89
            [
×
NEW
90
                'tempKey' => $value,
×
NEW
91
                'tempKey.' => $configuration,
×
NEW
92
            ],
×
NEW
93
            $context->renderable,
×
NEW
94
            $context->viewModel,
×
NEW
95
        );
×
96

NEW
97
        if (!is_array($result) || !array_key_exists('tempKey', $result)) {
×
UNCOV
98
            return $value;
×
99
        }
100

NEW
101
        return $result['tempKey'];
×
102
    }
103

104
    /**
105
     * @param array<string|int, mixed> $configuration
106
     */
107
    private function applyStdWrap(mixed $value, array $configuration): mixed
×
108
    {
109
        if ($this->cObj === null || !is_array($configuration['stdWrap.'] ?? null)) {
×
110
            return $value;
×
111
        }
112

113
        $apply = fn(string $string) => $this->cObj->stdWrap($string, $configuration['stdWrap.']) ?? $value;
×
114

115
        // Backup and override current value
116
        $currentValue = $this->cObj->getCurrentVal();
×
117
        $this->cObj->setCurrentVal($this->convertValueToStringableValue($value));
×
118

119
        try {
120
            // Apply stdWrap directly on stringable value
121
            if (Utility\StringUtility::isStringable($value)) {
×
122
                return Utility\StringUtility::processStringable($value, $apply(...));
×
123
            }
124

125
            // Apply stdWrap on empty string, but give consumers the chance to perform actions
126
            // based on the current value (which reflects the non-stringable resolved value)
127
            return $apply('');
×
128
        } finally {
129
            // Restore previous current value
130
            $this->cObj?->setCurrentVal($currentValue);
×
131
        }
132
    }
133

134
    /**
135
     * Build stringable value for usage as current value in COR.
136
     * Passes through stringables and tries to convert scalar-arrays to a string list.
137
     */
138
    private function convertValueToStringableValue(mixed $value): ?string
×
139
    {
140
        if ($value === null) {
×
141
            return null;
×
142
        }
143

144
        if (Utility\StringUtility::isStringable($value)) {
×
145
            return (string)$value;
×
146
        }
147

148
        if (is_array($value)) {
×
149
            return implode(',', array_filter($value, is_scalar(...)));
×
150
        }
151

152
        return null;
×
153
    }
154
}
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