Sulu\Bundle\MediaBundle\Collection\Manager\CollectionManager::getTreeById PHP Method

getTreeById() public method

public getTreeById ( $id, $locale )
    public function getTreeById($id, $locale)
    {
        $collectionSet = $this->collectionRepository->findTree($id, $locale);
        /** @var Collection[] $collections sorted by id */
        $collections = [];
        /** @var Collection[] $result collections without parent */
        $result = [];
        foreach ($collectionSet as $collection) {
            $apiEntity = new Collection($collection, $locale);
            $this->addPreview($apiEntity);
            $collections[$collection->getId()] = $apiEntity;
            if ($collection->getParent() !== null) {
                $collections[$collection->getParent()->getId()]->addChild($apiEntity);
            } else {
                $result[] = $apiEntity;
            }
        }
        return $result;
    }

Usage Example

Example #1
0
 public function testGetTreeById()
 {
     $collectionRepository = $this->prophesize(CollectionRepository::class);
     $mediaRepository = $this->prophesize(MediaRepository::class);
     $formatManager = $this->prophesize(FormatManagerInterface::class);
     $userRepository = $this->prophesize(UserRepositoryInterface::class);
     $entityManager = $this->prophesize(EntityManager::class);
     $collectionManager = new CollectionManager($collectionRepository->reveal(), $mediaRepository->reveal(), $formatManager->reveal(), $userRepository->reveal(), $entityManager->reveal(), null, '50x50', []);
     $entities = [$this->createEntity(1, 'de'), $this->createEntity(2, 'de', 1), $this->createEntity(3, 'de', 1), $this->createEntity(4, 'de', 3), $this->createEntity(5, 'de', 3), $this->createEntity(6, 'de')];
     $collectionRepository->findTree(5, 'de')->willReturn($entities);
     $result = $collectionManager->getTreeById(5, 'de');
     $this->assertCount(2, $result);
     $this->assertEquals(1, $result[0]->getId());
     $this->assertEquals(6, $result[1]->getId());
     $this->assertCount(0, $result[1]->getChildren());
     $result = $result[0]->getChildren();
     $this->assertCount(2, $result);
     $this->assertEquals(2, $result[0]->getId());
     $this->assertEquals(3, $result[1]->getId());
     $this->assertCount(0, $result[0]->getChildren());
     $result = $result[1]->getChildren();
     $this->assertCount(2, $result);
     $this->assertEquals(4, $result[0]->getId());
     $this->assertEquals(5, $result[1]->getId());
     $this->assertCount(0, $result[0]->getChildren());
     $this->assertCount(0, $result[1]->getChildren());
 }