Commit ca9bf67d authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS_Controller_6' into 'main'

Ptps controller 6

See merge request !6
parents 217fc744 a36bd71e
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
@@ -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
+17 −1
Original line number Diff line number Diff line
@@ -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);
    }
}
+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
+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