|
| 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\SwatchesGraphQl\Model\Resolver\Product\Options\DataProvider; |
| 9 | + |
| 10 | +use Magento\Swatches\Helper\Data as SwatchData; |
| 11 | +use Magento\Swatches\Helper\Media as SwatchesMedia; |
| 12 | +use Magento\Swatches\Model\Swatch; |
| 13 | + |
| 14 | +/** |
| 15 | + * Data provider for options swatches. |
| 16 | + */ |
| 17 | +class SwatchDataProvider |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var SwatchData |
| 21 | + */ |
| 22 | + private $swatchHelper; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var SwatchesMedia |
| 26 | + */ |
| 27 | + private $swatchMediaHelper; |
| 28 | + |
| 29 | + /** |
| 30 | + * SwatchDataProvider constructor. |
| 31 | + * |
| 32 | + * @param SwatchData $swatchHelper |
| 33 | + * @param SwatchesMedia $swatchMediaHelper |
| 34 | + */ |
| 35 | + public function __construct( |
| 36 | + SwatchData $swatchHelper, |
| 37 | + SwatchesMedia $swatchMediaHelper |
| 38 | + ) { |
| 39 | + $this->swatchHelper = $swatchHelper; |
| 40 | + $this->swatchMediaHelper = $swatchMediaHelper; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns swatch data by option ID. |
| 45 | + * |
| 46 | + * @param string $optionId |
| 47 | + * @return array|null |
| 48 | + */ |
| 49 | + public function getData(string $optionId): ?array |
| 50 | + { |
| 51 | + $swatches = $this->swatchHelper->getSwatchesByOptionsId([$optionId]); |
| 52 | + if (!isset($swatches[$optionId]['type'], $swatches[$optionId]['value'])) { |
| 53 | + return null; |
| 54 | + } |
| 55 | + $type = (int)$swatches[$optionId]['type']; |
| 56 | + $value = $swatches[$optionId]['value']; |
| 57 | + $data = ['value' => $value, 'type' => $type]; |
| 58 | + if ($type === Swatch::SWATCH_TYPE_VISUAL_IMAGE) { |
| 59 | + $data['thumbnail'] = $this->swatchMediaHelper->getSwatchAttributeImage( |
| 60 | + Swatch::SWATCH_THUMBNAIL_NAME, |
| 61 | + $value |
| 62 | + ); |
| 63 | + } |
| 64 | + return $data; |
| 65 | + } |
| 66 | +} |
0 commit comments