Skip to content

Commit ada7463

Browse files
committed
Improved the config/validation config and fixed the lookup
1 parent 111f57a commit ada7463

File tree

8 files changed

+104
-28
lines changed

8 files changed

+104
-28
lines changed

src/Builder/AlternativeLookup.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,22 @@ public function getNode(): Node
7878
return (new IsolatedValueAppendingBuilder(
7979
new Node\Expr\Variable('input'),
8080
new Node\Expr\Variable('output'),
81-
array_filter([
81+
[
82+
$this->getLookupNode(),
83+
$this->merge?->getNode(),
84+
new Node\Stmt\Return_(
85+
new Node\Expr\Variable('output')
86+
),
87+
]
88+
))->getNode();
89+
}
90+
91+
public function getLookupNode() : Node
92+
{
93+
return (new IsolatedValueAppendingBuilder(
94+
new Node\Expr\Variable('input'),
95+
new Node\Expr\Variable('lookup'),
96+
[
8297
new Node\Stmt\TryCatch(
8398
stmts: [
8499
new Node\Stmt\Expression(
@@ -115,10 +130,10 @@ class: new Node\Name\FullyQualified('PDO'),
115130
),
116131
new Node\Stmt\Expression(
117132
expr: new Node\Expr\Assign(
118-
var: new Node\Expr\Variable('lookup'),
133+
var: new Node\Expr\Variable('data'),
119134
expr: new Node\Expr\MethodCall(
120135
var: new Node\Expr\Variable('stmt'),
121-
name: new Node\Name('fetchAll'),
136+
name: new Node\Name('fetch'),
122137
args: [
123138
new Node\Arg(
124139
new Node\Expr\ClassConstFetch(
@@ -180,11 +195,10 @@ class: new Node\Name\FullyQualified('PDO'),
180195
),
181196
],
182197
),
183-
$this->merge?->getNode(),
184198
new Node\Stmt\Return_(
185-
new Node\Expr\Variable('output')
186-
),
187-
])
199+
expr: new Node\Expr\Variable('data')
200+
)
201+
]
188202
))->getNode();
189203
}
190204

@@ -199,7 +213,7 @@ public function compileParams(): array
199213
name: new Node\Name('bindParam'),
200214
args: [
201215
new Node\Arg(
202-
is_string($key) ? new Node\Scalar\String_($key) : new Node\Scalar\LNumber($key)
216+
is_string($key) ? new Node\Scalar\Encapsed([new Node\Scalar\EncapsedStringPart(':'), new Node\Scalar\EncapsedStringPart($key)]) : new Node\Scalar\LNumber($key)
203217
),
204218
new Node\Arg(
205219
$param

src/Builder/ConditionalLookup.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private function getNodeAlternatives(): Node
6868
class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBucket'),
6969
args: [
7070
new Node\Arg(
71-
value: new Node\Expr\Variable('input')
71+
value: new Node\Expr\Variable('output')
7272
)
7373
],
7474
),
@@ -91,7 +91,7 @@ class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBuc
9191
fn (Node\Expr $condition, AlternativeLookup $lookup)
9292
=> new Node\Stmt\ElseIf_(
9393
cond: $condition,
94-
stmts: $this->compileAlternative($lookup)
94+
stmts: $this->compileAlternative($lookup),
9595
),
9696
array_column($alternatives, 0),
9797
array_column($alternatives, 1)
@@ -100,7 +100,14 @@ class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBuc
100100
stmts: [
101101
new Node\Stmt\Expression(
102102
new Node\Expr\Yield_(
103-
new Node\Expr\Variable('input')
103+
value: new Node\Expr\New_(
104+
class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBucket'),
105+
args: [
106+
new Node\Arg(
107+
value: new Node\Expr\Variable('output')
108+
),
109+
],
110+
),
104111
),
105112
),
106113
],

src/Builder/Loader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function compileParams(): array
221221
name: new Node\Name('bindParam'),
222222
args: [
223223
new Node\Arg(
224-
is_string($key) ? new Node\Scalar\String_($key) : new Node\Scalar\LNumber($key)
224+
is_string($key) ? new Node\Scalar\Encapsed([new Node\Scalar\EncapsedStringPart(':'), new Node\Scalar\EncapsedStringPart($key)]) : new Node\Scalar\LNumber($key)
225225
),
226226
new Node\Arg(
227227
$param

src/Builder/Lookup.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,21 @@ class: new Node\Stmt\Class_(
8787
expr: new Node\Expr\Yield_()
8888
)
8989
),
90-
...$this->compileAlternative($this->alternative),
91-
new Node\Stmt\Return_(
92-
expr: new Node\Expr\Variable('output')
90+
new Node\Stmt\Do_(
91+
cond: new Node\Expr\Assign(
92+
var: new Node\Expr\Variable('input'),
93+
expr: new Node\Expr\Yield_(
94+
value: new Node\Expr\New_(
95+
class: new Node\Name\FullyQualified('Kiboko\Component\Bucket\AcceptanceResultBucket'),
96+
args: [
97+
new Node\Arg(
98+
value: new Node\Expr\Variable('output')
99+
),
100+
],
101+
),
102+
),
103+
),
104+
stmts: $this->compileAlternative($this->alternative),
93105
),
94106
],
95107
],

src/Configuration/Extractor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public function getConfigTreeBuilder()
1717
->children()
1818
->scalarNode('query')
1919
->isRequired()
20+
->validate()
21+
->ifTrue(fn ($data) => is_string($data) && $data !== '' && (!str_starts_with(strtoupper($data), 'SELECT') && !str_starts_with(strtoupper($data), 'select')))
22+
->thenInvalid('Your query should be start with "SELECT".')
23+
->end()
2024
->end()
2125
->arrayNode('connection')
2226
->children()

src/Configuration/Loader.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,35 @@ public function getConfigTreeBuilder()
1515

1616
$builder->getRootNode()
1717
->children()
18-
->scalarNode('query')->end()
18+
->scalarNode('query')
19+
->isRequired()
20+
->validate()
21+
->ifTrue(fn ($data) => is_string($data) && $data !== '' && (!str_starts_with(strtoupper($data), 'INSERT') && !str_starts_with(strtoupper($data), 'UPDATE')))
22+
->thenInvalid('Your query must start with the keyword "INSERT" ou "UPDATE".')
23+
->end()
24+
->end()
1925
->arrayNode('params')
20-
->variablePrototype()
21-
->validate()
22-
->ifArray()
23-
->thenInvalid('A parameter cann\'t be an array.')
24-
->end()
26+
->beforeNormalization()
27+
->ifTrue(function ($data) {
28+
foreach ($data as $key => $value) {
29+
return !is_string($key) && !is_int($key);
30+
}
31+
32+
return false;
33+
})
34+
->thenInvalid('Your parameter key can only be a string or an integer.')
35+
->end()
36+
->beforeNormalization()
37+
->ifTrue(function ($data) {
38+
foreach ($data as $key => $value) {
39+
return is_string($key) && str_starts_with($key, ':');
40+
}
41+
42+
return false;
43+
})
44+
->thenInvalid('Your parameter can\'t start with ":".')
45+
->end()
46+
->scalarPrototype()
2547
->validate()
2648
->ifTrue(isExpression())
2749
->then(asExpression())

src/Configuration/Lookup.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@ public function getConfigTreeBuilder()
2525
->children()
2626
->scalarNode('query')
2727
->validate()
28-
->ifTrue(fn ($data) => is_string($data) && $data !== '' && (!str_starts_with($data, 'SELECT') && !str_starts_with($data, 'select')))
28+
->ifTrue(fn ($data) => is_string($data) && $data !== '' && (!str_starts_with(strtoupper($data), 'SELECT') && !str_starts_with(strtoupper($data), 'select')))
2929
->thenInvalid('Your query should be start with "SELECT".')
3030
->end()
3131
->end()
3232
->arrayNode('params')
33-
->variablePrototype()
34-
->validate()
35-
->ifArray()
36-
->thenInvalid('A parameter cann\'t be an array.')
37-
->end()
33+
->beforeNormalization()
34+
->ifTrue(function ($data) {
35+
foreach ($data as $key => $value) {
36+
return !is_string($key) && !is_int($key);
37+
}
38+
39+
return false;
40+
})
41+
->thenInvalid('Your parameter key can only be a string or an integer.')
42+
->end()
43+
->beforeNormalization()
44+
->ifTrue(function ($data) {
45+
foreach ($data as $key => $value) {
46+
return is_string($key) && str_starts_with($key, ':');
47+
}
48+
49+
return false;
50+
})
51+
->thenInvalid('Your parameter can\'t start with ":".')
52+
->end()
53+
->scalarPrototype()
3854
->validate()
3955
->ifTrue(isExpression())
4056
->then(asExpression())
@@ -91,6 +107,7 @@ private function getConditionalTreeBuilder(): TreeBuilder
91107
->end()
92108
->end()
93109
->scalarNode('query')
110+
->isRequired()
94111
->validate()
95112
->ifTrue(fn ($data) => is_string($data) && $data !== '' && (!str_starts_with($data, 'SELECT') && !str_starts_with($data, 'select')))
96113
->thenInvalid('Your query should be start with "SELECT".')

src/Service.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function compile(array $config): RepositoryInterface
7171
} elseif (array_key_exists('loader', $config)) {
7272
$loaderFactory = new Factory\Loader($this->interpreter);
7373

74-
return $loaderFactory->compile($config['lookup']);
74+
return $loaderFactory->compile($config['loader']);
7575
} else {
7676
throw new InvalidConfigurationException(
7777
'Could not determine if the factory should build an extractor or a loader.'

0 commit comments

Comments
 (0)