Skip to content
Snippets Groups Projects
Commit 78234963 authored by Александр Плохих's avatar Александр Плохих :waxing_gibbous_moon:
Browse files

create filtrer controller and action and req

parent 258321e3
No related branches found
No related tags found
1 merge request!3Ptps|controller 3
<?php
namespace App\Actions;
class UniqElementsAction
{
/**
* Удаляет дубликаты, оставив только уникальные значения
* @param array $array
* @return array
*/
public static function act(array $array): array
{
return array_unique($array, SORT_REGULAR);
}
}
\ No newline at end of file
......@@ -2,27 +2,21 @@
namespace App\Controller;
use App\Actions\UniqElementsAction;
use App\Requests\UniqElementsRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\HttpFoundation\Request;
use function App\Repository\uniqElements;
class UniqElementsController extends AbstractController
{
/**
* Контроллер удаляет дубликаты, оставив только уникальные значения используя uniqElements функцию
* @param Request $request
* @return Response
*/
#[Route('/uniq/elements', name: 'app_uniq_elements')]
public function index(Request $request): Response
#[Route('/uniq/elements', name: 'app_uniq_elements', methods: ['POST'])]
public function index(UniqElementsRequest $request, UniqElementsAction $action): Response
{
$requestArray = $request->toArray();
$return = uniqElements($requestArray);
return new JsonResponse($return, Response::HTTP_OK);
return new JsonResponse(
$action->act($request->serialise()),
200
);
}
}
<?php
namespace App\Repository;
/**
* Удалить дубликаты, оставив только уникальные значения
* @param array $array
* @return array
*/
function uniqElements(array $array): array {
$splitedArray = [];
foreach ($array as $tuple) {
$key = $tuple[0];
$item = $tuple[1];
$splitedArray[$key][] = $item;
}
$returnableArray = [];
foreach ($splitedArray as $key => $item){
$item = array_unique($item);
foreach ($item as $oneOfItem){
$returnableArray[] = [
$key,
$oneOfItem
];
}
}
return $returnableArray;
}
<?php
namespace App\Requests;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Contracts\Service\Attribute\Required;
class UniqElementsRequest extends BaseRequest
{
#[Type('array')]
#[Required]
public $data;
/**
* @return mixed
*/
public function serialise(): mixed
{
return $this->data;
}
}
{% extends 'base.html.twig' %}
{% block title %}Hello UniqElementsController!{% 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/UniqElementsController.php</code></li>
<li>Your template at <code>/home/tamanit/myProj/iqdevTranningProgram/templates/uniq_elements/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