Skip to content
Snippets Groups Projects
Commit 0743d784 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 fd752d7e 1af58e1c
No related branches found
No related tags found
1 merge request!4Ptps|controller 4
<?php
namespace App\Actions;
class PrepareMenuAction
{
/**
* Группирует подразедлы в верхние разделы меню
* Дочерние элементы помещает в массив родителя с ключом submenu
* Значение под ключом depth определяет уровень раздела
* Массив $aMenu всегда должен начинается с элемента depth = 0,
* все последующие элементы с depth = 1 являются его дочерними элементами
*
* @param array $menu
* @return array
*/
public function act(array $menu): array
{
$returnableAMenu = [];
foreach ($menu as $menuPoint) {
$depth = $menuPoint["depth"];
if ($depth === 1) {
$tempArray = array_pop($returnableAMenu);
$tempArray['submenu'][] = $menuPoint;
$returnableAMenu[] = $tempArray;
} else {
$returnableAMenu[] = $menuPoint;
}
}
return $returnableAMenu;
}
}
<?php
namespace App\Controller;
use App\Actions\PrepareMenuAction;
use App\Requests\MenuRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class PrepareMenuController extends AbstractController
{
/**
* Контроллер реструктурирует массив меню добавляя вложенность по значению depth
* используюя prepareMenu функцию
* @param MenuRequest $request
* @param PrepareMenuAction $action
* @return Response
*/
#[Route('/prepare/menu', name: 'app_prepare_menu', methods: ['POST'])]
public function index(MenuRequest $request, PrepareMenuAction $action): Response
{
return new JsonResponse($action->act($request->serialise()));
}
}
<?php
namespace App\Requests;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;
class MenuRequest extends BaseRequest
{
#[Type('array')]
#[All(
constraints: new Collection(fields:[
'name' => [
new NotBlank(),
new Type('string'),
],
'depth' => [
new NotBlank(),
new Type('integer'),
]
])
)]
public $menu;
public function serialise(): mixed
{
return $this->menu;
}
}
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