/** * Builds the list of the categories from the API response. * * @param array $response * @return CategoryInterface[] */ public function extractCategoriesFromApiResponse(array $response) { $categories = []; foreach ($response as $category) { if (!isset($category['id'])) { continue; } $cat = new Category(); $cat->setId($category['id']); $cat->setName($category['name']); if (isset($category['cats'])) { $subCategories = []; foreach ($category['cats'] as $subCategory) { $subCat = new Category(); $subCat->setId($subCategory['id']); $subCat->setName($subCategory['name']); $subCat->setParentCategory($cat); $subCategories[] = $subCat; } $cat->setSubCategories($subCategories); } $categories[] = $cat; } return $categories; }
public function testInstance() { $root = new Category(); $rootName = 'Video'; $root->setName($rootName); $rootId = 102; $root->setId($rootId); $subCategoryMovies = new Category(); $subCategoryMovies->setId(202); $subCategoryMovies->setName('Movies'); $subCategoryMovies->setParentCategory($root); $subCategoryCartoons = new Category(); $subCategoryCartoons->setId(203); $subCategoryCartoons->setName('Cartoons'); $subCategoryCartoons->setParentCategory($root); $root->setSubCategories([$subCategoryMovies, $subCategoryCartoons]); $categoryInterface = '\\Martial\\T411\\Api\\Category\\CategoryInterface'; $this->assertInstanceOf($categoryInterface, $root); $this->assertSame($rootId, $root->getId()); $this->assertSame($rootName, $root->getName()); $this->assertContainsOnly($categoryInterface, $root->getSubCategories()); $this->assertSame($root, $subCategoryCartoons->getParentCategory()); }