Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
1 merge request!6Ptps controller 6
......@@ -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
......@@ -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);
}
}
<?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
{% 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
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