Skip to content

Commit

Permalink
- added modifiedToArray() method
Browse files Browse the repository at this point in the history
- ListModifiedProperties() result will be sorted
- fromArray() will only mark data keys as modified
  • Loading branch information
oliwierptak committed Feb 22, 2022
1 parent 6ae9dd9 commit 368ac12
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/Popo/Plugin/ClassPlugin/FromArrayClassPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public function run(BuilderPluginInterface $builder): void
}
\$this->\$name = \$value;
\$this->updateMap[\$name] = true;
if (\array_key_exists(\$name, \$data)) {
\$this->updateMap[\$name] = true;
}
}
return \$this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class ListModifiedPropertiesClassPlugin implements ClassPluginInterface
public function run(BuilderPluginInterface $builder): void
{
$body = <<<EOF
return array_keys(\$this->updateMap);
\$sorted = \array_keys(\$this->updateMap);
sort(\$sorted, \SORT_STRING);
return \$sorted;
EOF;

$builder->getClass()
Expand Down
29 changes: 29 additions & 0 deletions src/Popo/Plugin/ClassPlugin/ModifiedToArrayClassPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types = 1);

namespace Popo\Plugin\ClassPlugin;

use Popo\Plugin\BuilderPluginInterface;
use Popo\Plugin\ClassPluginInterface;

class ModifiedToArrayClassPlugin implements ClassPluginInterface
{
public function run(BuilderPluginInterface $builder): void
{
$body = <<<EOF
\$data = \$this->toArray();
\$modifiedProperties = \$this->listModifiedProperties();
return \array_filter(\$data, function (\$key) use (\$modifiedProperties) {
return \in_array(\$key, \$modifiedProperties);
}, \ARRAY_FILTER_USE_KEY);
EOF;

$builder->getClass()
->addMethod('modifiedToArray')
->setPublic()
->setReturnType('array')
->setBody($body);
}
}
2 changes: 2 additions & 0 deletions src/Popo/PopoConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Popo\Plugin\ClassPlugin\IsNewClassPlugin;
use Popo\Plugin\ClassPlugin\ListModifiedPropertiesClassPlugin;
use Popo\Plugin\ClassPlugin\MetadataClassPlugin;
use Popo\Plugin\ClassPlugin\ModifiedToArrayClassPlugin;
use Popo\Plugin\ClassPlugin\PopoMethodClassPlugin;
use Popo\Plugin\ClassPlugin\RequireAllClassPlugin;
use Popo\Plugin\ClassPlugin\ToArrayClassPlugin;
Expand All @@ -37,6 +38,7 @@ class PopoConfigurator
*/
protected array $classPluginCollection = [
ToArrayClassPlugin::class,
ModifiedToArrayClassPlugin::class,
FromArrayClassPlugin::class,
IsNewClassPlugin::class,
ListModifiedPropertiesClassPlugin::class,
Expand Down
31 changes: 31 additions & 0 deletions tests/suite/App/PopoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ public function test_toArray(): void
);
}

public function test_modifiedArray(): void
{
$foo = (new Foo)->fromArray([
'title' => 'Aaaa bbb ccccc',
'isTest' => false,
'idFromExampleSchema' => 111,
]);

$this->assertEquals(
[
'title' => 'Aaaa bbb ccccc',
'isTest' => false,
'idFromExampleSchema' => 111,
],
$foo->modifiedToArray()
);
}

public function test_fromArray(): void
{
$foo = (new Foo);
Expand Down Expand Up @@ -214,6 +232,19 @@ public function test_modified_properties(): void
$this->assertEquals(['title'], $foo->listModifiedProperties());
}

public function test_modified_properties_from_array(): void
{
$foo = (new Foo)->fromArray(
[
'title' => 'Aaaa bbb ccccc',
'isTest' => false,
'idFromExampleSchema' => 111,
],
);

$this->assertEquals(['idFromExampleSchema', 'isTest', 'title'], $foo->listModifiedProperties());
}

public function test_default_bool_value(): void
{
$foo = (new Foo);
Expand Down

0 comments on commit 368ac12

Please sign in to comment.