Loading src/Controller/HomeController.php +70 −17 Original line number Diff line number Diff line Loading @@ -2,9 +2,8 @@ namespace App\Controller; use DateInterval; use DatePeriod; use DateTime; use App\Action\Functions; use App\Validation\{ArrayValidation, DateValidation}; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; Loading @@ -13,29 +12,83 @@ use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { private function countFriday13(int $year): iterable { $stardDate = new DateTime("{$year}-01-01 Friday"); $year += 1; $endDate = new DateTime("{$year}-01-01"); $interval = new DateInterval('P7D'); foreach(new DatePeriod($stardDate, $interval, $endDate) as $day) { yield new DateTimeImmutable($day->format("Y-m-d")); private Functions $functions; public function __construct(Functions $functions) { $this->functions = $functions; } #[Route('/func1', name: 'home', methods: ['POST'])] public function func1(Request $request): Response { $array = $request->get('arr'); if (!ArrayValidation::validateFunc1($array)) { return new Response("Invalid array"); } $array = $this->functions->sortPrice($array); return $this->json($array); } #[Route('/func2', name: 'func2', methods: ['POST'])] public function func2(Request $request): Response { $id = $request->query->getInt('id'); $array = $request->get('arr'); if (!ArrayValidation::validateFunc2($array)) { return new Response("Invalid array"); } $result = $this->functions->search($array, $id); return $this->json($result); } #[Route('/func3', name: 'func3', methods: ['POST'])] public function home(Request $request): Response { $array = $request->get('arr'); $result = $this->functions->uniqElements($array); return $this->json($result); } #[Route('/func4', name: 'func4', methods: ['POST'])] public function func4(Request $request): Response { $array = $request->get('arr'); if (!ArrayValidation::validateFunc4($array)) { return new Response("Invalid array"); } $result = $this->functions->prepareMenu($array); return $this->json($result); } #[Route('/func5/{day}/{month}/{year}', name: 'func5')] public function func5(int $day, int $month, int $year): Response { $dateAsString = $year . "-" . $month . "-" . $day; try { $result = $this->functions->howDaysToNy(new DateTimeImmutable($dateAsString)); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json(["Days before NY:" => $result]); } #[Route('/{year}', name: 'home')] public function home(int $year): Response #[Route('/func6/{year}', name: 'func6', methods: ['GET'])] public function func6(int $year): Response { $fridays = array(); foreach($this->countFriday13($year) as $date) { try { foreach ($this->functions->countFriday13($year) as $date) { $fridays[] = $date->format("Y-m-d l"); } return $this->render('home.html.twig', ['fridays' => $fridays]); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json($fridays); } #[Route('/{startDate}/{endDate}', name: 'home')] // 01-01-2024 public function home(string $startDate, string $endDate): Response #[Route('/func7/{startDate}/{endDate}', name: 'home')] // 01-01-2024 public function func7(string $startDate, string $endDate): Response { if (DateValidation::validate($startDate) && DateValidation::validate($endDate)) { try { Loading Loading
src/Controller/HomeController.php +70 −17 Original line number Diff line number Diff line Loading @@ -2,9 +2,8 @@ namespace App\Controller; use DateInterval; use DatePeriod; use DateTime; use App\Action\Functions; use App\Validation\{ArrayValidation, DateValidation}; use DateTimeImmutable; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; Loading @@ -13,29 +12,83 @@ use Symfony\Component\Routing\Attribute\Route; class HomeController extends AbstractController { private function countFriday13(int $year): iterable { $stardDate = new DateTime("{$year}-01-01 Friday"); $year += 1; $endDate = new DateTime("{$year}-01-01"); $interval = new DateInterval('P7D'); foreach(new DatePeriod($stardDate, $interval, $endDate) as $day) { yield new DateTimeImmutable($day->format("Y-m-d")); private Functions $functions; public function __construct(Functions $functions) { $this->functions = $functions; } #[Route('/func1', name: 'home', methods: ['POST'])] public function func1(Request $request): Response { $array = $request->get('arr'); if (!ArrayValidation::validateFunc1($array)) { return new Response("Invalid array"); } $array = $this->functions->sortPrice($array); return $this->json($array); } #[Route('/func2', name: 'func2', methods: ['POST'])] public function func2(Request $request): Response { $id = $request->query->getInt('id'); $array = $request->get('arr'); if (!ArrayValidation::validateFunc2($array)) { return new Response("Invalid array"); } $result = $this->functions->search($array, $id); return $this->json($result); } #[Route('/func3', name: 'func3', methods: ['POST'])] public function home(Request $request): Response { $array = $request->get('arr'); $result = $this->functions->uniqElements($array); return $this->json($result); } #[Route('/func4', name: 'func4', methods: ['POST'])] public function func4(Request $request): Response { $array = $request->get('arr'); if (!ArrayValidation::validateFunc4($array)) { return new Response("Invalid array"); } $result = $this->functions->prepareMenu($array); return $this->json($result); } #[Route('/func5/{day}/{month}/{year}', name: 'func5')] public function func5(int $day, int $month, int $year): Response { $dateAsString = $year . "-" . $month . "-" . $day; try { $result = $this->functions->howDaysToNy(new DateTimeImmutable($dateAsString)); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json(["Days before NY:" => $result]); } #[Route('/{year}', name: 'home')] public function home(int $year): Response #[Route('/func6/{year}', name: 'func6', methods: ['GET'])] public function func6(int $year): Response { $fridays = array(); foreach($this->countFriday13($year) as $date) { try { foreach ($this->functions->countFriday13($year) as $date) { $fridays[] = $date->format("Y-m-d l"); } return $this->render('home.html.twig', ['fridays' => $fridays]); } catch (\Exception $e) { return new Response($e->getMessage()); } return $this->json($fridays); } #[Route('/{startDate}/{endDate}', name: 'home')] // 01-01-2024 public function home(string $startDate, string $endDate): Response #[Route('/func7/{startDate}/{endDate}', name: 'home')] // 01-01-2024 public function func7(string $startDate, string $endDate): Response { if (DateValidation::validate($startDate) && DateValidation::validate($endDate)) { try { Loading