This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merge magento/2.3-develop into magento-tsg/2.3-develop-com-pr1
- Loading branch information
Showing
59 changed files
with
3,306 additions
and
382 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Model\Product; | ||
|
||
use Magento\Framework\EntityManager\HydratorInterface; | ||
|
||
/** | ||
* Class is used to extract data and populate entity with data | ||
*/ | ||
class Hydrator implements HydratorInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function extract($entity) | ||
{ | ||
return $entity->getData(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function hydrate($entity, array $data) | ||
{ | ||
$lockedAttributes = $entity->getLockedAttributes(); | ||
$entity->unlockAttributes(); | ||
$entity->setData(array_merge($entity->getData(), $data)); | ||
foreach ($lockedAttributes as $attribute) { | ||
$entity->lockAttribute($attribute); | ||
} | ||
|
||
return $entity; | ||
} | ||
} |
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
102 changes: 102 additions & 0 deletions
102
app/code/Magento/CatalogGraphQl/Model/Category/CategoryFilter.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,102 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Category; | ||
|
||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
use Magento\Catalog\Model\ResourceModel\Category\Collection; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Store\Api\Data\StoreInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Search\Model\Query; | ||
|
||
/** | ||
* Category filter allows to filter collection using 'id, url_key, name' from search criteria. | ||
*/ | ||
class CategoryFilter | ||
{ | ||
/** | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* @param ScopeConfigInterface $scopeConfig | ||
*/ | ||
public function __construct( | ||
ScopeConfigInterface $scopeConfig | ||
) { | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* Filter for filtering the requested categories id's based on url_key, ids, name in the result. | ||
* | ||
* @param array $args | ||
* @param Collection $categoryCollection | ||
* @param StoreInterface $store | ||
* @throws InputException | ||
*/ | ||
public function applyFilters(array $args, Collection $categoryCollection, StoreInterface $store) | ||
{ | ||
$categoryCollection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, ['eq' => 1]); | ||
foreach ($args['filters'] as $field => $cond) { | ||
foreach ($cond as $condType => $value) { | ||
if ($field === 'ids') { | ||
$categoryCollection->addIdFilter($value); | ||
} else { | ||
$this->addAttributeFilter($categoryCollection, $field, $condType, $value, $store); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add filter to category collection | ||
* | ||
* @param Collection $categoryCollection | ||
* @param string $field | ||
* @param string $condType | ||
* @param string|array $value | ||
* @param StoreInterface $store | ||
* @throws InputException | ||
*/ | ||
private function addAttributeFilter($categoryCollection, $field, $condType, $value, $store) | ||
{ | ||
if ($condType === 'match') { | ||
$this->addMatchFilter($categoryCollection, $field, $value, $store); | ||
return; | ||
} | ||
$categoryCollection->addAttributeToFilter($field, [$condType => $value]); | ||
} | ||
|
||
/** | ||
* Add match filter to collection | ||
* | ||
* @param Collection $categoryCollection | ||
* @param string $field | ||
* @param string $value | ||
* @param StoreInterface $store | ||
* @throws InputException | ||
*/ | ||
private function addMatchFilter($categoryCollection, $field, $value, $store) | ||
{ | ||
$minQueryLength = $this->scopeConfig->getValue( | ||
Query::XML_PATH_MIN_QUERY_LENGTH, | ||
ScopeInterface::SCOPE_STORE, | ||
$store | ||
); | ||
$searchValue = str_replace('%', '', $value); | ||
$matchLength = strlen($searchValue); | ||
if ($matchLength < $minQueryLength) { | ||
throw new InputException(__('Invalid match filter')); | ||
} | ||
|
||
$categoryCollection->addAttributeToFilter($field, ['like' => "%{$searchValue}%"]); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
app/code/Magento/CatalogGraphQl/Model/Resolver/CategoryList.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,114 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CatalogGraphQl\Model\Resolver; | ||
|
||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\ExtractDataFromCategoryTree; | ||
use Magento\Framework\Exception\InputException; | ||
use Magento\Framework\GraphQl\Config\Element\Field; | ||
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException; | ||
use Magento\Framework\GraphQl\Query\ResolverInterface; | ||
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; | ||
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\CategoryTree; | ||
use Magento\CatalogGraphQl\Model\Category\CategoryFilter; | ||
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; | ||
|
||
/** | ||
* Category List resolver, used for GraphQL category data request processing. | ||
*/ | ||
class CategoryList implements ResolverInterface | ||
{ | ||
/** | ||
* @var CategoryTree | ||
*/ | ||
private $categoryTree; | ||
|
||
/** | ||
* @var CollectionFactory | ||
*/ | ||
private $collectionFactory; | ||
|
||
/** | ||
* @var CategoryFilter | ||
*/ | ||
private $categoryFilter; | ||
|
||
/** | ||
* @var ExtractDataFromCategoryTree | ||
*/ | ||
private $extractDataFromCategoryTree; | ||
|
||
/** | ||
* @param CategoryTree $categoryTree | ||
* @param ExtractDataFromCategoryTree $extractDataFromCategoryTree | ||
* @param CategoryFilter $categoryFilter | ||
* @param CollectionFactory $collectionFactory | ||
*/ | ||
public function __construct( | ||
CategoryTree $categoryTree, | ||
ExtractDataFromCategoryTree $extractDataFromCategoryTree, | ||
CategoryFilter $categoryFilter, | ||
CollectionFactory $collectionFactory | ||
) { | ||
$this->categoryTree = $categoryTree; | ||
$this->extractDataFromCategoryTree = $extractDataFromCategoryTree; | ||
$this->categoryFilter = $categoryFilter; | ||
$this->collectionFactory = $collectionFactory; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) | ||
{ | ||
if (isset($value[$field->getName()])) { | ||
return $value[$field->getName()]; | ||
} | ||
$store = $context->getExtensionAttributes()->getStore(); | ||
|
||
$rootCategoryIds = []; | ||
if (!isset($args['filters'])) { | ||
$rootCategoryIds[] = (int)$store->getRootCategoryId(); | ||
} else { | ||
$categoryCollection = $this->collectionFactory->create(); | ||
try { | ||
$this->categoryFilter->applyFilters($args, $categoryCollection, $store); | ||
} catch (InputException $e) { | ||
return []; | ||
} | ||
|
||
foreach ($categoryCollection as $category) { | ||
$rootCategoryIds[] = (int)$category->getId(); | ||
} | ||
} | ||
|
||
$result = $this->fetchCategories($rootCategoryIds, $info); | ||
return $result; | ||
} | ||
|
||
/** | ||
* Fetch category tree data | ||
* | ||
* @param array $categoryIds | ||
* @param ResolveInfo $info | ||
* @return array | ||
* @throws GraphQlNoSuchEntityException | ||
*/ | ||
private function fetchCategories(array $categoryIds, ResolveInfo $info) | ||
{ | ||
$fetchedCategories = []; | ||
foreach ($categoryIds as $categoryId) { | ||
$categoryTree = $this->categoryTree->getTree($info, $categoryId); | ||
if (empty($categoryTree)) { | ||
continue; | ||
} | ||
$fetchedCategories[] = current($this->extractDataFromCategoryTree->execute($categoryTree)); | ||
} | ||
|
||
return $fetchedCategories; | ||
} | ||
} |
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
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
Oops, something went wrong.