diff --git a/src/Requests/BaseRequest.php b/src/Requests/BaseRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..9bebc7d6f452a61c26bc818de4af0c13f9a98d39 --- /dev/null +++ b/src/Requests/BaseRequest.php @@ -0,0 +1,62 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Validator\ConstraintViolation; +use Symfony\Component\Validator\Validator\ValidatorInterface; + +abstract class BaseRequest +{ + public function __construct(protected ValidatorInterface $validator) + { + $this->populate(); + + if ($this->autoValidateRequest()) { + $this->validate(); + } + } + + public function validate(): void + { + $errors = $this->validator->validate($this); + + $messages = ['message' => 'validation_failed', 'errors' => []]; + + /** @var ConstraintViolation $errors */ + foreach ($errors as $message) { + $messages['errors'][] = [ + 'property' => $message->getPropertyPath(), + 'value' => $message->getInvalidValue(), + 'message' => $message->getMessage(), + ]; + } + + if (count($messages['errors']) > 0) { + $response = new JsonResponse($messages, 201); + $response->send(); + + exit; + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } + + protected function populate(): void + { + foreach ($this->getRequest()->request->all() as $property => $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } + + protected function autoValidateRequest(): bool + { + return true; + } +} \ No newline at end of file diff --git a/src/Requests/DiffDaysRequest.php b/src/Requests/DiffDaysRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..8cba54ec46355d1a907b4ae71183f235dc6e3959 --- /dev/null +++ b/src/Requests/DiffDaysRequest.php @@ -0,0 +1,14 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\Validator\Constraints as Assert; + +class DiffDaysRequest extends BaseRequest +{ + #[Assert\Type('date')] + public $startDate; + + #[Assert\Type('date')] + public $endDate; +} \ No newline at end of file diff --git a/src/Requests/MenuRequest.php b/src/Requests/MenuRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..e012d1336f70877e112dd24c60320ef96a500e55 --- /dev/null +++ b/src/Requests/MenuRequest.php @@ -0,0 +1,16 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\Validator\Constraints as Assert; + +class MenuRequest extends BaseRequest +{ + #[Assert\All([ + new Assert\Collection([ + 'name' => new Assert\Type('string'), + 'depth' => new Assert\Type('int'), + ]) + ])] + public array $items; +} \ No newline at end of file diff --git a/src/Requests/SearchRequest.php b/src/Requests/SearchRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..eb03843a0e6d750fbe684a107a47ef6a93f12313 --- /dev/null +++ b/src/Requests/SearchRequest.php @@ -0,0 +1,20 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\Validator\Constraints as Assert; + +class SearchRequest extends BaseRequest +{ + #[Assert\All([ + new Assert\Collection([ + 'id' => new Assert\Type('int'), + 'name' => new Assert\Type('string'), + 'age' => new Assert\Type('int'), + ]) + ])] + public array $items; + + #[Assert\Type('int')] + public int $id; +} \ No newline at end of file diff --git a/src/Requests/SortPriceRequest.php b/src/Requests/SortPriceRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..38bdf00f9b75b9988f739a4209abf08ce32fa512 --- /dev/null +++ b/src/Requests/SortPriceRequest.php @@ -0,0 +1,16 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\Validator\Constraints as Assert; + +class SortPriceRequest extends BaseRequest +{ + #[Assert\All([ + new Assert\Collection([ + 'price' => new Assert\Type('int'), + 'count' => new Assert\Type('int') + ]) + ])] + public array $items; +} \ No newline at end of file diff --git a/src/Requests/UniqElementsRequest.php b/src/Requests/UniqElementsRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..f53156b25a3d24b1c0ec9f80af9dd7bc63184fb8 --- /dev/null +++ b/src/Requests/UniqElementsRequest.php @@ -0,0 +1,16 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\Validator\Constraints as Assert; + +class UniqElementsRequest extends BaseRequest +{ + #[Assert\All([ + new Assert\Type('array'), + new Assert\All([ + new Assert\Type('string'), + ]) + ])] + public array $items; +} \ No newline at end of file diff --git a/src/Validation/ArrayValidation.php b/src/Validation/ArrayValidation.php deleted file mode 100644 index 5807a447814593f64ecf069980c81099b8be861d..0000000000000000000000000000000000000000 --- a/src/Validation/ArrayValidation.php +++ /dev/null @@ -1,26 +0,0 @@ -<?php - -namespace App\Validation; - -class ArrayValidation -{ - public static function validateFunc1(array $array): bool - { - $prices = array_column($array, 'price'); - $counts = array_column($array, 'count'); - return ctype_digit(implode('',$prices)) && ctype_digit(implode('', $counts)); - } - - public static function validateFunc2(array $array): bool - { - $ids = array_column($array, 'id'); - $ages = array_column($array, 'age'); - return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages)); - } - - public static function validateFunc4(array $array): bool - { - $depths = array_column($array, 'depth'); - return ctype_digit(implode('', $depths)); - } -} \ No newline at end of file diff --git a/src/Validation/DateValidation.php b/src/Validation/DateValidation.php deleted file mode 100644 index 18046f84a0fef64f875f2f4c4a25c452d43df8c2..0000000000000000000000000000000000000000 --- a/src/Validation/DateValidation.php +++ /dev/null @@ -1,14 +0,0 @@ -<?php - -namespace App\Validation; - -class DateValidation -{ - public static function validate(string $date): bool - { - if (strtotime($date)) { - return true; - } - return false; - } -} \ No newline at end of file