Skip to content

Commit 6c467b4

Browse files
authored
Merge pull request #11 from maltehuebner/improve-value-assigner
Improve value assigner
2 parents 39c9184 + 3315937 commit 6c467b4

4 files changed

Lines changed: 54 additions & 69 deletions

File tree

src/Factory/ParamConverterFactory/ParamConverterFactory.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Factory/ParamConverterFactory/ParamConverterFactoryInterface.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Factory/ValueAssigner/ValueAssigner.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MalteHuebner\DataQueryBundle\Factory\ValueAssigner;
44

5-
use MalteHuebner\DataQueryBundle\Factory\ParamConverterFactory\ParamConverterFactoryInterface;
5+
use Doctrine\Persistence\ManagerRegistry;
66
use MalteHuebner\DataQueryBundle\FieldList\ParameterFieldList\ParameterField;
77
use MalteHuebner\DataQueryBundle\FieldList\QueryFieldList\QueryField;
88
use MalteHuebner\DataQueryBundle\Parameter\ParameterInterface;
@@ -15,7 +15,7 @@
1515

1616
class ValueAssigner implements ValueAssignerInterface
1717
{
18-
public function __construct(private readonly ParamConverterFactoryInterface $paramConverterFactory)
18+
public function __construct(private readonly ManagerRegistry $managerRegistry)
1919
{
2020

2121
}
@@ -32,25 +32,25 @@ public function assignQueryPropertyValueFromRequest(RequestParameterList $reques
3232
$type = $queryField->getType();
3333

3434
switch ($type) {
35-
case 'float':
35+
case ValueType::FLOAT:
3636
$query->$methodName((float)$value);
3737
break;
3838

39-
case 'int':
39+
case ValueType::INT:
4040
$value = $this->convertToInt($value, $queryField->getParameterName());
4141
$query->$methodName($value);
4242
break;
4343

44-
case 'string':
44+
case ValueType::STRING:
4545
$query->$methodName((string)$value);
4646
break;
4747

48-
case 'mixed':
48+
case ValueType::MIXED:
4949
$query->$methodName($value);
5050
break;
5151

5252
default:
53-
$query = $this->assignEntityValueFromParamConverter($requestParameterList, $query, $queryField);
53+
$query = $this->assignEntityValueFromRepository($requestParameterList, $query, $queryField);
5454
break;
5555
}
5656

@@ -69,56 +69,62 @@ public function assignParameterPropertyValueFromRequest(RequestParameterList $re
6969
$type = $parameterField->getType();
7070

7171
switch ($type) {
72-
case 'float':
72+
case ValueType::FLOAT:
7373
$parameter->$methodName((float)$value);
7474
break;
7575

76-
case 'int':
76+
case ValueType::INT:
7777
$value = $this->convertToInt($value, $parameterField->getParameterName());
7878
$parameter->$methodName($value);
7979
break;
8080

81-
case 'string':
81+
case ValueType::STRING:
8282
$parameter->$methodName((string)$value);
8383
break;
8484

85-
case 'mixed':
85+
case ValueType::MIXED:
8686
$parameter->$methodName($value);
8787
break;
8888
}
8989

9090
return $parameter;
9191
}
9292

93-
protected function assignEntityValueFromParamConverter(RequestParameterList $requestParameterList, QueryInterface $query, QueryField $queryField): QueryInterface
93+
protected function assignEntityValueFromRepository(RequestParameterList $requestParameterList, QueryInterface $query, QueryField $queryField): QueryInterface
9494
{
95-
if ($converter = $this->paramConverterFactory->createParamConverter($queryField->getType())) {
96-
$methodName = $queryField->getMethodName();
97-
$newParameterName = ClassUtil::getLowercaseShortnameFromFqcn($queryField->getType());
95+
$parameterName = $queryField->getParameterName();
96+
$entityClass = $queryField->getType();
97+
$methodName = $queryField->getMethodName();
9898

99-
$paramConverterConfiguration = new ParamConverter(['name' => $newParameterName]);
99+
if (!$requestParameterList->has($parameterName)) {
100+
return $query;
101+
}
100102

101-
$request = new Request($requestParameterList->getList());
103+
$id = $requestParameterList->get($parameterName);
104+
$entityManager = $this->managerRegistry->getManagerForClass($entityClass);
105+
106+
if (!$entityManager) {
107+
throw new \RuntimeException(sprintf('No entity manager found for class %s', $entityClass));
108+
}
102109

103-
try {
104-
$converter->apply($request, $paramConverterConfiguration);
105-
} catch (NotFoundHttpException) {
106-
return $query;
107-
}
110+
$entity = $entityManager->getRepository($entityClass)->find($id);
108111

109-
$query->$methodName($request->get($newParameterName));
112+
if (null === $entity) {
113+
return $query;
110114
}
111115

116+
$query->$methodName($entity);
117+
112118
return $query;
113119
}
114120

115-
protected function convertToInt(string $stringValue, string $parameterValue): int
121+
protected function convertToInt(string $stringValue, string $parameterName): int
116122
{
117-
// ^-?(\d+|\d{1,3}(?:\,\d{3})+)?(\.\d+)?$
118-
// if (!ctype_digit($stringValue)) {
119-
// throw new ParameterConverterException('int', $stringValue, $parameterValue);
120-
// }
123+
if (!preg_match('/^-?\d+$/', $stringValue)) {
124+
throw new \InvalidArgumentException(sprintf('Parameter "%s" is not a valid integer: "%s"', $parameterName, $stringValue));
125+
}
121126

122127
return (int)$stringValue;
123128
}
129+
124130
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace MalteHuebner\DataQueryBundle\Factory\ValueAssigner;
4+
5+
class ValueType
6+
{
7+
public const STRING = 'string';
8+
public const INT = 'int';
9+
public const FLOAT = 'float';
10+
public const BOOLEAN = 'boolean';
11+
public const ARRAY = 'array';
12+
public const OBJECT = 'object';
13+
public const NULL = 'null';
14+
public const MIXED = 'mixed';
15+
16+
private function __construct()
17+
{
18+
19+
}
20+
}

0 commit comments

Comments
 (0)