Skip to content

Commit

Permalink
Update Request.php
Browse files Browse the repository at this point in the history
  • Loading branch information
secure73 authored May 8, 2024
1 parent 5b21911 commit abfe285
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,18 @@ public function getStartExecutionTime(): float
*/
public function definePostSchema(array $toValidatePost): bool
{
//TODO: brake this function into smaller functions
$errors = []; // Initialize an empty array to store errors
$requires = [];
$optionals = [];
$all = [];

foreach ($toValidatePost as $validation_key => $validationString) {
(substr($validation_key, 0, 1) === '?') ? $requires[$validation_key] = $validationString : $optionals[ltrim($validation_key, '?')] = $validationString; // Use ternary operator
(!substr($validation_key, 0, 1) === '?') ? $requires[$validation_key] = $validationString : $optionals[ltrim($validation_key, '?')] = $validationString; // Use ternary operator
$all[$validation_key] = $validationString;
}
foreach($this->post as $postName => $postValue) { //if there is any post other than defined schma, instanlty breake the process and return false.
if(!array_key_exists($postName ,$requires) || !array_key_exists($postName,$optionals)) {
if(!array_key_exists($postName ,$all)) {
$errors[$postName] = "unwanted post $postName";
unset($this->post[$postName]);
}
Expand All @@ -116,7 +119,6 @@ public function definePostSchema(array $toValidatePost): bool
foreach($requires as $validation_key => $validation_value) { //now only check existance of requires post
if ((!isset($this->post[$validation_key]) || empty($this->post[$validation_key]))) {
$errors[] = "Missing required field: $validation_key";
continue; // Skip to the next iteration
}
}
if (count($errors) > 0) { //if requires not exists , stop process and return false
Expand All @@ -125,6 +127,7 @@ public function definePostSchema(array $toValidatePost): bool
}
return false;
}

foreach($requires as $validation_key => $validationString) { //now validate requires post Schema
$validationResult = $this->checkPostKeyValue($validation_key, $validationString);
if (!$validationResult) {
Expand Down

0 comments on commit abfe285

Please sign in to comment.