diff --git a/src/Action/Functions.php b/src/Action/Functions.php index cf39542d7c5af531de7b17e84a0cc09eeda01c38..acd4ab97f7e62667f7033b8dee64d148873ed0fc 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -6,6 +6,9 @@ namespace App\Action; use Exception; use DateTimeImmutable; +use DateTime; +use DatePeriod; +use DateInterval; class Functions { @@ -100,4 +103,21 @@ class Functions $dateInterval = date_diff(new DateTimeImmutable($endYear), $date); return (int)$dateInterval->format("%a") + 1; } + + /** + * Вернет все пятницы 13 в году + * @param int $year год, в котором необходимо произвести расчет + * @return DateTimeImmutable[] + * @throws Exception + */ + public function countFriday13(int $year): iterable + { + $startDate = new DateTime("$year-01-01 Friday"); + ++$year; + $endDate = new DateTime("$year-01-01"); + $interval = new DateInterval('P7D'); + foreach (new DatePeriod($startDate, $interval, $endDate) as $day) { + yield new DateTimeImmutable($day->format("Y-m-d")); + } + } } \ No newline at end of file diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 1b2695df759e9b8dc5cd4adfca949a12cb252a20..1d3494287cd0a17a857c5173ca974c81ec013348 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -8,7 +8,8 @@ use App\Requests\{ SearchRequest, UniqElementsRequest, MenuRequest, - HowDaysToNyRequest + HowDaysToNyRequest, + CountFriday13Request }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -60,4 +61,19 @@ class HomeController extends AbstractController } return $this->json(["Days before NY:" => $result]); } + + #[Route('/countFriday13', name: 'countFriday13', methods: ['GET'])] + public function countFriday13(CountFriday13Request $request): Response + { + $year = $request->getRequest()->get('year'); + $fridays = array(); + try { + foreach ($this->functions->countFriday13($year) as $date) { + $fridays[] = $date->format("Y-m-d l"); + } + } catch (\Exception $e) { + return new Response($e->getMessage()); + } + return $this->json($fridays); + } } diff --git a/src/Requests/CountFriday13Request.php b/src/Requests/CountFriday13Request.php new file mode 100644 index 0000000000000000000000000000000000000000..e34f1b85da36cf2aaf57490d1f23ccdfaef3925c --- /dev/null +++ b/src/Requests/CountFriday13Request.php @@ -0,0 +1,21 @@ +getRequest()->query->all() as $property => $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } +} \ No newline at end of file diff --git a/templates/home.html.twig b/templates/home.html.twig index 6ce69c6ff8ec4817cb0d43c5b94b8741627c4edc..bb1d7381066549854b7c126f51b8584f07a82e53 100644 --- a/templates/home.html.twig +++ b/templates/home.html.twig @@ -1,3 +1,6 @@ {% block body %} -

Кол-во дней до НГ: {{ count }}

+

Кол-во пятниц 13 в году: {{ fridays|length }}

+ {% for day in fridays %} +

{{ day }}

+ {% endfor %} {% endblock %} \ No newline at end of file