diff --git a/src/Actions/SortPriceAction.php b/src/Actions/SortPriceAction.php new file mode 100644 index 0000000000000000000000000000000000000000..13d202339227a31c984fa97d578baaa43318a1de --- /dev/null +++ b/src/Actions/SortPriceAction.php @@ -0,0 +1,31 @@ +<?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; + } +} diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php new file mode 100644 index 0000000000000000000000000000000000000000..417b4612aca35c041b3aa5b1f03f127c247a6a8d --- /dev/null +++ b/src/Controller/SortPriceController.php @@ -0,0 +1,32 @@ +<?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)); + } +} diff --git a/src/Entity/PricesEntity.php b/src/Entity/PricesEntity.php new file mode 100644 index 0000000000000000000000000000000000000000..2b3257f6aa23a2bd44c55b567b5a87533c36b4aa --- /dev/null +++ b/src/Entity/PricesEntity.php @@ -0,0 +1,39 @@ +<?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']; + } +}