Skip to content

Commit

Permalink
fix: scalar issues during copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed Dec 20, 2024
1 parent 47b906a commit 748eb97
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
91 changes: 91 additions & 0 deletions src/Component/Akeneo/DataStructure/BuildProductMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Misery\Component\Akeneo\DataStructure;

use Misery\Component\Converter\Matcher;
use Misery\Model\DataStructure\ItemInterface;

class BuildProductMatcher
{
/**
* Convert multi-dimensional structure data to a Single Dimensional Structure with Matcher
*
* Product Payload
*
* {
* "values": [
* "color": [
* {
* "data": "red",
* "locale": null,
* "scope": null,
* }
* ]
* ]
* }
*
* Becomes
*
* {
* "values|color": {
* "data": "red",
* "locale": null,
* "scope": null,
* "matcher": {"sep":"|", "matches":["values", "color"], "scope":null, "locale":null}
* }
* }
* - Scope-able & localizable: "values|color|nl_BE|ecom"
* - Scope-able: "values|color|ecom"
* - localizable: "values|color|nl_BE"
**/
public static function fromApiPayload(array $productData, array $attributeData = []): array
{
throw new \Exception('Deprecated! use a proper Factory');
}

public static function fromItem(ItemInterface $item): array
{
$fields = [];
foreach ($item->getItemNodes() as $code => $itemNode) {
$matcher = $itemNode->getMatcher();

if ($matcher->matches('values')) {
$fields['values'][$matcher->getPrimaryKey()][] = $itemNode->getValue();
} else {
$fields[$code] = $itemNode->getValue();
}
}

return $fields;
}

public static function revertMatcherToProduct(array $productData): array
{
$fields = [];
foreach ($productData as $field => &$fieldValue) {
$matcher = $fieldValue['matcher'] ?? null;
if ($matcher instanceof Matcher) {
unset($fieldValue['matcher']);
$fields['values'][$matcher->getPrimaryKey()][] = $fieldValue;
} else {
$fields[$field] = $fieldValue;
}
}

return $fields;
}

public static function revertToKeyValue(array $productData): array
{
$fields = [];
foreach ($productData as $field => $fieldValue) {
$fields[$field] = $fieldValue;
$matcher = $fieldValue['matcher'] ?? null;
if ($matcher instanceof Matcher) {
$fields[$field] = $fieldValue['data'] ?? null;
}
}

return $fields;
}
}
2 changes: 1 addition & 1 deletion src/Model/DataStructure/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function copyItem(string $fromCode, string $toCode): void
}

// Property Values
if (is_string($itemValue)) {
if (!is_array($itemValue)) {
$this->addItem($toCode, $itemValue, $item->getContext());
return;
}
Expand Down

0 comments on commit 748eb97

Please sign in to comment.