Skip to content
Snippets Groups Projects
Commit 258c3f56 authored by Адлан Шамавов's avatar Адлан Шамавов
Browse files

refactoring

parent 0ad5009d
No related branches found
No related tags found
1 merge request!1Ptps controller 1
......@@ -23,3 +23,4 @@
/public/assets/
/assets/vendor/
###< symfony/asset-mapper ###
/.idea
\ No newline at end of file
<?php
declare(strict_types=1);
namespace App\Action;
class Functions
{
/**
* Выполняет сортировку массива по убыванию цены
* @param array $array
* @return array
*/
public function sortPrice(array $array): array
{
$prices = array_column($array, 'price');
$counts = array_column($array, 'count');
array_multisort(
$prices,
SORT_DESC,
$counts,
SORT_ASC, $array
);
return $array;
}
/**
* На выход должна вернуть отсортированный массив по ключу *price* DESC
* и во вторую очередь по *count* ASC:
* [['price'=>12, 'count'=>4], ['price'=>10, 'count'=>2], ['price'=>8, 'count'=>4],
* ['price'=>8, 'count'=>5], ['price'=>5, 'count'=>5],]
*/
}
\ No newline at end of file
......@@ -2,35 +2,31 @@
namespace App\Controller;
use App\Action\Functions;
use App\Validation\ArrayValidation;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
{
private function sortPrice(array $array): array
private Functions $functions;
public function __construct(Functions $functions)
{
$price = [];
$count = [];
foreach ($array as $key => $row) {
$price[$key] = $row['price'];
$count[$key] = $row['count'];
}
array_multisort($price, SORT_DESC, $count, SORT_ASC, $array);
return $array;
$this->functions = $functions;
}
#[Route('/', name: 'home')]
public function home(): Response
#[Route('/', name: 'home', methods: ['POST'])]
public function home(Request $request): Response
{
$array = array(
['price'=>10, 'count'=>2],
['price'=>5, 'count'=>5],
['price'=>8, 'count'=>5],
['price'=>12, 'count'=>4],
['price'=>8, 'count'=>4],
);
$array = $this->sortPrice($array);
$array = $request->get('arr');
$errors = ArrayValidation::validate($array);
if (count($errors) > 0) {
return new Response((string)$errors);
}
$array = $this->functions->sortPrice($array);
return $this->json($array);
}
}
<?php
namespace App\Validation;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintViolationListInterface;
class ArrayValidation
{
public static function validate(array $array): ConstraintViolationListInterface
{
$validator = Validation::createValidator();
$constraints = new Assert\Optional([
new Assert\Collection([
new Assert\Optional([
new Assert\Type('array'),
new Assert\Collection([
'price' => new Assert\Type('int'),
'count' => new Assert\Type('int')
])
])
])
]);
return $validator->validate($array, $constraints);
}
}
\ No newline at end of file
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