Skip to content
Snippets Groups Projects
Commit 1482daa5 authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS|Controller_7' into 'main'

make controller

See merge request !7
parents 87eadfde 06ac412f
No related branches found
No related tags found
1 merge request!7make controller
<?php
namespace App\Actions;
use DateTimeImmutable;
class DiffDaysAction
{
/**
* Вернет кол-во дней между датами
* @param DateTimeImmutable $dateStart дата начала
* @param DateTimeImmutable $dateEnd дата окончания
* @return int
* */
public function act(
DateTimeImmutable $dateStart,
DateTimeImmutable $dateEnd
): int {
return (int) $dateStart->diff($dateEnd)->format('%a');
}
}
<?php
namespace App\Controller;
use App\Actions\DiffDaysAction;
use App\Requests\DiffDaysRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class DiffDaysController extends AbstractController
{
/**
* Контроллер вернет кол-во дней между датами
* @param DiffDaysRequest $request
* @param DiffDaysAction $action
* @return Response
*/
#[Route('/diff/days', name: 'app_diff_days', methods: ['POST'])]
public function index(DiffDaysRequest $request, DiffDaysAction $action): Response
{
$array = $request->serialise();
return new JsonResponse($action->act($array['startDate'], $array['endDate']));
}
}
<?php
namespace App\Requests;
use DateTimeImmutable;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\NotBlank;
class DiffDaysRequest extends BaseRequest
{
#[Date]
#[NotBlank]
public $startDate;
#[Date]
#[NotBlank]
public $endDate;
/**
* @return mixed
*/
public function serialise(): mixed
{
return [
'startDate' => new DateTimeImmutable($this->startDate),
'endDate' => new DateTimeImmutable($this->endDate),
];
}
}
\ 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