Skip to content

Commit f8991d1

Browse files
committed
merged FieldFormatDefinition and RequestParamData - now all checks are done using Validators (except for nullability/required)
1 parent 42934b0 commit f8991d1

File tree

4 files changed

+45
-152
lines changed

4 files changed

+45
-152
lines changed

app/V1Module/presenters/base/BasePresenter.php

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -243,29 +243,9 @@ private function processParamsLoose(array $paramData)
243243
$paramValue = $this->getValueFromParamData($param);
244244
$formatInstanceArr[$param->name] = $paramValue;
245245

246-
// check if null
247-
if ($paramValue === null) {
248-
if ($param->required) {
249-
throw new InvalidArgumentException($param->name, "The parameter is required and cannot be null.");
250-
}
251-
252-
if (!$param->nullable) {
253-
throw new InvalidArgumentException(
254-
$param->name,
255-
"The parameter is not nullable and thus cannot be null."
256-
);
257-
}
258-
259-
// only non null values should be validated
260-
continue;
261-
}
262-
263-
// use every provided validator
264-
foreach ($param->validators as $validator) {
265-
if (!$validator->validate($paramValue)) {
266-
throw new InvalidArgumentException($param->name);
267-
}
268-
}
246+
// this throws when it does not conform
247+
$this->logger->log(var_export($param, true), ILogger::DEBUG);
248+
$param->conformsToDefinition($paramValue);
269249
}
270250

271251
// cast to stdClass
@@ -285,10 +265,7 @@ private function processParamsFormat(string $format)
285265

286266
///TODO: handle nested MetaFormat creation
287267
$formatInstance = MetaFormatHelper::createFormatInstance($format);
288-
foreach ($nameToFieldDefinitionsMap as $fieldName => $fieldData) {
289-
$requestParamData = $fieldData->requestData;
290-
//$this->logger->log(var_export($requestParamData, true), ILogger::DEBUG);
291-
268+
foreach ($nameToFieldDefinitionsMap as $fieldName => $requestParamData) {
292269
$value = $this->getValueFromParamData($requestParamData);
293270

294271
if (!$formatInstance->checkedAssign($fieldName, $value)) {

app/helpers/MetaFormats/FieldFormatDefinition.php

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

app/helpers/MetaFormats/MetaFormatHelper.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,15 @@ public static function createNameToFieldDefinitionsMap(string $className)
152152
$formats = [];
153153
foreach ($fields as $fieldName => $value) {
154154
$field = $class->getProperty($fieldName);
155-
// the format can be null (not present)
156-
$format = self::extractFormatFromAttribute($field);
157-
// get null if there is no type
158-
$reflectionType = $field->getType();
159-
$fieldType = $reflectionType?->getName();
160-
$nullable = $reflectionType?->allowsNull() ?? false;
161-
162155
$requestParamData = self::extractFormatParameterData($field);
163156
if ($requestParamData === null) {
164157
throw new InternalServerException(
165158
"The field $fieldName of class $className does not have a RequestAttribute."
166159
);
167160
}
168161

169-
$formats[$fieldName] = new FieldFormatDefinition($format, $fieldType, $nullable, $requestParamData);
162+
///TODO: add base type (PHP type of the field) validators to $requestParamData
163+
$formats[$fieldName] = $requestParamData;
170164
}
171165

172166
return $formats;

app/helpers/MetaFormats/RequestParamData.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Helpers\MetaFormats;
44

5+
use App\Exceptions\InvalidArgumentException;
56
use App\Helpers\MetaFormats\Validators\StringValidator;
67
use App\Helpers\Swagger\AnnotationParameterData;
78

@@ -30,6 +31,44 @@ public function __construct(
3031
$this->nullable = $nullable;
3132
}
3233

34+
/**
35+
* Checks whether a value meets this definition.
36+
* @param mixed $value The value to be checked.
37+
* @throws \App\Exceptions\InvalidArgumentException Thrown when the value does not meet the definition.
38+
* @return bool Returns whether the value passed the test.
39+
*/
40+
public function conformsToDefinition(mixed $value)
41+
{
42+
// check if null
43+
if ($value === null) {
44+
if (!$this->required) {
45+
///TODO: what if a required param can be null? Does that mean that required & null is fine? How to check the required constrains then?
46+
//throw new InvalidArgumentException($this->name, "The parameter is required and cannot be null.");
47+
return true;
48+
}
49+
50+
if (!$this->nullable) {
51+
throw new InvalidArgumentException(
52+
$this->name,
53+
"The parameter is not nullable and thus cannot be null."
54+
);
55+
}
56+
57+
// only non null values should be validated
58+
// (validators do not expect null)
59+
return true;
60+
}
61+
62+
// use every provided validator
63+
foreach ($this->validators as $validator) {
64+
if (!$validator->validate($value)) {
65+
throw new InvalidArgumentException($this->name);
66+
}
67+
}
68+
69+
return true;
70+
}
71+
3372
public function toAnnotationParameterData()
3473
{
3574
$dataType = null;

0 commit comments

Comments
 (0)