Skip to content
Snippets Groups Projects
Commit 68f8659a authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

Merge branch 'PTPS_Controller_8' into PTPS_Controller_9

parents 2a5ed5cc c6ab1ce2
No related branches found
No related tags found
1 merge request!9Ptps controller 9
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -4,11 +4,11 @@ declare(strict_types=1);
namespace App\Action;
use DateTime;
use DateTimeImmutable;
use DateTime;
use DateInterval;
use Exception;
use DatePeriod;
use Exception;
class Functions
{
......@@ -52,8 +52,8 @@ class Functions
public function search(array $array, int $id): ?array
{
$rowId = array_search($id, array_column($array, 'id'), true);
if ($rowId) {
$rowId = array_search($id, array_column($array, 'id'), false);
if ($rowId !== false) {
return $array[$rowId];
}
return null;
......@@ -71,13 +71,13 @@ class Functions
}
/**
* Выходной массив:
* Array (
* [0] => Array([0] => laravel, [1] => php)
* [1] => Array([0] => codeigniter, [1] => php)
* [3] => Array([0] => c++, [1] => java))
* )
*/
* Выходной массив:
* Array (
* [0] => Array([0] => laravel, [1] => php)
* [1] => Array([0] => codeigniter, [1] => php)
* [3] => Array([0] => c++, [1] => java))
* )
*/
/**
* Сгруппировать подразедлы в верхние разделы меню
......@@ -94,7 +94,7 @@ class Functions
{
$result = [];
foreach ($aMenu as $arr) {
if ($arr['depth'] === 0) {
if ($arr['depth'] == 0) {
$result[] = array(
'name' => $arr['name'],
'depth' => $arr['depth'],
......@@ -193,11 +193,11 @@ class Functions
* проверяет, что файл существует и выводит пользователю весь контент файла
* (файл можешь создать любой)
* @param string $filePath путь до файла
* @return void
* @throws Exception
* @return string
* @throws RuntimeException
*/
public function readLogFile(string $filePath): void
public function readLogFile(string $filePath): string
{
if (file_exists($filePath)) {
$text = "";
......@@ -207,7 +207,7 @@ class Functions
$text .= $line;
}
fclose($file);
print $text;
return $text;
}
else {
throw new RuntimeException("File not found: $filePath");
......@@ -235,5 +235,4 @@ class Functions
throw new RuntimeException("File not found: $filePath");
}
}
}
}
\ No newline at end of file
......@@ -3,9 +3,12 @@
namespace App\Controller;
use App\Action\Functions;
use App\Validation\{ArrayValidation, DateValidation};
use DateTimeImmutable;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
......@@ -17,6 +20,101 @@ class HomeController extends AbstractController
$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('/func6/{year}', name: 'func6', methods: ['GET'])]
public function func6(int $year): Response
{
$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);
}
#[Route('/func7/{startDate}/{endDate}', name: 'func7')] // 01-01-2024
public function func7(string $startDate, string $endDate): Response
{
if (DateValidation::validate($startDate) && DateValidation::validate($endDate)) {
try {
$result = $this->functions->diffDays(
new DateTimeImmutable($startDate),
new DateTimeImmutable($endDate)
);
return $this->json(["The difference of days:" => $result]);
} catch (\Exception $e) {
return new Response($e->getMessage());
}
}
return new Response("Invalid date format");
}
#[Route('/func8/{fileName}', name: 'func8')]
public function func8(string $fileName): Response // text.txt
{
$filePath = $this->getParameter('kernel.project_dir') . "/public/files/";
$text = $this->functions->readLogFile($filePath . $fileName);
$response = new JsonResponse($text);
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
return $response;
}
#[Route('/{fileName}', name: 'home')]
public function home(string $fileName): Response // text.txt
{
......
<?php
namespace App\Validation;
class ArrayValidation
{
public static function validateFunc1(array $array): bool
{
$prices = array_column($array, 'price');
$counts = array_column($array, 'count');
return ctype_digit(implode('',$prices)) && ctype_digit(implode('', $counts));
}
public static function validateFunc2(array $array): bool
{
$ids = array_column($array, 'id');
$ages = array_column($array, 'age');
return ctype_digit(implode('', $ids)) && ctype_digit(implode('', $ages));
}
public static function validateFunc4(array $array): bool
{
$depths = array_column($array, 'depth');
return ctype_digit(implode('', $depths));
}
}
\ No newline at end of file
<?php
namespace App\Validation;
class DateValidation
{
public static function validate(string $date): bool
{
if (strtotime($date)) {
return true;
}
return false;
}
}
\ 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