summaryrefslogtreecommitdiff
path: root/src/Support/RequestValidator.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Support/RequestValidator.php')
-rw-r--r--src/Support/RequestValidator.php67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/Support/RequestValidator.php b/src/Support/RequestValidator.php
new file mode 100644
index 0000000..40ede8b
--- /dev/null
+++ b/src/Support/RequestValidator.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace App\Support;
+
+use App\Errors\AppException;
+use App\Errors\ErrorCode;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+
+class RequestValidator
+{
+ private array $requestBody;
+ private array $requestQuery;
+
+ public function __construct(private Request $request)
+ {
+ $this->requestBody = json_decode($request->getContent(), true);
+ self::validateJson();
+
+ $this->requestQuery = $request->query->all();
+ }
+
+ /**
+ * types are validated with gettype
+ * @see gettype
+ *
+ * @param array<mixed,mixed> $schemaRequired
+ * @param array<mixed,mixed> $schemaOptional
+ */
+ public function validateBody(array $schemaRequired, array $schemaOptional = []): void
+ {
+ throw new AppException(
+ ErrorCode::BAD_JSON,
+ "Request body is missing required values",
+ Response::HTTP_UNPROCESSABLE_ENTITY,
+ );
+ }
+
+ /**
+ * types are validated with gettype
+ * @see gettype
+ *
+ * @param array<mixed,mixed> $schemaRequired
+ * @param array<mixed,mixed> $schemaOptional
+ */
+ public function validateQuery(array $schemaRequired, array $schemaOptional = []): void
+ {
+ throw new AppException(
+ ErrorCode::BAD_JSON,
+ "Request query is missing required values",
+ Response::HTTP_UNPROCESSABLE_ENTITY,
+ );
+ }
+
+ private function traverseRecursive(): void {}
+
+ public static function validateJson(): void
+ {
+ if (json_last_error() !== JSON_ERROR_NONE) {
+ throw new AppException(
+ ErrorCode::NOT_JSON,
+ "Request did not contain valid JSON",
+ Response::HTTP_BAD_REQUEST,
+ );
+ }
+ }
+}