-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Setono/options
Options
- Loading branch information
Showing
8 changed files
with
125 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\DataMapper\Product; | ||
|
||
use Setono\SyliusMeilisearchPlugin\DataMapper\DataMapperInterface; | ||
use Setono\SyliusMeilisearchPlugin\Document\Document; | ||
use Setono\SyliusMeilisearchPlugin\Document\Product as ProductDocument; | ||
use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface; | ||
use Setono\SyliusMeilisearchPlugin\Provider\IndexScope\IndexScope; | ||
use Sylius\Component\Core\Model\ProductInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
final class OptionsDataMapper implements DataMapperInterface | ||
{ | ||
public function map(IndexableInterface $source, Document $target, IndexScope $indexScope, array $context = []): void | ||
{ | ||
Assert::true($this->supports($source, $target, $indexScope, $context)); | ||
|
||
foreach ($source->getEnabledVariants() as $variant) { | ||
foreach ($variant->getOptionValues() as $optionValue) { | ||
$option = $optionValue->getOption()?->getCode(); | ||
if ($option === null) { | ||
continue; | ||
} | ||
|
||
$target->options[$option][] = (string) $optionValue->getValue(); | ||
} | ||
} | ||
|
||
foreach ($target->options as $option => $values) { | ||
$target->options[$option] = array_values(array_unique($values)); | ||
} | ||
} | ||
|
||
/** | ||
* @psalm-assert-if-true ProductInterface $source | ||
* @psalm-assert-if-true ProductDocument $target | ||
*/ | ||
public function supports(IndexableInterface $source, Document $target, IndexScope $indexScope, array $context = []): bool | ||
{ | ||
return $source instanceof ProductInterface && $target instanceof ProductDocument; | ||
} | ||
} |
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusMeilisearchPlugin\Normalizer; | ||
|
||
use Setono\SyliusMeilisearchPlugin\Document\Product; | ||
use Symfony\Component\Serializer\Exception\LogicException; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
final class ProductNormalizer implements NormalizerInterface | ||
{ | ||
public function __construct(private readonly NormalizerInterface $normalizer) | ||
{ | ||
} | ||
|
||
public function normalize(mixed $object, ?string $format = null, array $context = []): array | ||
{ | ||
if (!$this->supportsNormalization($object)) { | ||
throw new LogicException(sprintf('The object must be an instance of %s', Product::class)); | ||
} | ||
|
||
$data = $this->normalizer->normalize($object, $format, $context); | ||
if ($data instanceof \ArrayObject) { | ||
$data = $data->getArrayCopy(); | ||
} | ||
|
||
if (!is_array($data)) { | ||
throw new LogicException('The normalized product data must be an array or an ArrayObject'); | ||
} | ||
|
||
/** | ||
* @var string $option | ||
* @var list<string> $values | ||
*/ | ||
foreach ($data['options'] as $option => $values) { | ||
$data[$option . '_option'] = $values; | ||
} | ||
|
||
unset($data['options']); | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* @psalm-assert-if-true Product $data | ||
*/ | ||
public function supportsNormalization(mixed $data, ?string $format = null): bool | ||
{ | ||
return $data instanceof Product; | ||
} | ||
|
||
public function getSupportedTypes(?string $format): array | ||
{ | ||
return [ | ||
Product::class => true, | ||
]; | ||
} | ||
} |
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,11 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="Setono\SyliusMeilisearchPlugin\Normalizer\ProductNormalizer"> | ||
<argument type="service" id="serializer.normalizer.object"/> | ||
|
||
<tag name="serializer.normalizer"/> | ||
</service> | ||
</services> | ||
</container> |