init
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Catalog\ViewModel\LayeredNavigation;
|
||||
|
||||
use Magento\Framework\View\Element\Block\ArgumentInterface;
|
||||
use Magento\Swatches\Helper\Data as SwatchHelper;
|
||||
use Magento\Swatches\Helper\Media as SwatchMediaHelper;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
|
||||
class AppliedFilterSwatch implements ArgumentInterface
|
||||
{
|
||||
/**
|
||||
* @var array<int, array{color: string, image: string}>
|
||||
*/
|
||||
private array $swatches = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly SwatchHelper $swatchHelper,
|
||||
private readonly SwatchMediaHelper $swatchMediaHelper
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{color: string, image: string}
|
||||
*/
|
||||
public function getSwatch(int $optionId): array
|
||||
{
|
||||
if ($optionId <= 0) {
|
||||
return ['color' => '', 'image' => ''];
|
||||
}
|
||||
|
||||
if (isset($this->swatches[$optionId])) {
|
||||
return $this->swatches[$optionId];
|
||||
}
|
||||
|
||||
$swatch = $this->swatchHelper->getSwatchesByOptionsId([$optionId])[$optionId] ?? [];
|
||||
$type = (int) ($swatch['type'] ?? Swatch::SWATCH_TYPE_EMPTY);
|
||||
$value = trim((string) ($swatch['value'] ?? ''));
|
||||
|
||||
if ($type === Swatch::SWATCH_TYPE_VISUAL_COLOR
|
||||
&& preg_match('/^#[0-9a-f]{3,8}$/i', $value)
|
||||
) {
|
||||
return $this->swatches[$optionId] = ['color' => $value, 'image' => ''];
|
||||
}
|
||||
|
||||
if ($type === Swatch::SWATCH_TYPE_VISUAL_IMAGE && $value !== '') {
|
||||
return $this->swatches[$optionId] = [
|
||||
'color' => '',
|
||||
'image' => $this->swatchMediaHelper->getSwatchAttributeImage(
|
||||
Swatch::SWATCH_IMAGE_NAME,
|
||||
$value
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return $this->swatches[$optionId] = ['color' => '', 'image' => ''];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user