1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\Database\Doctrine\Form\ChoiceList; |
19
|
|
|
|
20
|
|
|
use Doctrine\DBAL\Connection; |
|
|
|
|
21
|
|
|
use Doctrine\DBAL\Types\ConversionException; |
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Types\Type; |
|
|
|
|
23
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
|
|
|
24
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Loads entities using a {@link QueryBuilder} instance. |
28
|
|
|
* |
29
|
|
|
* @author Benjamin Eberlei <[email protected]> |
30
|
|
|
* @author Bernhard Schussek <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class ORMQueryBuilderLoader implements EntityLoaderInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Contains the query builder that builds the query for fetching the |
36
|
|
|
* entities. |
37
|
|
|
* |
38
|
|
|
* This property should only be accessed through queryBuilder. |
39
|
|
|
*/ |
40
|
|
|
private $queryBuilder; |
41
|
|
|
|
42
|
|
|
public function __construct(QueryBuilder $queryBuilder) |
43
|
|
|
{ |
44
|
|
|
$this->queryBuilder = $queryBuilder; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function getEntities(): array |
51
|
|
|
{ |
52
|
|
|
return $this->queryBuilder->getQuery()->execute(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function getEntitiesByIds(string $identifier, array $values): array |
59
|
|
|
{ |
60
|
|
|
if (null !== $this->queryBuilder->getMaxResults() || 0 < (int) $this->queryBuilder->getFirstResult()) { |
61
|
|
|
// an offset or a limit would apply on results including the where clause with submitted id values |
62
|
|
|
// that could make invalid choices valid |
63
|
|
|
$choices = []; |
64
|
|
|
$metadata = $this->queryBuilder->getEntityManager()->getClassMetadata(\current($this->queryBuilder->getRootEntities())); |
65
|
|
|
|
66
|
|
|
foreach ($this->getEntities() as $entity) { |
67
|
|
|
if (\in_array((string) \current($metadata->getIdentifierValues($entity)), $values, true)) { |
68
|
|
|
$choices[] = $entity; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $choices; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$qb = clone $this->queryBuilder; |
76
|
|
|
$alias = \current($qb->getRootAliases()); |
77
|
|
|
$parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_' . $identifier; |
78
|
|
|
$parameter = \str_replace('.', '_', $parameter); |
79
|
|
|
$where = $qb->expr()->in($alias . '.' . $identifier, ':' . $parameter); |
80
|
|
|
|
81
|
|
|
// Guess type |
82
|
|
|
$entity = \current($qb->getRootEntities()); |
83
|
|
|
$metadata = $qb->getEntityManager()->getClassMetadata($entity); |
84
|
|
|
|
85
|
|
|
if (\in_array($type = $metadata->getTypeOfField($identifier), ['integer', 'bigint', 'smallint'])) { |
86
|
|
|
$parameterType = Connection::PARAM_INT_ARRAY; |
87
|
|
|
|
88
|
|
|
// Filter out non-integer values (e.g. ""). If we don't, some |
89
|
|
|
// databases such as PostgreSQL fail. |
90
|
|
|
$values = \array_values(\array_filter($values, function ($v) { |
91
|
|
|
return (string) $v === (string) (int) $v || \ctype_digit($v); |
92
|
|
|
})); |
93
|
|
|
} elseif (\in_array($type, ['ulid', 'uuid', 'guid'])) { |
94
|
|
|
$parameterType = Connection::PARAM_STR_ARRAY; |
95
|
|
|
|
96
|
|
|
// Like above, but we just filter out empty strings. |
97
|
|
|
$values = \array_values(\array_filter($values, function ($v) { |
98
|
|
|
return '' !== (string) $v; |
99
|
|
|
})); |
100
|
|
|
|
101
|
|
|
// Convert values into right type |
102
|
|
|
if (Type::hasType($type)) { |
103
|
|
|
$doctrineType = Type::getType($type); |
104
|
|
|
$platform = $qb->getEntityManager()->getConnection()->getDatabasePlatform(); |
105
|
|
|
|
106
|
|
|
foreach ($values as &$value) { |
107
|
|
|
try { |
108
|
|
|
$value = $doctrineType->convertToDatabaseValue($value, $platform); |
109
|
|
|
} catch (ConversionException $e) { |
110
|
|
|
throw new TransformationFailedException(\sprintf('Failed to transform "%s" into "%s".', $value, $type), 0, $e); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
unset($value); |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
$parameterType = Connection::PARAM_STR_ARRAY; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (!$values) { |
|
|
|
|
120
|
|
|
return []; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $qb->andWhere($where) |
124
|
|
|
->getQuery() |
125
|
|
|
->setParameter($parameter, $values, $parameterType) |
126
|
|
|
->getResult(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths