Skip to content

Commit

Permalink
added: setAction support for std data fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Thijzer committed Sep 29, 2023
1 parent 05c1c10 commit 560d292
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
31 changes: 30 additions & 1 deletion src/Component/Action/SetValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Misery\Component\Common\Options\OptionsInterface;
use Misery\Component\Common\Options\OptionsTrait;
use Misery\Component\Converter\Matcher;

class SetValueAction implements ActionInterface, OptionsInterface
{
Expand All @@ -14,13 +15,41 @@ class SetValueAction implements ActionInterface, OptionsInterface
/** @var array */
private $options = [
'key' => null,
'field' => null,
'value' => null,
];

public function apply(array $item): array
{
$item[$this->options['key']] = $this->options['value'];
$field = $this->getOption('field', $this->getOption('key'));
$value = $this->getOption('value');

$key = $this->findMatchedValueData($item, $field);
if ($key) {
$item[$key]['data'] = $value;

return $item;
}

// don't allow array expansion when the field is not found
// use the expand function for this
if (key_exists($field, $item)) {
$item[$field] = $value;
}

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;
}
}
96 changes: 95 additions & 1 deletion tests/Component/Action/SetValueActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Misery\Component\Action;

use Misery\Component\Action\SetValueAction;
use Misery\Component\Converter\Matcher;
use PHPUnit\Framework\TestCase;

class SetValueActionTest extends TestCase
Expand Down Expand Up @@ -31,7 +32,7 @@ public function test_it_should_set_a_value_action(): void
], $format->apply($item));
}

public function test_it_should_set_a_new_value_action(): void
public function test_it_should_not_set_a_new_value_action(): void
{
$format = new SetValueAction();

Expand All @@ -50,7 +51,100 @@ public function test_it_should_set_a_new_value_action(): void
'brand' => 'louis',
'description' => 'LV',
'sku' => '1',
], $format->apply($item));
}

public function test_it_should_set_a_null_value_action(): void
{
$format = new SetValueAction();

$item = [
'brand' => 'louis',
'description' => 'LV',
'sku' => '1',
'published' => '1',
];

$format->setOptions([
'field' => 'published',
'value' => null,
]);

$this->assertEquals([
'brand' => 'louis',
'description' => 'LV',
'sku' => '1',
'published' => null,
], $format->apply($item));
}

public function test_it_should_set_fields_std_data()
{
$format = new SetValueAction();

$item = [
'values|street' => [
'matcher' => Matcher::create('street'),
'scope' => null,
'locale' => null,
'data' => '123 Main Street',
],
];

$format->setOptions([
'field' => 'street',
'value' => null
]);

$this->assertEquals([
'values|street' => [
'matcher' => Matcher::create('street'),
'scope' => null,
'locale' => null,
'data' => null,
],
], $format->apply($item));

$format->setOptions([
'field' => 'street',
'value' => 'unknown',
]);

$this->assertEquals([
'values|street' => [
'matcher' => Matcher::create('street'),
'scope' => null,
'locale' => null,
'data' => 'unknown',
],
], $format->apply($item));
}

public function test_it_should_not_set_fields_std_data()
{
$format = new SetValueAction();

$item = [
'values|street' => [
'matcher' => Matcher::create('street'),
'scope' => null,
'locale' => null,
'data' => '123 Main Street',
],
];

$format->setOptions([
'field' => 'zone',
'value' => 'urban'
]);

$this->assertEquals([
'values|street' => [
'matcher' => Matcher::create('street'),
'scope' => null,
'locale' => null,
'data' => '123 Main Street',
],
], $format->apply($item));
}
}

0 comments on commit 560d292

Please sign in to comment.