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

create prices model sort action and controller

parent 9d38d459
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
<?php

namespace App\Actions;

use App\Entity\InterfaceDataEntity;

class SortPriceAction implements InterfaceAction
{
    /**
     * Выполняет сортировку массива по убыванию цены
     * @param InterfaceDataEntity $model
     * @return array отсортированный
     */
    public function act(InterfaceDataEntity $model): array
    {
        $array = $model->prices;

        $priceColumn = array_column($array, "price");
        $countColumn = array_column($array, "count");

        array_multisort(
            $priceColumn,
            SORT_DESC,
            $countColumn,
            SORT_ASC,
            $array,
        );

        return $array;
    }
}
+32 −0
Original line number Diff line number Diff line
<?php

namespace App\Controller;

use App\Actions\SortPriceAction;
use App\Entity\PricesEntity;
use App\Service\ValidationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

class SortPriceController extends AbstractController
{
    /**
     * Контроллер волняет сортировку массива по убыванию цены используя sortPrice
     * @param Request $request
     * @param SortPriceAction $action
     * @return JsonResponse
     */
    #[Route('/sort/price', name: 'app_sort_price', methods: ['POST'])]
    public function index(Request $request, SortPriceAction $action): JsonResponse
    {
        $priceEntity = new PricesEntity();
        $priceEntity->serialise($request);

        $validation = new ValidationService();
        $validation->validate($priceEntity);

        return new JsonResponse($action->act($priceEntity));
    }
}
+39 −0
Original line number Diff line number Diff line
<?php

namespace App\Entity;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Contracts\Service\Attribute\Required;

class PricesEntity implements InterfaceDataEntity
{
    #[Type('array')]
    #[NotBlank()]
    #[Required()]
    #[All(
        constraints: [
            new Collection(
                fields: [
                    'price' => [
                        new NotBlank(),
                        new Type('integer'),
                    ],
                    'count' => [
                        new NotBlank(),
                        new Type('integer'),
                    ],
                ],
            )
        ]
    )]
    public $prices;

    public function serialise(Request $request): void
    {
        $this->prices = $request->toArray()['prices'];
    }
}