Loading src/Action/Functions.php +20 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,9 @@ namespace App\Action; use Exception; use DateTimeImmutable; use DateTime; use DatePeriod; use DateInterval; class Functions { Loading Loading @@ -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 src/Controller/HomeController.php +17 −1 Original line number Diff line number Diff line Loading @@ -8,7 +8,8 @@ use App\Requests\{ SearchRequest, UniqElementsRequest, MenuRequest, HowDaysToNyRequest HowDaysToNyRequest, CountFriday13Request }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; Loading Loading @@ -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); } } src/Requests/CountFriday13Request.php 0 → 100644 +21 −0 Original line number Diff line number Diff line <?php namespace App\Requests; use Symfony\Component\Validator\Constraints as Assert; class CountFriday13Request extends BaseRequest { #[Assert\Type('int')] #[Assert\Positive] public int $year; protected function populate(): void { foreach ($this->getRequest()->query->all() as $property => $value) { if (property_exists($this, $property)) { $this->{$property} = $value; } } } } No newline at end of file templates/home.html.twig +4 −1 Original line number Diff line number Diff line {% block body %} <h1>Кол-во дней до НГ: {{ count }}</h1> <h1>Кол-во пятниц 13 в году: {{ fridays|length }}</h1> {% for day in fridays %} <p>{{ day }}</p> {% endfor %} {% endblock %} No newline at end of file Loading
src/Action/Functions.php +20 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,9 @@ namespace App\Action; use Exception; use DateTimeImmutable; use DateTime; use DatePeriod; use DateInterval; class Functions { Loading Loading @@ -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
src/Controller/HomeController.php +17 −1 Original line number Diff line number Diff line Loading @@ -8,7 +8,8 @@ use App\Requests\{ SearchRequest, UniqElementsRequest, MenuRequest, HowDaysToNyRequest HowDaysToNyRequest, CountFriday13Request }; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; Loading Loading @@ -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); } }
src/Requests/CountFriday13Request.php 0 → 100644 +21 −0 Original line number Diff line number Diff line <?php namespace App\Requests; use Symfony\Component\Validator\Constraints as Assert; class CountFriday13Request extends BaseRequest { #[Assert\Type('int')] #[Assert\Positive] public int $year; protected function populate(): void { foreach ($this->getRequest()->query->all() as $property => $value) { if (property_exists($this, $property)) { $this->{$property} = $value; } } } } No newline at end of file
templates/home.html.twig +4 −1 Original line number Diff line number Diff line {% block body %} <h1>Кол-во дней до НГ: {{ count }}</h1> <h1>Кол-во пятниц 13 в году: {{ fridays|length }}</h1> {% for day in fridays %} <p>{{ day }}</p> {% endfor %} {% endblock %} No newline at end of file