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

CPS-IT / handlebars / 28575318793

02 Jul 2026 08:09AM UTC coverage: 86.951% (-2.7%) from 89.609%
28575318793

Pull #605

github

web-flow
Merge 23fc6cefa into fe28bf1ca
Pull Request #605: [FEATURE] Implement `object-access` data processor

3 of 53 new or added lines in 3 files covered. (5.66%)

1466 of 1686 relevant lines covered (86.95%)

6.86 hits per line

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

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

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

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

48
        return $this;
9✔
49
    }
50

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

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

60
        return $this;
1✔
61
    }
62

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

80
        foreach ($dataSources as $dataSource) {
7✔
81
            $found = false;
7✔
82
            $result = $this->resolveForDataSource($key, $dataSource, $found);
7✔
83

84
            if ($found) {
7✔
85
                return $result;
6✔
86
            }
87
        }
88

89
        return $default;
1✔
90
    }
91

NEW
92
    public function resolveCurrentUid(): int|string
×
93
    {
NEW
94
        $uid = $this->resolve('uid', DataSource::ContentObjectRenderer);
×
95

NEW
96
        if (!is_numeric($uid)) {
×
NEW
97
            return '*unknown*';
×
98
        }
99

NEW
100
        return (int)$uid;
×
101
    }
102

103
    /**
104
     * @param DataSource|list<DataSource> $dataSources
105
     */
106
    public function with(string $key, mixed $value, DataSource|array $dataSources = []): self
3✔
107
    {
108
        // Apply to all configured data sources if no data sources are configured explicitly
109
        if ($dataSources === []) {
3✔
110
            foreach ($this->dataSources as $dataSource => $configuration) {
1✔
111
                $this->dataSources[$dataSource][$key] = $value;
1✔
112
            }
113

114
            return $this;
1✔
115
        }
116

117
        if ($dataSources instanceof DataSource) {
2✔
118
            $dataSources = [$dataSources];
1✔
119
        }
120

121
        foreach ($dataSources as $dataSource) {
2✔
122
            $this->dataSources[$dataSource->value] ??= [];
2✔
123
            $this->dataSources[$dataSource->value][$key] = $value;
2✔
124
        }
125

126
        return $this;
2✔
127
    }
128

129
    private function resolveForDataSource(string $key, DataSource $dataSource, bool &$found = false): mixed
7✔
130
    {
131
        $configuration = $this->get($dataSource);
7✔
132
        $found = array_key_exists($key, $configuration);
7✔
133

134
        return $found ? $configuration[$key] : null;
7✔
135
    }
136

137
    /**
138
     * @return list<DataSource>
139
     */
140
    private function getConfiguredDataSourcesSortedByPriority(): array
1✔
141
    {
142
        $dataSources = array_map(DataSource::from(...), array_keys($this->dataSources));
1✔
143

144
        return DataSource::sortByPriority($dataSources);
1✔
145
    }
146
}
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