Skip to content
Snippets Groups Projects
Commit a1f25123 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

temp commit

parent c6ab1ce2
No related branches found
No related tags found
1 merge request!8Ptps controller 8
<?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
<?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
<?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
<?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
<?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
<?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
<?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
<?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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment