-
Notifications
You must be signed in to change notification settings - Fork 55
/
StaticDatabaseMapper.php
107 lines (91 loc) · 3.21 KB
/
StaticDatabaseMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
declare(strict_types = 1);
/*
* This file is part of the package t3g/blog.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace T3G\AgencyPack\Blog\Routing\Aspect;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Routing\Aspect\StaticMappableAspectInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class StaticDatabaseMapper implements StaticMappableAspectInterface, \Countable
{
protected array $settings;
protected string $field;
protected string $table;
protected string $groupBy;
protected array $where;
protected array $values;
public function __construct(array $settings)
{
$field = $settings['field'] ?? null;
$table = $settings['table'] ?? null;
$where = $settings['where'] ?? [];
$groupBy = $settings['groupBy'] ?? '';
if (!is_string($field)) {
throw new \InvalidArgumentException('field must be string', 1550156808);
}
if (!is_string($table)) {
throw new \InvalidArgumentException('table must be string', 1550156812);
}
if (!is_string($groupBy)) {
throw new \InvalidArgumentException('groupBy must be string', 1550158149);
}
if (!is_array($where)) {
throw new \InvalidArgumentException('where must be an array', 1550157442);
}
$this->settings = $settings;
$this->field = $field;
$this->table = $table;
$this->where = $where;
$this->groupBy = $groupBy;
$this->values = $this->buildValues();
}
public function count(): int
{
return count($this->values);
}
public function generate(string $value): ?string
{
return $this->respondWhenInValues($value);
}
public function resolve(string $value): ?string
{
return $this->respondWhenInValues($value);
}
protected function respondWhenInValues(string $value): ?string
{
if (in_array($value, $this->values, true)) {
return $value;
}
return null;
}
/**
* Builds range based on given settings and ensures each item is string.
* The amount of items is limited to 1000 in order to avoid brute-force
* scenarios and the risk of cache-flooding.
*
* In case that is not enough, creating a custom and more specific mapper
* is encouraged. Using high values that are not distinct exposes the site
* to the risk of cache-flooding.
*
* @return string[]
*/
protected function buildValues(): array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($this->table);
$queryBuilder
->select($this->field)
->from($this->table);
if ($this->groupBy !== '') {
$queryBuilder->groupBy($this->groupBy);
}
foreach ($this->where as $key => $value) {
$queryBuilder->andWhere($queryBuilder->expr()->eq($key, $queryBuilder->createNamedParameter($value)));
}
return array_map('strval', array_column($queryBuilder->executeQuery()->fetchAllAssociative(), $this->field));
}
}