Skip to content

Commit f02f900

Browse files
authored
Merge pull request #13 from maltehuebner/fix-missing-paramconverter
Big bunch of changes for missing param converter.
2 parents 4f460bb + 5bb7ef9 commit f02f900

6 files changed

Lines changed: 98 additions & 22 deletions

File tree

src/Attribute/QueryAttribute/RequiredQueryParameter.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,30 @@
77
#[\Attribute]
88
class RequiredQueryParameter extends AbstractAttribute implements QueryAttributeInterface
99
{
10-
public function __construct(private readonly string $parameterName)
10+
public function __construct(
11+
private readonly string $parameterName,
12+
private readonly ?string $repository = null,
13+
private readonly ?string $repositoryMethod = null,
14+
private readonly ?string $accessor = null,
15+
) {}
16+
17+
public function getParameterName(): string
18+
{
19+
return $this->parameterName;
20+
}
21+
22+
public function getRepository(): ?string
1123
{
24+
return $this->repository;
25+
}
1226

27+
public function getRepositoryMethod(): ?string
28+
{
29+
return $this->repositoryMethod;
1330
}
1431

15-
public function getParameterName(): string
32+
public function getAccessor(): ?string
1633
{
17-
return $this->parameterName;
34+
return $this->accessor;
1835
}
1936
}

src/Factory/QueryFactory/QueryFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use MalteHuebner\DataQueryBundle\Query\QueryInterface;
1515
use MalteHuebner\DataQueryBundle\RequestParameterList\RequestParameterList;
1616
use App\Criticalmass\Util\ClassUtil;
17-
use Doctrine\Persistence\ManagerRegistry;
1817
use Symfony\Component\Validator\ConstraintViolationListInterface;
1918
use Symfony\Component\Validator\Validator\ValidatorInterface;
2019

src/Factory/ValueAssigner/ValueAssigner.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
use MalteHuebner\DataQueryBundle\Parameter\ParameterInterface;
99
use MalteHuebner\DataQueryBundle\Query\QueryInterface;
1010
use MalteHuebner\DataQueryBundle\RequestParameterList\RequestParameterList;
11-
use App\Criticalmass\Util\ClassUtil;
12-
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
13-
use Symfony\Component\HttpFoundation\Request;
14-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1511

1612
class ValueAssigner implements ValueAssignerInterface
1713
{
18-
public function __construct(private readonly ManagerRegistry $managerRegistry)
14+
public function __construct(
15+
private readonly ManagerRegistry $managerRegistry
16+
)
1917
{
2018

2119
}
@@ -94,26 +92,42 @@ protected function assignEntityValueFromRepository(RequestParameterList $request
9492
{
9593
$parameterName = $queryField->getParameterName();
9694
$entityClass = $queryField->getType();
97-
$methodName = $queryField->getMethodName();
9895

9996
if (!$requestParameterList->has($parameterName)) {
10097
return $query;
10198
}
10299

103-
$id = $requestParameterList->get($parameterName);
104-
$entityManager = $this->managerRegistry->getManagerForClass($entityClass);
100+
$queryParameterValue = $requestParameterList->get($parameterName);
101+
102+
if ($queryField->getRepository()) {
103+
$repository = $this->managerRegistry->getRepository($queryField->getRepository());
104+
} else {
105+
$repository = $this->managerRegistry->getRepository($entityClass);
106+
}
107+
108+
if (!$repository) {
109+
throw new \RuntimeException(sprintf('No repository found for class %s', $entityClass));
110+
}
105111

106-
if (!$entityManager) {
107-
throw new \RuntimeException(sprintf('No entity manager found for class %s', $entityClass));
112+
if ($queryField->getRepositoryMethod()) {
113+
$methodName = $queryField->getRepositoryMethod();
114+
} else {
115+
$methodName = 'find';
108116
}
109117

110-
$entity = $entityManager->getRepository($entityClass)->find($id);
118+
$entity = $repository->$methodName($queryParameterValue);
119+
120+
if ($queryField->getAccessor()) {
121+
$accessMethodName = $queryField->getAccessor();
122+
$entity = $entity->$accessMethodName();
123+
}
111124

112125
if (null === $entity) {
113126
return $query;
114127
}
115128

116-
$query->$methodName($entity);
129+
$setMethodName = $queryField->getMethodName();
130+
$query->$setMethodName($entity);
117131

118132
return $query;
119133
}
@@ -126,5 +140,4 @@ protected function convertToInt(string $stringValue, string $parameterName): int
126140

127141
return (int)$stringValue;
128142
}
129-
130143
}

src/Factory/ValueAssigner/ValueAssignerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ interface ValueAssignerInterface
1313
public function assignQueryPropertyValueFromRequest(RequestParameterList $requestParameterList, QueryInterface $query, QueryField $queryField): QueryInterface;
1414

1515
public function assignParameterPropertyValueFromRequest(RequestParameterList $requestParameterList, ParameterInterface $parameter, ParameterField $parameterField): ParameterInterface;
16-
}
16+
}

src/FieldList/QueryFieldList/QueryField.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,55 @@
77
class QueryField extends AbstractField
88
{
99
protected ?string $parameterName = null;
10+
protected ?string $repository = null;
11+
protected ?string $repositoryMethod = null;
12+
protected ?string $accessor = null;
1013

1114
public function getParameterName(): ?string
1215
{
1316
return $this->parameterName;
1417
}
1518

16-
public function setParameterName(string $parameterName): QueryField
19+
public function setParameterName(string $parameterName): self
1720
{
1821
$this->parameterName = $parameterName;
1922

2023
return $this;
2124
}
25+
26+
public function getRepository(): ?string
27+
{
28+
return $this->repository;
29+
}
30+
31+
public function setRepository(?string $repository): self
32+
{
33+
$this->repository = $repository;
34+
35+
return $this;
36+
}
37+
38+
public function getRepositoryMethod(): ?string
39+
{
40+
return $this->repositoryMethod;
41+
}
42+
43+
public function setRepositoryMethod(?string $repositoryMethod): self
44+
{
45+
$this->repositoryMethod = $repositoryMethod;
46+
47+
return $this;
48+
}
49+
50+
public function getAccessor(): ?string
51+
{
52+
return $this->accessor;
53+
}
54+
55+
public function setAccessor(?string $accessor): self
56+
{
57+
$this->accessor = $accessor;
58+
59+
return $this;
60+
}
2261
}

src/FieldList/QueryFieldList/QueryFieldListFactory.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ protected function addQueryPropertiesToList(): void
3535
$queryField = new QueryField();
3636
$queryField
3737
->setPropertyName($reflectionProperty->getName())
38-
->setParameterName($instance->getParameterName());
38+
->setParameterName($instance->getParameterName())
39+
->setRepository($instance->getRepository())
40+
->setRepositoryMethod($instance->getRepositoryMethod())
41+
->setAccessor($instance->getAccessor())
42+
;
3943

40-
// Optional: Typ holen (ab PHP 7.4+ möglich)
4144
$type = $reflectionProperty->getType();
45+
4246
if ($type) {
4347
$queryField->setType($type->getName());
4448
}
@@ -71,7 +75,11 @@ protected function addQueryMethodsToList(): void
7175
$queryField
7276
->setMethodName($reflectionMethod->getName())
7377
->setParameterName($instance->getParameterName())
74-
->setType($reflectionType->getName());
78+
->setType($reflectionType->getName())
79+
->setRepository($instance->getRepository())
80+
->setRepositoryMethod($instance->getRepositoryMethod())
81+
->setAccessor($instance->getAccessor())
82+
;
7583

7684
$this->queryFieldList->addField($reflectionMethod->getName(), $queryField);
7785
}

0 commit comments

Comments
 (0)