Loading src/Action/Functions.php +31 −0 Original line number Diff line number Diff line Loading @@ -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 src/Controller/HomeController.php +21 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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); } } src/Requests/SearchRequest.php 0 → 100644 +20 −0 Original line number Diff line number Diff line <?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 src/Requests/UniqElementsRequest.php 0 → 100644 +16 −0 Original line number Diff line number Diff line <?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 Loading
src/Action/Functions.php +31 −0 Original line number Diff line number Diff line Loading @@ -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
src/Controller/HomeController.php +21 −1 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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); } }
src/Requests/SearchRequest.php 0 → 100644 +20 −0 Original line number Diff line number Diff line <?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
src/Requests/UniqElementsRequest.php 0 → 100644 +16 −0 Original line number Diff line number Diff line <?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