|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Catalog\Model\Product\Webapi; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 11 | +use Magento\Framework\Webapi\Request; |
| 12 | +use Magento\Framework\Webapi\Rest\Request\DeserializerInterface; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class for checking empty array and remove it from the output result |
| 16 | + */ |
| 17 | +class ProductOutputProcessor |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var Request |
| 21 | + */ |
| 22 | + private $request; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var DeserializerInterface |
| 26 | + */ |
| 27 | + private $deserializer; |
| 28 | + |
| 29 | + /** |
| 30 | + * @param Request $request |
| 31 | + * @param DeserializerInterface $deserializer |
| 32 | + */ |
| 33 | + public function __construct( |
| 34 | + Request $request, |
| 35 | + DeserializerInterface $deserializer |
| 36 | + ) { |
| 37 | + $this->request = $request; |
| 38 | + $this->deserializer = $deserializer; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Removing attribute from the result array if its null or empty |
| 43 | + * |
| 44 | + * @param ProductInterface $product |
| 45 | + * @param array $result |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + public function execute( |
| 49 | + ProductInterface $product, |
| 50 | + array $result |
| 51 | + ): array { |
| 52 | + $requestContent = $this->request->getContent() ?? []; |
| 53 | + if (empty($requestContent)) { |
| 54 | + return $result; |
| 55 | + } |
| 56 | + $requestContentDetails = (array)$this->deserializer->deserialize($requestContent); |
| 57 | + $requestProductList = $this->extractProductList($requestContentDetails); |
| 58 | + |
| 59 | + $requestProductList = array_filter( |
| 60 | + $requestProductList, |
| 61 | + function ($requestProduct) use ($product) { |
| 62 | + return isset($requestProduct['sku']) && $requestProduct['sku'] === $product->getSku(); |
| 63 | + } |
| 64 | + ); |
| 65 | + |
| 66 | + if (empty($requestProductList)) { |
| 67 | + return $result; |
| 68 | + } |
| 69 | + |
| 70 | + $requestProduct = current($requestProductList); |
| 71 | + |
| 72 | + if (empty($product->getTierPrices()) && !array_key_exists('tier_prices', $requestProduct)) { |
| 73 | + unset($result['tier_prices']); |
| 74 | + } |
| 75 | + |
| 76 | + if (empty($product->getProductLinks()) && !array_key_exists('product_links', $requestProduct)) { |
| 77 | + unset($result['product_links']); |
| 78 | + } |
| 79 | + |
| 80 | + return $result; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Extract product list from the request content details |
| 85 | + * |
| 86 | + * @param array $contentDetails |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + private function extractProductList(array $contentDetails): array |
| 90 | + { |
| 91 | + $productList = []; |
| 92 | + $arrayIterator = new \RecursiveArrayIterator($contentDetails); |
| 93 | + $iterator = new \RecursiveIteratorIterator($arrayIterator, \RecursiveIteratorIterator::SELF_FIRST); |
| 94 | + foreach ($iterator as $iteratorKey => $iteratorValue) { |
| 95 | + if ($iteratorKey === 'product') { |
| 96 | + array_push($productList, $iteratorValue); |
| 97 | + } |
| 98 | + } |
| 99 | + return $productList; |
| 100 | + } |
| 101 | +} |
0 commit comments