Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • a.shamavov/iqdevtranningprogram
1 result
Show changes
Commits on Source (12)
......@@ -23,4 +23,25 @@ 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;
}
}
\ No newline at end of file
......@@ -3,7 +3,10 @@
namespace App\Controller;
use App\Action\Functions;
use App\Requests\SortPriceRequest;
use App\Requests\{
SortPriceRequest,
SearchRequest
};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
......@@ -18,4 +21,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
{
$requestData = $request->getRequest()->toArray();
$id = $request->getRequest()->get('id');
$result = $this->functions->search($requestData['items'], $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