diff --git a/src/Actions/IdSearchAction.php b/src/Actions/IdSearchAction.php new file mode 100644 index 0000000000000000000000000000000000000000..695329150f8356cafbe37813f6c032ff44fd337b --- /dev/null +++ b/src/Actions/IdSearchAction.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Actions; + +class IdSearchAction +{ + /** + * Ðайдет Ñлемент Ñ ÑƒÐºÐ°Ð·Ð°Ð½Ñ‹Ð¼ id + * @param array $array - маÑÑив, Ñодержащий Ñлементы Ñо Ñтруктурой + * [ + * 'id' => 30, + * 'name' => 'Jhon', + * 'age' => 23, + * ] + * @param $id - ид иÑкомого Ñлемента + * @return array|null - найденный Ñлемент/ вернет null при его отÑутÑтвии + */ + public static function act(array $array): ?array + { + + foreach ($array['users'] as $item){ + if ($item['id'] === $array['id']){ + return $item; + } + } + + return null; + } +} diff --git a/src/Controller/IdSearchController.php b/src/Controller/IdSearchController.php new file mode 100644 index 0000000000000000000000000000000000000000..86ba51440ce4ad7a521308c256fa8fea71e2f3ad --- /dev/null +++ b/src/Controller/IdSearchController.php @@ -0,0 +1,27 @@ +<?php + +namespace App\Controller; + +use App\Actions\IdSearchAction; +use App\Requests\UsersRequest; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\Routing\Attribute\Route; + +class IdSearchController extends AbstractController +{ + /** + * Контроллер найдет Ñлемент Ñ ÑƒÐºÐ°Ð·Ð°Ð½Ñ‹Ð¼ id иÑÐ¿Ð¾Ð»ÑŒÐ·ÑƒÑ sortPrice + * @param UsersRequest $request + * @param IdSearchAction $action + * @return JsonResponse + */ + #[Route('/search', name: 'app_search', methods: ['POST'])] + public function index(UsersRequest $request, IdSearchAction $action): JsonResponse + { + return new JsonResponse( + $action->act($request->serialise()), + 200 + ); + } +} \ No newline at end of file diff --git a/src/Requests/UsersRequest.php b/src/Requests/UsersRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..1d5bd95f0d2b5f3840e001e6247e272c555a8d82 --- /dev/null +++ b/src/Requests/UsersRequest.php @@ -0,0 +1,45 @@ +<?php + +namespace App\Requests; + +use Symfony\Component\Validator\Constraints\All; +use Symfony\Component\Validator\Constraints\Collection; +use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\Constraints\Optional; +use Symfony\Component\Validator\Constraints\Type; + +class UsersRequest extends BaseRequest +{ + #[Type('integer')] + #[NotBlank] + public $id; + + #[Type('array')] + #[All( + constraints: new Collection(fields: [ + 'id' => [ + new Type('integer'), + new NotBlank(), + ], + 'name' => [ + new Type('string'), + new NotBlank(), + ], + 'age' => new Optional([ + new Type('integer'), + ]) + ]) + )] + public $users; + + /** + * @return array + */ + public function serialise(): array + { + return [ + 'id' => $this->id, + 'users' => $this->users, + ]; + } +} \ No newline at end of file