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

CPS-IT / handlebars / 33596869355

02 Sep 2026 05:58AM UTC coverage: 90.098% (-0.2%) from 90.324%
33596869355

push

github

web-flow
Merge pull request #628 from CPS-IT/feature/keyword

[FEATURE] Simplify keyword-based data source collection resolution

12 of 17 new or added lines in 4 files covered. (70.59%)

1 existing line in 1 file now uncovered.

1565 of 1737 relevant lines covered (90.1%)

6.94 hits per line

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

90.38
/Classes/DataProcessing/DataSource/DataSourceCollection.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "handlebars".
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\Typo3Handlebars\DataProcessing\DataSource;
19

20
use CPSIT\Typo3Handlebars\Exception;
21

22
/**
23
 * DataSourceCollection
24
 *
25
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
26
 * @license GPL-2.0-or-later
27
 */
28
final class DataSourceCollection
29
{
30
    /**
31
     * @var array<value-of<DataSource>, array<string|int, mixed>>
32
     */
33
    private array $dataSources = [];
34

35
    /**
36
     * @return array<string|int, mixed>
37
     */
38
    public function get(DataSource $dataSource): array
14✔
39
    {
40
        return $this->dataSources[$dataSource->value] ?? [];
14✔
41
    }
42

43
    /**
44
     * @param array<string|int, mixed> $configuration
45
     */
46
    public function set(DataSource $dataSource, array $configuration): self
12✔
47
    {
48
        $this->dataSources[$dataSource->value] = $configuration;
12✔
49

50
        return $this;
12✔
51
    }
52

53
    public function has(DataSource $dataSource): bool
2✔
54
    {
55
        return array_key_exists($dataSource->value, $this->dataSources);
2✔
56
    }
57

58
    public function remove(DataSource $dataSource): self
1✔
59
    {
60
        unset($this->dataSources[$dataSource->value]);
1✔
61

62
        return $this;
1✔
63
    }
64

65
    /**
66
     * @template T
67
     * @param non-empty-string $key
68
     * @param DataSource|list<DataSource> $dataSources
69
     * @param T $default
70
     * @return mixed|T
71
     */
72
    public function resolve(string $key, DataSource|array $dataSources = [], mixed $default = null): mixed
11✔
73
    {
74
        // Get from all configured data sources (in the given order) if no data sources are configured explicitly
75
        // The order can be seen as priority for each single data source
76
        if ($dataSources === []) {
11✔
77
            $dataSources = $this->getConfiguredDataSourcesSortedByPriority();
2✔
78
        } elseif ($dataSources instanceof DataSource) {
10✔
79
            $dataSources = [$dataSources];
9✔
80
        }
81

82
        foreach ($dataSources as $dataSource) {
11✔
83
            $found = false;
11✔
84
            $result = $this->resolveForDataSource($key, $dataSource, $found);
11✔
85

86
            if ($found) {
11✔
87
                return $result;
9✔
88
            }
89
        }
90

91
        return $default;
3✔
92
    }
93

94
    /**
95
     * @template T
96
     * @param non-empty-string $keyword
97
     * @param DataSource|list<DataSource> $dataSources
98
     * @param T $default
99
     * @return array{mixed|T, non-empty-string}
100
     * @throws Exception\KeywordCannotBeResolved
101
     */
102
    public function resolveKeyword(string $keyword, DataSource|array $dataSources = [], mixed $default = null): array
4✔
103
    {
104
        $variableName = $this->resolve($keyword, DataSource::ProcessorConfiguration);
4✔
105

106
        if (!is_string($variableName) || $variableName === '') {
4✔
107
            throw new Exception\KeywordCannotBeResolved($keyword);
2✔
108
        }
109

110
        return [
2✔
111
            $this->resolve($variableName, $dataSources, $default),
2✔
112
            $variableName,
2✔
113
        ];
2✔
114
    }
115

UNCOV
116
    public function resolveCurrentUid(): int|string
×
117
    {
118
        $uid = $this->resolve('uid', DataSource::ContentObjectRenderer);
×
119

120
        if (!is_numeric($uid)) {
×
121
            return '*unknown*';
×
122
        }
123

124
        return (int)$uid;
×
125
    }
126

127
    /**
128
     * @param DataSource|list<DataSource> $dataSources
129
     */
130
    public function with(string $key, mixed $value, DataSource|array $dataSources = []): self
3✔
131
    {
132
        // Apply to all configured data sources if no data sources are configured explicitly
133
        if ($dataSources === []) {
3✔
134
            foreach ($this->dataSources as $dataSource => $configuration) {
1✔
135
                $this->dataSources[$dataSource][$key] = $value;
1✔
136
            }
137

138
            return $this;
1✔
139
        }
140

141
        if ($dataSources instanceof DataSource) {
2✔
142
            $dataSources = [$dataSources];
1✔
143
        }
144

145
        foreach ($dataSources as $dataSource) {
2✔
146
            $this->dataSources[$dataSource->value] ??= [];
2✔
147
            $this->dataSources[$dataSource->value][$key] = $value;
2✔
148
        }
149

150
        return $this;
2✔
151
    }
152

153
    private function resolveForDataSource(string $key, DataSource $dataSource, bool &$found = false): mixed
11✔
154
    {
155
        $configuration = $this->get($dataSource);
11✔
156
        $found = array_key_exists($key, $configuration);
11✔
157

158
        return $found ? $configuration[$key] : null;
11✔
159
    }
160

161
    /**
162
     * @return list<DataSource>
163
     */
164
    private function getConfiguredDataSourcesSortedByPriority(): array
2✔
165
    {
166
        $dataSources = array_map(DataSource::from(...), array_keys($this->dataSources));
2✔
167

168
        return DataSource::sortByPriority($dataSources);
2✔
169
    }
170
}
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