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

refactoring

parent c83d1ae6
No related branches found
No related tags found
1 merge request!3Ptps controller 3
......@@ -23,3 +23,4 @@
/public/assets/
/assets/vendor/
###< symfony/asset-mapper ###
/.idea
\ No newline at end of file
Q
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
\ No newline at end of file
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
:
.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302
H
src/Action/Functions.php,4\3\43dcb35f966f0fa054ba7993783bf64ca2be218c
R
"src/Validation/ArrayValidation.php,6\0\6037d4b4b463114752d4470f297faf20f6eb091f
\ No newline at end of file
Q
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
\ No newline at end of file
!src/Controller/HomeController.php,a\d\ad8b439416d1e02614f47c5b471c7c4e587dca82
:
.gitignore,a\5\a5cc2925ca8258af241be7e5b0381edf30266302
H
src/Action/Functions.php,4\3\43dcb35f966f0fa054ba7993783bf64ca2be218c
R
"src/Validation/ArrayValidation.php,6\0\6037d4b4b463114752d4470f297faf20f6eb091f
\ 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],]
*/
/**
* Найдет элемент с указаным id
* @param array $array - массив, содержащий элементы со структурой
* [
* 'id' => 30,
* 'name' => 'Jhon',
* 'age' => 23,
* ]
* @param $id - ид искомого элемента
* @return ?array - найденный элемент
*/
public function search(array $array, int $id): ?array
{
$rowId = array_search($id, array_column($array, 'id'), false);
if ($rowId !== false) {
return $array[$rowId];
}
return null;
}
/**
* Удалить дубликаты, оставив только уникальные значения
* @param array $array
* @return array
*/
public function uniqElements(array $array): array
{
return array_unique($array, SORT_REGULAR);
}
/**
* Выходной массив:
* Array (
* [0] => Array([0] => laravel, [1] => php)
* [1] => Array([0] => codeigniter, [1] => php)
* [3] => Array([0] => c++, [1] => java))
* )
*/
}
......@@ -2,25 +2,31 @@
namespace App\Controller;
use App\Action\Functions;
use App\Validation\ArrayValidation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class HomeController extends AbstractController
{
private function uniqElements(array $array): array {
return array_unique($array, SORT_REGULAR);
private Functions $functions;
public function __construct(Functions $functions)
{
$this->functions = $functions;
}
#[Route('/', name: 'home')]
public function home(): Response
#[Route('/', name: 'home', methods: ['POST'])]
public function home(Request $request): Response
{
$arr = [
['laravel', 'php'],
['codeigniter', 'php'],
['laravel', 'php'],
['c++', 'java'],
];
return $this->json($this->uniqElements($arr));
$array = $request->get('arr');
$errors = ArrayValidation::validate($array);
if (count($errors) > 0) {
return new Response((string)$errors);
}
$result = $this->functions->uniqElements($array);
return $this->json($result);
}
}
<?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([
new Assert\Type('string'),
new Assert\Type('string'),
])
])
])
]);
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