-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GraphQL-123: Fix regexp when filter by root category id
-- fix coupling between object in CategoryTree
- Loading branch information
Valeriy Nayda
committed
Oct 1, 2018
1 parent
93c3ded
commit 4195613
Showing
3 changed files
with
70 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...gento/CatalogGraphQl/Model/Resolver/Products/DataProvider/ExtractDataFromCategoryTree.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider; | ||
|
||
use Magento\CatalogGraphQl\Model\Category\Hydrator; | ||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
|
||
/** | ||
* Extarct data from category tree | ||
*/ | ||
class ExtractDataFromCategoryTree | ||
{ | ||
/** | ||
* @var Hydrator | ||
*/ | ||
private $categoryHydrator; | ||
|
||
/** | ||
* @param Hydrator $categoryHydrator | ||
*/ | ||
public function __construct( | ||
Hydrator $categoryHydrator | ||
) { | ||
$this->categoryHydrator = $categoryHydrator; | ||
} | ||
|
||
/** | ||
* @param \Iterator $iterator | ||
* @return array | ||
*/ | ||
public function execute(\Iterator $iterator): array | ||
{ | ||
$tree = []; | ||
while ($iterator->valid()) { | ||
/** @var CategoryInterface $category */ | ||
$category = $iterator->current(); | ||
$iterator->next(); | ||
$nextCategory = $iterator->current(); | ||
$tree[$category->getId()] = $this->categoryHydrator->hydrateCategory($category); | ||
if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) { | ||
$tree[$category->getId()]['children'] = $this->execute($iterator); | ||
} | ||
} | ||
|
||
return $tree; | ||
} | ||
} |