Skip to content

"append" (true|false) for map & object #25

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

Merged
merged 3 commits into from
Jun 14, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-5.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 5
on: push
jobs:
phpstan:
phpstan5:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-6.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 6
on: push
jobs:
phpstan:
phpstan6:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-7.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 7
on: push
jobs:
phpstan:
phpstan7:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan-8.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PHPStan level 8
on: push
jobs:
phpstan:
phpstan8:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
6 changes: 5 additions & 1 deletion src/Builder/ArrayMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kiboko\Plugin\FastMap\Builder;

use Kiboko\Component\FastMapConfig\ArrayBuilderInterface;
use Kiboko\Contract\Mapping\CompilableMapperInterface;
use PhpParser\Builder;
use PhpParser\Node;

Expand All @@ -16,6 +17,9 @@ public function __construct(private ArrayBuilderInterface $mapper)

public function getNode(): Node
{
/** @var CompilableMapperInterface $compilableMapper */
$compilableMapper = $this->mapper->getMapper();

return new Node\Expr\New_(
new Node\Stmt\Class_(
name: null,
Expand All @@ -29,7 +33,7 @@ public function getNode(): Node
subNodes: [
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
'stmts' => [
...$this->mapper->getMapper()->compile(new Node\Expr\Variable('output')),
...$compilableMapper->compile(new Node\Expr\Variable('output')),
new Node\Stmt\Return_(new Node\Expr\Variable('output')),
],
'params' => [
Expand Down
8 changes: 5 additions & 3 deletions src/Builder/ConditionalMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ private function compileConditions(array $alternatives): Node

/** @var Builder $builder */
[$condition, $builder] = array_shift($alternatives);
/** @var Node\Stmt\Expression $expression */
$expression = $parser->parse('<?php '.$this->interpreter->compile($condition, ['input', 'output']).';')[0];

return new Node\Expr\New_(
new Node\Stmt\Class_(
Expand Down Expand Up @@ -106,7 +108,7 @@ function ($alternative) {
],
'stmts' => [
new Node\Stmt\If_(
cond: $parser->parse('<?php '.$this->interpreter->compile($condition, ['input', 'output']).';')[0]->expr,
cond: $expression->expr,
subNodes: [
'stmts' => [
new Node\Stmt\Return_(
Expand All @@ -130,11 +132,11 @@ function ($alternative) {
),
],
'elseifs' => array_map(
function ($alternative, $index) use ($parser) {
function ($alternative, $index) use ($expression) {
[$condition, $repository] = $alternative;

return new Node\Stmt\ElseIf_(
cond: $parser->parse('<?php '.$this->interpreter->compile($condition, ['input', 'output']).';')[0]->expr,
cond: $expression->expr,
stmts: [
new Node\Stmt\Return_(
new Node\Expr\FuncCall(
Expand Down
6 changes: 5 additions & 1 deletion src/Builder/ObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kiboko\Plugin\FastMap\Builder;

use Kiboko\Component\FastMapConfig\ObjectBuilderInterface;
use Kiboko\Contract\Mapping\CompilableMapperInterface;
use PhpParser\Builder;
use PhpParser\Node;

Expand All @@ -16,6 +17,9 @@ public function __construct(private ObjectBuilderInterface $mapper)

public function getNode(): Node
{
/** @var CompilableMapperInterface $compilableMapper */
$compilableMapper = $this->mapper->getMapper();

return new Node\Expr\New_(
new Node\Stmt\Class_(
name: null,
Expand All @@ -29,7 +33,7 @@ public function getNode(): Node
subNodes: [
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
'stmts' => [
...$this->mapper->getMapper()->compile(new Node\Expr\Variable('output')),
...$compilableMapper->compile(new Node\Expr\Variable('output')),
new Node\Stmt\Return_(new Node\Expr\Variable('output')),
],
'params' => [
Expand Down
3 changes: 3 additions & 0 deletions src/Builder/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class: new Node\Name\FullyQualified(
new Node\Arg(
value: new Node\Expr\Variable('line')
),
new Node\Arg(
value: new Node\Expr\Variable('line')
),
]
),
),
Expand Down
16 changes: 7 additions & 9 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->children()
->scalarNode('class')->end()
->booleanNode('append')->end()
->scalarNode('expression')->end()
->arrayNode('expression_language')
->scalarPrototype()->end()
Expand All @@ -48,25 +49,22 @@ public function getConfigTreeBuilder(): TreeBuilder
->thenInvalid('Your configuration should be an array.')
->end()
->validate()
->always(mutuallyExclusiveFields('copy', 'constant', 'map', 'object', 'list', 'collection'))
->always(mutuallyExclusiveFields('append', 'list', 'collection', 'object'))
->end()
->validate()
->always(mutuallyExclusiveFields('expression', 'copy', 'constant', 'map'))
->always(mutuallyExclusiveFields('expression', 'map'))
->end()
->validate()
->always(mutuallyExclusiveFields('constant', 'copy', 'map', 'object', 'list', 'collection'))
->always(mutuallyExclusiveFields('map', 'object', 'list', 'collection'))
->end()
->validate()
->always(mutuallyExclusiveFields('map', 'copy', 'constant', 'object', 'list', 'collection'))
->always(mutuallyExclusiveFields('object', 'map', 'list', 'collection'))
->end()
->validate()
->always(mutuallyExclusiveFields('object', 'copy', 'constant', 'map', 'list', 'collection'))
->always(mutuallyExclusiveFields('list', 'map', 'object', 'collection'))
->end()
->validate()
->always(mutuallyExclusiveFields('list', 'copy', 'constant', 'map', 'object', 'collection'))
->end()
->validate()
->always(mutuallyExclusiveFields('collection', 'copy', 'constant', 'map', 'object', 'list'))
->always(mutuallyExclusiveFields('collection', 'map', 'object', 'list'))
->end()
->validate()
->always(mutuallyDependentFields('object', 'class', 'expression'))
Expand Down
15 changes: 11 additions & 4 deletions src/Factory/ArrayMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Kiboko\Plugin\FastMap\Factory;

use Kiboko\Component\FastMapConfig\ArrayAppendBuilder;
use Kiboko\Component\FastMapConfig\ArrayBuilder;
use Kiboko\Contract\Configurator;
use Kiboko\Plugin\FastMap;
Expand All @@ -17,7 +18,7 @@
private Processor $processor;
private ConfigurationInterface $configuration;

public function __construct(private ?ExpressionLanguage $interpreter, private array $additionalExpressionVariables = [])
public function __construct(private ?ExpressionLanguage $interpreter, private array $additionalExpressionVariables = [], private bool $append = false)
{
$this->processor = new Processor();
$this->configuration = new FastMap\Configuration\MapMapper();
Expand Down Expand Up @@ -54,9 +55,15 @@ public function validate(array $config): bool

public function compile(array $config): Repository\TransformerMapper
{
$mapper = new ArrayBuilder(
interpreter: $this->interpreter,
);
if ($this->append) {
$mapper = new ArrayAppendBuilder(
interpreter: $this->interpreter,
);
} else {
$mapper = new ArrayBuilder(
interpreter: $this->interpreter,
);
}

$builder = new FastMap\Builder\Transformer(
new FastMap\Builder\ArrayMapper($mapper)
Expand Down
2 changes: 1 addition & 1 deletion src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function compile(array $config): Factory\Repository\TransformerMapper
return $conditionalFactory->compile($config['conditional']);
}
if (\array_key_exists('map', $config)) {
$arrayFactory = new Factory\ArrayMapper($interpreter, $this->additionalExpressionVariables);
$arrayFactory = new Factory\ArrayMapper($interpreter, $this->additionalExpressionVariables, isset($config['append']) && $config['append']);

return $arrayFactory->compile($config['map']);
}
Expand Down