-
Notifications
You must be signed in to change notification settings - Fork 0
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 #2 from snowio/feature/localised-attribute-options
Localised attribute options
- Loading branch information
Showing
4 changed files
with
104 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
declare(strict_types=1); | ||
namespace SnowIO\Akeneo3Magento2; | ||
|
||
use SnowIO\Akeneo3DataModel\LocalizedString; | ||
use SnowIO\Magento2DataModel\AttributeOption as Magento2AttributeOption; | ||
use SnowIO\Akeneo3DataModel\AttributeOption as Akeneo3AttributeOption; | ||
use SnowIO\Magento2DataModel\AttributeOptionStoreLabel; | ||
use SnowIO\Magento2DataModel\AttributeOptionStoreLabelSet; | ||
|
||
final class LocalisedAttributeOptionMapper extends DataMapper | ||
{ | ||
/** | ||
* @param string[] $localeStoreMap | ||
* @param string $defaultLocale | ||
* @return LocalisedAttributeOptionMapper | ||
*/ | ||
public static function withLocaleStoreMap(array $localeStoreMap, string $defaultLocale): self | ||
{ | ||
return new self($localeStoreMap, $defaultLocale); | ||
} | ||
|
||
public function __invoke(Akeneo3AttributeOption $attributeOption): Magento2AttributeOption | ||
{ | ||
$labels = $attributeOption->getLabels(); | ||
$storeLabels = AttributeOptionStoreLabelSet::of(array_map([$this, 'getStoreLabel'], iterator_to_array($labels))); | ||
/** @var Magento2AttributeOption $magentoAttributeOption */ | ||
$magentoAttributeOption = ($this->attributeOptionMapper)($attributeOption); | ||
|
||
return $magentoAttributeOption | ||
->withStoreLabels($storeLabels); | ||
} | ||
|
||
private function getStoreLabel(LocalizedString $label) | ||
{ | ||
/** @var string|null $storeCode */ | ||
$storeCode = $this->localeStoreMap[$label->getLocale()] ?? null; | ||
|
||
if (!$storeCode) { | ||
throw new \InvalidArgumentException("Unknown Store Code for locale '{$label->getLocale()}'"); | ||
} | ||
|
||
return AttributeOptionStoreLabel::of($storeCode, $label->getValue()); | ||
} | ||
|
||
|
||
private $localeStoreMap; | ||
private $attributeOptionMapper; | ||
|
||
private function __construct(array $localeStoreMap, string $defaultLocale) | ||
{ | ||
$this->localeStoreMap = $localeStoreMap; | ||
$this->attributeOptionMapper = AttributeOptionMapper::withDefaultLocale($defaultLocale); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace SnowIO\Akeneo3Magento2\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SnowIO\Akeneo3DataModel\AttributeOption as AkeneoAttributeOption; | ||
use SnowIO\Akeneo3DataModel\AttributeOptionIdentifier; | ||
use SnowIO\Akeneo3DataModel\InternationalizedString; | ||
use SnowIO\Akeneo3DataModel\LocalizedString; | ||
use SnowIO\Akeneo3Magento2\AttributeOptionMapper; | ||
use SnowIO\Akeneo3Magento2\LocalisedAttributeOptionMapper; | ||
use SnowIO\Magento2DataModel\AttributeOption as Magento2AttributeOption; | ||
use SnowIO\Magento2DataModel\AttributeOptionStoreLabel; | ||
use SnowIO\Magento2DataModel\AttributeOptionStoreLabelSet; | ||
|
||
class LocalisedAttributeOptionMapperTest extends TestCase | ||
{ | ||
|
||
public function testMapWithLocalStoreMap() | ||
{ | ||
$input = AkeneoAttributeOption::of(AttributeOptionIdentifier::of('size', 'size-large')) | ||
->withLabels(InternationalizedString::fromJson([ | ||
'en_GB' => "Large", "fr_FR" => "Grand", "de_DE" => "Gross" | ||
])); | ||
|
||
$mapper = LocalisedAttributeOptionMapper::withLocaleStoreMap([ | ||
"en_GB" => "en_gb", "fr_FR" => "fr_fr", "de_DE" => "de_de" | ||
], "en_GB"); | ||
|
||
$actual = $mapper($input); | ||
$expected = Magento2AttributeOption::of('size', 'large', 'Large') | ||
->withStoreLabels(AttributeOptionStoreLabelSet::of([ | ||
AttributeOptionStoreLabel::of("en_gb", "Large"), | ||
AttributeOptionStoreLabel::of("fr_fr", "Grand"), | ||
AttributeOptionStoreLabel::of("de_de", "Gross"), | ||
])); | ||
|
||
self::assertTrue($expected->equals($actual)); | ||
} | ||
} |