Skip to content
Snippets Groups Projects
Commit 299d09ba authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS_Controller_3' into 'main'

Ptps controller 3

See merge request !3
parents a11f1d90 9ef57e2e
No related branches found
No related tags found
1 merge request!3Ptps controller 3
......@@ -23,4 +23,35 @@ class Functions
);
return $array;
}
/**
* Найдет элемент с указаным id
* @param array $array - массив, содержащий элементы со структурой
* [
* 'id' => 30,
* 'name' => 'Jhon',
* 'age' => 23,
* ]
* @param $id - ид искомого элемента
* @return ?array - найденный элемент
*/
public function search(array $array, int $id): ?array
{
$rowId = array_search($id, array_column($array, 'id'), false);
if ($rowId !== false) {
return $array[$rowId];
}
return null;
}
/**
* Удалить дубликаты, оставив только уникальные значения
* @param array $array
* @return array
*/
public function uniqElements(array $array): array
{
return array_unique($array, SORT_REGULAR);
}
}
\ No newline at end of file
......@@ -3,7 +3,11 @@
namespace App\Controller;
use App\Action\Functions;
use App\Requests\SortPriceRequest;
use App\Requests\{
SortPriceRequest,
SearchRequest,
UniqElementsRequest
};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
......@@ -18,4 +22,20 @@ class HomeController extends AbstractController
$array = $this->functions->sortPrice($request->getRequest()->toArray()['items']);
return $this->json($array);
}
#[Route('/search', name: 'search', methods: ['POST'])]
public function search(SearchRequest $request): Response
{
$array = $request->getRequest()->toArray()['items'];
$id = $request->getRequest()->query->get('id');
$result = $this->functions->search($array, $id);
return $this->json($result);
}
#[Route('/uniqElements', name: 'uniqElements', methods: ['POST'])]
public function uniqElements(UniqElementsRequest $request): Response
{
$result = $this->functions->uniqElements($request->getRequest()->toArray()['items']);
return $this->json($result);
}
}
<?php
namespace App\Requests;
use Symfony\Component\Validator\Constraints as Assert;
class SearchRequest extends BaseRequest
{
#[Assert\All([
new Assert\Collection([
'id' => new Assert\Type('int'),
'name' => new Assert\Type('string'),
'age' => new Assert\Type('int'),
])
])]
public array $items;
#[Assert\Type('int')]
public int $id;
}
\ No newline at end of file
<?php
namespace App\Requests;
use Symfony\Component\Validator\Constraints as Assert;
class UniqElementsRequest extends BaseRequest
{
#[Assert\All([
new Assert\Type('array'),
new Assert\All([
new Assert\Type('string'),
])
])]
public array $items;
}
\ 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