Skip to content
Snippets Groups Projects
Commit 2e9d064e authored by Александр Плохих's avatar Александр Плохих :waxing_gibbous_moon:
Browse files

add controller | action | request

parent 17fe374e
No related branches found
No related tags found
1 merge request!6make controller
<?php
namespace App\Actions;
use DateTimeImmutable;
class CountFriday13Action
{
/**
* Вернет все пятницы 13 в году
* @param int $year год, в котором необходимо произвести расчет
* @return DateTimeImmutable[]
*/
public function act(int $year): array
{
$AllFri13 = [];
for ($i = 1; $i <= 12; $i++) {
$next13 = (new DateTimeImmutable())->setDate($year, $i, 13);
if ($next13->format("D") === "Fri") {
$AllFri13[] = $next13;
}
}
return $AllFri13;
}
}
...@@ -2,27 +2,24 @@ ...@@ -2,27 +2,24 @@
namespace App\Controller; namespace App\Controller;
use App\Actions\CountFriday13Action;
use App\Requests\AllFri13Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use function App\Repository\countFriday13;
class CountFriday13Controller extends AbstractController class CountFriday13Controller extends AbstractController
{ {
/** /**
* git Контроллер вернет все пятницы 13 в году используя countFriday13 функцию * git Контроллер вернет все пятницы 13 в году используя countFriday13 функцию
* @param Request $request * @param AllFri13Request $request
* @param CountFriday13Action $action
* @return Response * @return Response
*/ */
#[Route('/count/friday13', name: 'app_count_friday13')] #[Route('/count/friday13', name: 'app_count_friday13')]
public function index(Request $request): Response public function index(AllFri13Request $request, CountFriday13Action $action): Response
{ {
$year = $request->toArray()['year']; return new JsonResponse($action->act($request->serialise()));
$returnableArray = countFriday13($year);
return new JsonResponse($returnableArray, Response::HTTP_OK);
} }
} }
<?php
namespace App\Repository;
use DateTimeImmutable;
/**
* Вернет все пятницы 13 в году
* @param int $yaer год, в котором необходимо произвести расчет
* @return DateTimeImmutable[]
*/
function countFriday13(int $year): iterable {
$date = new DateTimeImmutable();
$AllFri13 = [];
for ($i = 1; $i<=12; $i++ ){
$next13 = $date->setDate($year, $i, 13);
if ($next13->format("D") === 'Fri'){
$AllFri13[] = $next13;
}
}
return $AllFri13;
}
<?php
namespace App\Requests;
use Symfony\Component\Validator\Constraints\Positive;
use Symfony\Component\Validator\Constraints\Type;
class AllFri13Request extends BaseRequest
{
#[Type('integer')]
#[Positive]
public $year;
/**
* @return mixed
*/
public function serialise(): int
{
return (int) $this->year;
}
}
\ 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