diff --git a/src/Action/Functions.php b/src/Action/Functions.php index 6bed9563d6e021ff4ddca2d70d1d220a4ba4b804..cf401a88e69c6ee9b6873506c83e41e7fba0df6e 100644 --- a/src/Action/Functions.php +++ b/src/Action/Functions.php @@ -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 diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index a5a3611cddaeecbb20280e4b75f78a841737598b..db9d53a5564324672f42776981da51b77b34d334 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -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); + } } diff --git a/src/Requests/SearchRequest.php b/src/Requests/SearchRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..eb03843a0e6d750fbe684a107a47ef6a93f12313 --- /dev/null +++ b/src/Requests/SearchRequest.php @@ -0,0 +1,20 @@ + 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 diff --git a/src/Requests/UniqElementsRequest.php b/src/Requests/UniqElementsRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..f53156b25a3d24b1c0ec9f80af9dd7bc63184fb8 --- /dev/null +++ b/src/Requests/UniqElementsRequest.php @@ -0,0 +1,16 @@ +