forked from Thijzer/csv-validation-header.wip
-
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.
- Loading branch information
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/Component/Akeneo/DataStructure/BuildProductMatcher.php
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,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; | ||
} | ||
} |
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