Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Magento Improvements #99

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Component/Action/ActionItemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Misery\Component\Action;

use Misery\Model\DataStructure\ItemInterface;

interface ActionItemInterface extends ActionInterface
{
public function applyAsItem(ItemInterface $item): void;
}
27 changes: 23 additions & 4 deletions src/Component/Action/ConcatAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,40 @@
use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Common\Utils\ValueFormatter;
use Misery\Model\DataStructure\ItemInterface;

class ConcatAction implements OptionsInterface
class ConcatAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;
private $repo;
private $prepReader;

public const NAME = 'concat';

/** @var array */
private $options = [
'key' => null,
'field' => null,
'format' => '%s',
];

public function applyAsItem(ItemInterface $item): void
{
$format = $this->getOption('format');
$field = $this->getOption('field', $this->getOption('key'));
if (null == $field) {
return;
}

$dataValues = [];
foreach (ValueFormatter::getKeys($format) as $key) {
$dataValues[$key] = $item->getItem($key)?->getDataValue();
}

$item->addItem(
$field,
ValueFormatter::format($format, $dataValues)
);
}

public function apply(array $item): array
{
$field = $this->getOption('field', $this->getOption('key'));
Expand All @@ -28,7 +47,7 @@ public function apply(array $item): array
}

// don't check if array_key_exist here, concat should always work, if the field doesn't exist
// somewhat the point to form a new field from concatination
// somewhat the point to form a new field from concatenation
$item[$field] = ValueFormatter::format($this->getOption('format'), $item);

return $item;
Expand Down
15 changes: 14 additions & 1 deletion src/Component/Action/CopyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Converter\Matcher;
use Misery\Model\DataStructure\ItemInterface;

class CopyAction implements ActionInterface, OptionsInterface
class CopyAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;

Expand All @@ -18,6 +19,18 @@ class CopyAction implements ActionInterface, OptionsInterface
'to' => null,
];

public function applyAsItem(ItemInterface $item): void
{
$from = $this->getOption('from');
$to = $this->getOption('to');

if (null === $from || null === $to) {
return;
}

$item->copyItem($from, $to);
}

public function apply(array $item): array
{
$to = $this->getOption('to');
Expand Down
8 changes: 7 additions & 1 deletion src/Component/Action/DebugAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Model\DataStructure\ItemInterface;

class DebugAction implements OptionsInterface, ActionInterface
class DebugAction implements OptionsInterface, ActionInterface, ActionItemInterface
{
use OptionsTrait;

Expand All @@ -17,6 +18,11 @@ class DebugAction implements OptionsInterface, ActionInterface
'until_field' => null,
];

public function applyAsItem(ItemInterface $item): void
{
dd($item);
}

public function apply(array $item): array
{
$untilField = $this->getOption('until_field');
Expand Down
15 changes: 14 additions & 1 deletion src/Component/Action/ExpandAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Model\DataStructure\ItemInterface;

class ExpandAction implements OptionsInterface
class ExpandAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;

Expand All @@ -17,6 +18,18 @@ class ExpandAction implements OptionsInterface
'list' => null,
];

public function applyAsItem(ItemInterface $item): void
{
$list = $this->getOption('set', $this->getOption('list', []));
if (empty($list)) {
return;
}

foreach ($list as $itemCode => $itemValue) {
$item->addItem($itemCode, $itemValue);
}
}

public function apply(array $item): array
{
return array_replace_recursive($this->getOption('set', $this->getOption('list', [])), $item);
Expand Down
21 changes: 20 additions & 1 deletion src/Component/Action/ExtensionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use Misery\Component\Configurator\ConfigurationTrait;
use Misery\Component\Configurator\ReadOnlyConfiguration;
use Misery\Component\Extension\ExtensionInterface;
use Misery\Model\DataStructure\ItemInterface;

class ExtensionAction implements OptionsInterface, ConfigurationAwareInterface
class ExtensionAction implements OptionsInterface, ConfigurationAwareInterface, ActionItemInterface
{
use OptionsTrait;
use ConfigurationTrait;
Expand All @@ -22,6 +23,24 @@ class ExtensionAction implements OptionsInterface, ConfigurationAwareInterface
'extension' => null,
];

public function applyAsItem(ItemInterface $item): void
{
$extension = $this->getOption('extension');
if (null === $extension) {
return;
}

// loadExtension
if (null === $this->extension) {
$extensionFile = $this->configuration->getExtensions()[$extension.'.php'] ?? null;
$this->extension = $this->loadExtension($extensionFile, 'Extensions\\'.$extension);
}

if (method_exists($this->extension, 'applyAsItem')) {
$this->extension->applyAsItem($item);
}
}

public function apply($item): array
{
$extension = $this->getOption('extension');
Expand Down
83 changes: 83 additions & 0 deletions src/Component/Action/FirstValueAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Misery\Component\Action;

use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Converter\Matcher;
use Misery\Model\DataStructure\ItemInterface;

class FirstValueAction implements ActionInterface
{
use OptionsTrait;

private $mapper = null;

public const NAME = 'first_value';

/** @var array */
private $options = [
'fields' => [],
'store_field' => null,
'default_value' => null,
];

public function applyAsItem(ItemInterface $item): void
{
$defaultValue = $this->getOption('default_value');
$storeField = $this->getOption('store_field');
$fields = $this->getOption('fields');

foreach ($fields as $field) {
if (!empty($item->getItem($field)->getDataValue())) {
$item->copyItem($field, $storeField);
return;
}
}

$item->addItem($storeField, $defaultValue);
}

public function apply(array $item): array
{
$defaultValue = $this->getOption('default_value');
$storeField = $this->getOption('store_field');
$fields = $this->getOption('fields');
$matcher = Matcher::create('values|'.$storeField);

foreach ($fields as $field) {
$field = $this->findMatchedValueData($item, $field) ?? $field;

// COPY matcher if match is found
if (isset($item[$field]['matcher'])) {
$matcher = $item[$field]['matcher']->duplicateWithNewKey($storeField);
$item[$matcher->getMainKey()] = $item[$field];
$item[$matcher->getMainKey()]['matcher'] = $matcher;
return $item;
}
}

if (!isset($item[$matcher->getMainKey()])) {
$item[$matcher->getMainKey()] = [
'matcher' => $matcher,
'data' => $defaultValue,
'locale' => null,
'scope' => null,
];
}

return $item;
}

private function findMatchedValueData(array $item, string $field): int|string|null
{
foreach ($item as $key => $itemValue) {
$matcher = $itemValue['matcher'] ?? null;
/** @var $matcher Matcher */
if ($matcher && $matcher->matches($field)) {
return $key;
}
}

return null;
}
}
17 changes: 16 additions & 1 deletion src/Component/Action/FrameAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Model\DataStructure\ItemInterface;

class FrameAction implements OptionsInterface
class FrameAction implements OptionsInterface, ActionItemInterface
{
use OptionsTrait;

Expand All @@ -17,6 +18,20 @@ class FrameAction implements OptionsInterface
'list' => [],
];

public function applyAsItem(ItemInterface $item): void
{
$fields = $this->getOption('fields', $this->getOption('list'));
if (empty($fields)) {
return;
}

// lets generate a multi-dimensional array
if (isset($fields[0])) {
$fields = array_fill_keys($fields, null);
}
$item->reFrame($fields);
}

public function apply(array $item): array
{
$fields = $this->getOption('fields', $this->getOption('list'));
Expand Down
17 changes: 16 additions & 1 deletion src/Component/Action/GroupAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Configurator\ConfigurationAwareInterface;
use Misery\Component\Configurator\ConfigurationTrait;
use Misery\Model\DataStructure\ItemInterface;

class GroupAction implements OptionsInterface, ConfigurationAwareInterface
class GroupAction implements OptionsInterface, ConfigurationAwareInterface, ActionInterface
{
use OptionsTrait;
use ConfigurationTrait;
Expand All @@ -20,6 +21,20 @@ class GroupAction implements OptionsInterface, ConfigurationAwareInterface
'actionProcessor' => null,
];

public function applyAsItem(ItemInterface $item): void
{
if ($this->getOption('name') && !$this->getOption('actionProcessor')) {
$this->setOption(
'actionProcessor',
$this->getConfiguration()->getGroupedActions($this->getOption('name'))
);
}

if ($this->getOption('actionProcessor')) {
$this->getOption('actionProcessor')->process($item);
}
}

public function apply(array $item): array
{
if ($this->getOption('name') && !$this->getOption('actionProcessor')) {
Expand Down
15 changes: 8 additions & 7 deletions src/Component/Action/ItemActionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace Misery\Component\Action;

use Misery\Model\DataStructure\ItemInterface;

class ItemActionProcessor
{
private $configurationRules;

public function __construct(array $configurationRules)
{
$this->configurationRules = $configurationRules;
}
public function __construct(private readonly array $configurationRules) {}

public function process(array $item): array
public function process(ItemInterface|array $item): ItemInterface|array
{
foreach ($this->configurationRules as $name => $action) {
if ($item instanceof ItemInterface) {
$action->applyAsItem($item);
continue;
}
$item = $action->apply($item);
}

Expand Down
8 changes: 1 addition & 7 deletions src/Component/Action/ItemActionProcessorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,12 @@
use Misery\Component\Common\Registry\RegistryInterface;
use Misery\Component\Configurator\Configuration;
use Misery\Component\Configurator\ConfigurationAwareInterface;
use Misery\Component\Configurator\ConfigurationManager;
use Misery\Component\Reader\ItemReaderAwareInterface;
use Misery\Component\Source\SourceCollection;

class ItemActionProcessorFactory implements RegisteredByNameInterface
{
private $registry;

public function __construct(RegistryInterface $registry)
{
$this->registry = $registry;
}
public function __construct(private readonly RegistryInterface $registry) {}

public function createActionProcessor(SourceCollection $sources, array $configuration): ItemActionProcessor
{
Expand Down
Loading
Loading