The CategoryManager is responsible for the centralized management of our categories.
public function testGetApiObjects() { $entities = [new CategoryEntity(), null, new CategoryEntity(), new CategoryEntity(), null]; $wrappers = $this->categoryManager->getApiObjects($entities, 'en'); $this->assertTrue($wrappers[0] instanceof CategoryWrapper); $this->assertTrue($wrappers[2] instanceof CategoryWrapper); $this->assertTrue($wrappers[3] instanceof CategoryWrapper); $this->assertEquals(null, $wrappers[1]); $this->assertEquals(null, $wrappers[4]); }
/** * Returns an array of serialized categories. * If parentKey is set, only the children of the category which is assigned to the given key are returned. * * @param string $locale * @param string $parentKey key of parent category * * @return 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); }); }
/** * {@inheritdoc} */ public function read(NodeInterface $node, PropertyInterface $property, $webspaceKey, $languageCode, $segmentKey) { $data = []; $categoryIds = $node->getPropertyValueWithDefault($property->getName(), []); $categories = $this->categoryManager->findByIds($categoryIds); $categories = $this->categoryManager->getApiObjects($categories, $languageCode); foreach ($categories as $category) { $data[] = $category->toArray(); } $this->setData($data, $property); }
/** * {@inheritdoc} */ public function getContentData(PropertyInterface $property) { $ids = $property->getValue(); if (!is_array($ids) || empty($ids)) { return []; } $data = []; $categoryEntities = $this->categoryManager->findByIds($ids); $categoryApiEntities = $this->categoryManager->getApiObjects($categoryEntities, $property->getStructure()->getLanguageCode()); foreach ($categoryApiEntities as $category) { $data[] = $category->toArray(); } return $data; }
public function testRead() { $categoryEntity1 = new CategoryEntity(); $categoryTranslation1 = new CategoryTranslationEntity(); $categoryTranslation1->setLocale('en'); $categoryTranslation1->setTranslation('Category 1'); $categoryEntity1->addTranslation($categoryTranslation1); $categoryEntity2 = new CategoryEntity(); $categoryTranslation2 = new CategoryTranslationEntity(); $categoryTranslation2->setLocale('en'); $categoryTranslation2->setTranslation('Category 2'); $categoryEntity2->addTranslation($categoryTranslation2); $category1 = new Category($categoryEntity1, 'en'); $category2 = new Category($categoryEntity2, 'en'); $this->categoryManager->expects($this->any())->method('findByIds')->will($this->returnValueMap([[[1, 2], [$categoryEntity1, $categoryEntity2]]])); $this->categoryManager->expects($this->any())->method('getApiObjects')->will($this->returnValueMap([[[$categoryEntity1, $categoryEntity2], 'en', [$category1, $category2]]])); $node = $this->getMockBuilder('PHPCR\\NodeInterface')->getMock(); $node->expects($this->any())->method('getPropertyValueWithDefault')->will($this->returnValueMap([['property', [], [1, 2]]])); $property = $this->getMockBuilder('Sulu\\Component\\Content\\Compat\\PropertyInterface')->getMock(); $property->expects($this->any())->method('getName')->willReturn('property'); $property->expects($this->any())->method('setValue')->with([$category1->toArray(), $category2->toArray()]); $this->categoryManager->expects($this->once())->method('findByIds'); $this->categoryList->read($node, $property, null, 'en', null); }