iblocksForExport = $iblocksForExport; $this->catalogRepository = new CatalogRepository(); $this->xmlCategoryFactory = new XmlCategoryFactory(); $this->fileRepository = new FileRepository(SiteRepository::getDefaultServerName()); } /** * @return XmlCategory[] */ public function getXmlCategories(): array { $xmlCategories = []; foreach ($this->iblocksForExport as $iblockId) { $categories = $this->catalogRepository->getCategoriesByIblockId($iblockId); if ($categories === null) { $categoryId = self::MILLION + $iblockId; $xmlCategory = $this->makeXmlCategoryFromIblock($iblockId, $categoryId); if (!$xmlCategory) { continue; } $xmlCategories[$categoryId] = $xmlCategory; } $xmlCategories = array_merge($xmlCategories, $this->getXmlSubCategories($categories)); } return $xmlCategories; } /** * Возвращает коллекцию подкатегорий категории * * @param \Bitrix\Main\ORM\Objectify\Collection $categories * @return XmlCategory[] */ public function getXmlSubCategories(Collection $categories): array { $xmlCategories = []; foreach ($categories as $categoryKey => $category) { if (!$category instanceof EntityObject) { continue; } try { $xmlCategory = $this->xmlCategoryFactory->create( $category, $this->fileRepository->getImageUrl($category->get('PICTURE'), $category->get('IBLOCK_ID')) ); if (!$xmlCategory) { continue; } $xmlCategories[$categoryKey] = $this->xmlCategoryFactory->create( $category, $this->fileRepository->getImageUrl($category->get('PICTURE'), $category->get('IBLOCK_ID')) ); } catch (ArgumentException | SystemException $exception) { AddMessage2Log($exception->getMessage()); } } return $xmlCategories; } /** * Создает XmlCategory из инфоблока * * @param int $iblockId * @param int $categoryId * @return \Intaro\RetailCrm\Model\Bitrix\Xml\XmlCategory|null */ public function makeXmlCategoryFromIblock(int $iblockId, int $categoryId): ?XmlCategory { $iblock = $this->catalogRepository->getIblockById($iblockId); if ($iblock === null) { return null; } try { return $this->xmlCategoryFactory->create( $iblock, $this->fileRepository->getImageUrl($iblock->get('PICTURE'), $iblockId), $categoryId ); } catch (ArgumentException | SystemException $exception) { AddMessage2Log($exception->getMessage()); } } }