Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • a.shamavov/iqdevtranningprogram
1 result
Show changes
Commits on Source (2)
......@@ -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,30 @@
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('/func1', name: 'home', methods: ['POST'])]
public function func1(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');
if (!ArrayValidation::validateFunc1($array)) {
return new Response("Invalid array");
}
$array = $this->functions->sortPrice($array);
return $this->json($array);
}
}
<?php
namespace App\Validation;
class ArrayValidation
{
public static function validateFunc1(array $array): bool
{
$prices = array_column($array, 'price');
$counts = array_column($array, 'count');
return ctype_digit(implode('',$prices)) && ctype_digit(implode('', $counts));
}
}
\ No newline at end of file