Skip to content

Commit 475bc14

Browse files
authored
Merge pull request #25 from php-etl/feature/output-mode
"append" (true|false) for map & object
2 parents 6f7d5af + a5b0187 commit 475bc14

File tree

11 files changed

+41
-23
lines changed

11 files changed

+41
-23
lines changed

.github/workflows/phpstan-5.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: PHPStan level 5
22
on: push
33
jobs:
4-
phpstan:
4+
phpstan5:
55
runs-on: ubuntu-latest
66
steps:
77
- uses: actions/checkout@v3

.github/workflows/phpstan-6.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: PHPStan level 6
22
on: push
33
jobs:
4-
phpstan:
4+
phpstan6:
55
runs-on: ubuntu-latest
66
steps:
77
- uses: actions/checkout@v3

.github/workflows/phpstan-7.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: PHPStan level 7
22
on: push
33
jobs:
4-
phpstan:
4+
phpstan7:
55
runs-on: ubuntu-latest
66
steps:
77
- uses: actions/checkout@v3

.github/workflows/phpstan-8.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: PHPStan level 8
22
on: push
33
jobs:
4-
phpstan:
4+
phpstan8:
55
runs-on: ubuntu-latest
66
steps:
77
- uses: actions/checkout@v3

src/Builder/ArrayMapper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Plugin\FastMap\Builder;
66

77
use Kiboko\Component\FastMapConfig\ArrayBuilderInterface;
8+
use Kiboko\Contract\Mapping\CompilableMapperInterface;
89
use PhpParser\Builder;
910
use PhpParser\Node;
1011

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

1718
public function getNode(): Node
1819
{
20+
/** @var CompilableMapperInterface $compilableMapper */
21+
$compilableMapper = $this->mapper->getMapper();
22+
1923
return new Node\Expr\New_(
2024
new Node\Stmt\Class_(
2125
name: null,
@@ -29,7 +33,7 @@ public function getNode(): Node
2933
subNodes: [
3034
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
3135
'stmts' => [
32-
...$this->mapper->getMapper()->compile(new Node\Expr\Variable('output')),
36+
...$compilableMapper->compile(new Node\Expr\Variable('output')),
3337
new Node\Stmt\Return_(new Node\Expr\Variable('output')),
3438
],
3539
'params' => [

src/Builder/ConditionalMapper.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ private function compileConditions(array $alternatives): Node
3535

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

3941
return new Node\Expr\New_(
4042
new Node\Stmt\Class_(
@@ -106,7 +108,7 @@ function ($alternative) {
106108
],
107109
'stmts' => [
108110
new Node\Stmt\If_(
109-
cond: $parser->parse('<?php '.$this->interpreter->compile($condition, ['input', 'output']).';')[0]->expr,
111+
cond: $expression->expr,
110112
subNodes: [
111113
'stmts' => [
112114
new Node\Stmt\Return_(
@@ -130,11 +132,11 @@ function ($alternative) {
130132
),
131133
],
132134
'elseifs' => array_map(
133-
function ($alternative, $index) use ($parser) {
135+
function ($alternative, $index) use ($expression) {
134136
[$condition, $repository] = $alternative;
135137

136138
return new Node\Stmt\ElseIf_(
137-
cond: $parser->parse('<?php '.$this->interpreter->compile($condition, ['input', 'output']).';')[0]->expr,
139+
cond: $expression->expr,
138140
stmts: [
139141
new Node\Stmt\Return_(
140142
new Node\Expr\FuncCall(

src/Builder/ObjectMapper.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Kiboko\Plugin\FastMap\Builder;
66

77
use Kiboko\Component\FastMapConfig\ObjectBuilderInterface;
8+
use Kiboko\Contract\Mapping\CompilableMapperInterface;
89
use PhpParser\Builder;
910
use PhpParser\Node;
1011

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

1718
public function getNode(): Node
1819
{
20+
/** @var CompilableMapperInterface $compilableMapper */
21+
$compilableMapper = $this->mapper->getMapper();
22+
1923
return new Node\Expr\New_(
2024
new Node\Stmt\Class_(
2125
name: null,
@@ -29,7 +33,7 @@ public function getNode(): Node
2933
subNodes: [
3034
'flags' => Node\Stmt\Class_::MODIFIER_PUBLIC,
3135
'stmts' => [
32-
...$this->mapper->getMapper()->compile(new Node\Expr\Variable('output')),
36+
...$compilableMapper->compile(new Node\Expr\Variable('output')),
3337
new Node\Stmt\Return_(new Node\Expr\Variable('output')),
3438
],
3539
'params' => [

src/Builder/Transformer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class: new Node\Name\FullyQualified(
9797
new Node\Arg(
9898
value: new Node\Expr\Variable('line')
9999
),
100+
new Node\Arg(
101+
value: new Node\Expr\Variable('line')
102+
),
100103
]
101104
),
102105
),

src/Configuration.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function getConfigTreeBuilder(): TreeBuilder
3333
->end()
3434
->children()
3535
->scalarNode('class')->end()
36+
->booleanNode('append')->end()
3637
->scalarNode('expression')->end()
3738
->arrayNode('expression_language')
3839
->scalarPrototype()->end()
@@ -48,25 +49,22 @@ public function getConfigTreeBuilder(): TreeBuilder
4849
->thenInvalid('Your configuration should be an array.')
4950
->end()
5051
->validate()
51-
->always(mutuallyExclusiveFields('copy', 'constant', 'map', 'object', 'list', 'collection'))
52+
->always(mutuallyExclusiveFields('append', 'list', 'collection', 'object'))
5253
->end()
5354
->validate()
54-
->always(mutuallyExclusiveFields('expression', 'copy', 'constant', 'map'))
55+
->always(mutuallyExclusiveFields('expression', 'map'))
5556
->end()
5657
->validate()
57-
->always(mutuallyExclusiveFields('constant', 'copy', 'map', 'object', 'list', 'collection'))
58+
->always(mutuallyExclusiveFields('map', 'object', 'list', 'collection'))
5859
->end()
5960
->validate()
60-
->always(mutuallyExclusiveFields('map', 'copy', 'constant', 'object', 'list', 'collection'))
61+
->always(mutuallyExclusiveFields('object', 'map', 'list', 'collection'))
6162
->end()
6263
->validate()
63-
->always(mutuallyExclusiveFields('object', 'copy', 'constant', 'map', 'list', 'collection'))
64+
->always(mutuallyExclusiveFields('list', 'map', 'object', 'collection'))
6465
->end()
6566
->validate()
66-
->always(mutuallyExclusiveFields('list', 'copy', 'constant', 'map', 'object', 'collection'))
67-
->end()
68-
->validate()
69-
->always(mutuallyExclusiveFields('collection', 'copy', 'constant', 'map', 'object', 'list'))
67+
->always(mutuallyExclusiveFields('collection', 'map', 'object', 'list'))
7068
->end()
7169
->validate()
7270
->always(mutuallyDependentFields('object', 'class', 'expression'))

src/Factory/ArrayMapper.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Kiboko\Plugin\FastMap\Factory;
66

7+
use Kiboko\Component\FastMapConfig\ArrayAppendBuilder;
78
use Kiboko\Component\FastMapConfig\ArrayBuilder;
89
use Kiboko\Contract\Configurator;
910
use Kiboko\Plugin\FastMap;
@@ -17,7 +18,7 @@
1718
private Processor $processor;
1819
private ConfigurationInterface $configuration;
1920

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

5556
public function compile(array $config): Repository\TransformerMapper
5657
{
57-
$mapper = new ArrayBuilder(
58-
interpreter: $this->interpreter,
59-
);
58+
if ($this->append) {
59+
$mapper = new ArrayAppendBuilder(
60+
interpreter: $this->interpreter,
61+
);
62+
} else {
63+
$mapper = new ArrayBuilder(
64+
interpreter: $this->interpreter,
65+
);
66+
}
6067

6168
$builder = new FastMap\Builder\Transformer(
6269
new FastMap\Builder\ArrayMapper($mapper)

0 commit comments

Comments
 (0)