Skip to content
Snippets Groups Projects
Commit 2de0012a authored by Nikita Chernykh's avatar Nikita Chernykh
Browse files

Merge branch 'PTPS_Controller_4' into 'main'

Ptps controller 4

See merge request !4
parents c9b88dcf d778f277
No related branches found
No related tags found
1 merge request!4Ptps controller 4
......@@ -44,4 +44,44 @@ class Functions
}
return null;
}
/**
* Удалить дубликаты, оставив только уникальные значения
* @param array $array
* @return array
*/
public function uniqElements(array $array): array
{
return array_unique($array, SORT_REGULAR);
}
/**
* Сгруппировать подразедлы в верхние разделы меню
* Дочерние элементы поместить в массив родителя с ключом submenu
* Значение под ключом depth определяет уровень раздела
* Массив $aMenu всегда начинается с элемента depth = 0,
* все последующие элементы с depth = 1 являются его дочерними
* элементами
* @param array $aMenu
* @return array
*/
public function prepareMenu(array $aMenu): array
{
$result = [];
foreach ($aMenu as $arr) {
if ($arr['depth'] == 0) {
$result[] = array(
'name' => $arr['name'],
'depth' => $arr['depth'],
'submenu' => []
);
continue;
}
$result[array_key_last($result)]['submenu'][] = array(
'name' => $arr['name'],
'depth' => $arr['depth'],
);
}
return $result;
}
}
\ No newline at end of file
......@@ -5,7 +5,9 @@ namespace App\Controller;
use App\Action\Functions;
use App\Requests\{
SortPriceRequest,
SearchRequest
SearchRequest,
UniqElementsRequest,
MenuRequest
};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
......@@ -37,4 +39,11 @@ class HomeController extends AbstractController
$result = $this->functions->uniqElements($request->getRequest()->toArray()['items']);
return $this->json($result);
}
#[Route('/prepareMenu', name: 'prepareMenu', methods: ['POST'])]
public function prepareMenu(MenuRequest $request): Response
{
$result = $this->functions->prepareMenu($request->getRequest()->toArray()['items']);
return $this->json($result);
}
}
<?php
namespace App\Requests;
use Symfony\Component\Validator\Constraints as Assert;
class MenuRequest extends BaseRequest
{
#[Assert\All([
new Assert\Collection([
'name' => new Assert\Type('string'),
'depth' => new Assert\Type('int'),
])
])]
public array $items;
}
\ No newline at end of file
{% block body %}
<h1>Сортировка массива:</h1>
<form method="POST">
{{ dump(array) }}
{% for key1, value1 in array %}
{% for key2, value2 in value1 %}
<input type="hidden" name="array[][]" value="{{ value2 }}">
<ul>
{% for value in menu %}
{% if value.depth == 0 %}
<h2>{{ value.name }}</h2>
{% for val in value.submenu %}
<li>{{ val.name }}</li>
{% endfor %}
{% endfor %}
<button type="submit">Отправить массив</button>
</form>
{% endif %}
{% endfor %}
</ul>
{% endblock %}
\ 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