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..3dd1716b17df2919d4f5fc5ca78d4930393cd9b2 --- /dev/null +++ b/src/Controller/IdSearchController.php @@ -0,0 +1,24 @@ +<?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())); + } +} \ 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 diff --git a/templates/id_search/index.html.twig b/templates/id_search/index.html.twig deleted file mode 100644 index 69a280e448c95e97964807ec52c04cf17bff3994..0000000000000000000000000000000000000000 --- a/templates/id_search/index.html.twig +++ /dev/null @@ -1,20 +0,0 @@ -{% extends 'base.html.twig' %} - -{% block title %}Hello IdSearchController!{% endblock %} - -{% block body %} -<style> - .example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; } - .example-wrapper code { background: #F5F5F5; padding: 2px 6px; } -</style> - -<div class="example-wrapper"> - <h1>Hello {{ controller_name }}! ✅</h1> - - This friendly message is coming from: - <ul> - <li>Your controller at <code>/home/tamanit/myProj/iqdevTranningProgram/src/Controller/IdSearchController.php</code></li> - <li>Your template at <code>/home/tamanit/myProj/iqdevTranningProgram/templates/id_search/index.html.twig</code></li> - </ul> -</div> -{% endblock %}