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

create prices model sort action and controller

parent 9d38d459
No related branches found
No related tags found
1 merge request!1Ptps|controller 1
<?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;
}
}
<?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));
}
}
<?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'];
}
}
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