Skip to content
Snippets Groups Projects
Commit 87eadfde authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS|Controller_2' into 'main'

Ptps|controller 2

See merge request !2
parents ecb8369a d8d5f596
No related branches found
No related tags found
1 merge request!2Ptps|controller 2
<?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;
}
}
<?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
<?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
{% 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 %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment