Skip to content

Commit

Permalink
Merge pull request #2 from snowio/feature/localised-attribute-options
Browse files Browse the repository at this point in the history
Localised attribute options
  • Loading branch information
Alexander Wanyoike authored Jun 19, 2020
2 parents 3fa0794 + e255b29 commit c51f3b3
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"require": {
"snowio/magento2-data-model": "^1.0",
"snowio/transform": "^1.0",
"snowio/akeneo3-data-model": "dev-master"
"snowio/akeneo3-data-model": "^0.2"
},
"require-dev": {
"phpunit/phpunit": "^7.3"
Expand Down
8 changes: 7 additions & 1 deletion src/AttributeOptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ public static function withDefaultLocale(string $defaultLocale): self

public function __invoke(Akeneo3AttributeOption $attributeOption): Magento2AttributeOption
{
return Magento2AttributeOption::of(
$magentoAttributeOption = Magento2AttributeOption::of(
$attributeOption->getAttributeCode(),
$this->obtainOptionCodeFromPrefixedOptionCode($attributeOption),
$attributeOption->getLabel($this->defaultLocale) ?? $attributeOption->getOptionCode()
);

if ($attributeOption->getSortOrder()) {
return $magentoAttributeOption->withSortOrder((int) $attributeOption->getSortOrder());
}

return $magentoAttributeOption;
}

private function obtainOptionCodeFromPrefixedOptionCode(Akeneo3AttributeOption $attributeOption)
Expand Down
55 changes: 55 additions & 0 deletions src/LocalisedAttributeOptionMapper.php
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);
}
}
41 changes: 41 additions & 0 deletions test/unit/LocalisedAttributeOptionMapperTest.php
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));
}
}

0 comments on commit c51f3b3

Please sign in to comment.