Skip to content

Commit

Permalink
Add new expectParameters method
Browse files Browse the repository at this point in the history
Linter recommendations applied
  • Loading branch information
bradleyhodges committed Aug 26, 2024
1 parent bfd2cec commit d4c37b3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
39 changes: 21 additions & 18 deletions src/APIManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,10 +693,11 @@ public function expectParameters(string $uses, array $parameters, bool $handleRe
]);
$this->apiResponseManager->bailOut(400);
}

break;
case 'XML':
$xmlContent = file_get_contents("php://input");
if (!$xmlContent) {
if ($xmlContent === '' || $xmlContent === '0' || $xmlContent === false) {
$this->apiResponseManager->addError([
'status' => '400',
'source' => ['pointer' => '/data'],
Expand All @@ -705,6 +706,7 @@ public function expectParameters(string $uses, array $parameters, bool $handleRe
]);
$this->apiResponseManager->bailOut(400);
}

$inputData = simplexml_load_string($xmlContent);
if ($inputData === false) {
$this->apiResponseManager->addError([
Expand All @@ -715,6 +717,7 @@ public function expectParameters(string $uses, array $parameters, bool $handleRe
]);
$this->apiResponseManager->bailOut(400);
}

break;
case 'POST':
$inputData = $_POST;
Expand All @@ -730,25 +733,25 @@ public function expectParameters(string $uses, array $parameters, bool $handleRe
'status' => '400',
'source' => ['pointer' => '/data'],
'title' => 'Unsupported data source',
'detail' => "Unsupported data source: {$uses}",
'detail' => 'Unsupported data source: ' . $uses,
]);
$this->apiResponseManager->bailOut(400);
}

// Loop through the parameters to validate and sanitize them
$result = [];
foreach ($parameters as $param) {
$mandatory = $param['mandatory'] ?? true;
$requires = $param['requires'] ?? null;
$name = $param['name'] ?? $requires;
$format = $param['format'] ?? null;
$strictFormat = $param['strictFormat'] ?? true;
$sanitize = $param['sanitize'] ?? true;
$descriptor = $param['descriptor'] ?? $name;
$maxLength = $param['maxLength'] ?? null;
foreach ($parameters as $parameter) {
$mandatory = $parameter['mandatory'] ?? true;
$requires = $parameter['requires'] ?? null;
$name = $parameter['name'] ?? $requires;
$format = $parameter['format'] ?? null;
$strictFormat = $parameter['strictFormat'] ?? true;
$sanitize = $parameter['sanitize'] ?? true;
$descriptor = $parameter['descriptor'] ?? $name;
$maxLength = $parameter['maxLength'] ?? null;

// Create a dynamic JSON Pointer for the error source
$pointer = "/data/attributes/{$name}";
$pointer = '/data/attributes/' . $name;

// Check if the required parameter exists in the input data
$value = $inputData[$requires] ?? null;
Expand All @@ -759,7 +762,7 @@ public function expectParameters(string $uses, array $parameters, bool $handleRe
'status' => '400', // Bad Request
'source' => ['pointer' => $pointer],
'title' => 'Missing Attribute',
'detail' => "{$descriptor} must not be empty.",
'detail' => $descriptor . ' must not be empty.',
];
continue;
}
Expand All @@ -773,22 +776,22 @@ public function expectParameters(string $uses, array $parameters, bool $handleRe
// Validate the format if a validation rule is provided
if ($format !== null) {
if (is_string($format)) {
if ($strictFormat && !preg_match($format, $value)) {
if ($strictFormat && (preg_match($format, $value) === 0 || preg_match($format, $value) === false)) {
$errors[] = [
'status' => '422', // Unprocessable Entity
'source' => ['pointer' => $pointer],
'title' => 'Invalid Format',
'detail' => "{$descriptor} is not in the required format.",
'detail' => $descriptor . ' is not in the required format.',
];
continue;
}
} elseif (is_int($format) && !filter_var($value, $format, $param['options'] ?? [])) {
} elseif (is_int($format) && !filter_var($value, $format, $parameter['options'] ?? [])) {
if ($strictFormat) {
$errors[] = [
'status' => '422', // Unprocessable Entity
'source' => ['pointer' => $pointer],
'title' => 'Invalid Format',
'detail' => "{$descriptor} is not in the required format.",
'detail' => $descriptor . ' is not in the required format.',
];
continue;
}
Expand All @@ -805,7 +808,7 @@ public function expectParameters(string $uses, array $parameters, bool $handleRe
}

// Handle errors if there are any
if (!empty($errors)) {
if ($errors !== []) {
foreach ($errors as $error) {
$this->apiResponseManager->addError($error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ApiResponseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function bailOut(?int $statusCode = 400): never
*/
public function canContinue(): bool
{
return empty($this->globalErrors);
return $this->globalErrors === [];
}

/**
Expand Down

0 comments on commit d4c37b3

Please sign in to comment.