Commit 78234963 authored by Александр Плохих's avatar Александр Плохих 🌔
Browse files

create filtrer controller and action and req

parent 258321e3
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
<?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
+8 −14
Original line number Diff line number Diff line
@@ -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
        );
    }
}
+0 −29
Original line number Diff line number Diff line
<?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;
}
+21 −0
Original line number Diff line number Diff line
<?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;
    }
}
+20 −0
Original line number Diff line number Diff line
{% 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 %}