Sulu\Bundle\CategoryBundle\Twig\CategoryTwigExtension::getCategoriesFunction PHP Метод

getCategoriesFunction() публичный Метод

If parentKey is set, only the children of the category which is assigned to the given key are returned.
public getCategoriesFunction ( string $locale, string $parentKey = null ) : array
$locale string
$parentKey string key of parent category
Результат array
    public function getCategoriesFunction($locale, $parentKey = null)
    {
        return $this->memoizeCache->memoize(function ($locale, $parentKey = null) {
            $entities = $this->categoryManager->findChildrenByParentKey($parentKey);
            $categories = $this->categoryManager->getApiObjects($entities, $locale);
            $context = SerializationContext::create();
            $context->setSerializeNull(true);
            return $this->serializer->serialize($categories, 'array', $context);
        });
    }

Usage Example

Пример #1
0
 /**
  * @dataProvider getProvider
  */
 public function testGet($categoryData, $locale = 'en', $parent = null, $depth = null)
 {
     $categoryEntities = [];
     $categoryApis = [];
     foreach ($categoryData as $category) {
         $categoryEntities[] = $this->createCategoryEntity($category);
         $categoryApis[] = $this->createCategoryApi($category);
     }
     $manager = $this->prophesize(CategoryManagerInterface::class);
     if (null === $parent) {
         $manager->find(null, null)->shouldBeCalled()->willReturn($categoryEntities);
     } else {
         $manager->findChildren($parent)->shouldBeCalled()->willReturn($categoryEntities);
     }
     $manager->getApiObjects($categoryEntities, $locale)->shouldBeCalled()->willReturn($categoryApis);
     $serializer = $this->prophesize(SerializerInterface::class);
     $serializer->serialize($categoryApis, 'array', Argument::type(SerializationContext::class))->shouldBeCalled()->willReturn($categoryData);
     $requestHandler = $this->prophesize(CategoryRequestHandlerInterface::class);
     $extension = new CategoryTwigExtension($manager->reveal(), $requestHandler->reveal(), $serializer->reveal(), $this->getMemoizeCache());
     $this->assertEquals($categoryData, $extension->getCategoriesFunction($locale, $parent, $depth));
 }