Skip to content

Commit 81e8331

Browse files
committed
Add meta as a promoted constructor parameter
1 parent 2bd3e95 commit 81e8331

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ In the following example, we emulate Bash-style jokers by mapping `?` to `.` and
9393

9494
```php
9595
$builder = new s9e\RegexpBuilder\Builder;
96-
$builder = new s9e\RegexpBuilder\Builder(
97-
meta: ['?' => '.', '*' => '.*']
98-
);
96+
$builder->meta->set('?', '.');
97+
$builder->meta->set('*', '.*');
98+
9999
echo '/', $builder->build(['foo?', 'bar*']), '/';
100100
```
101101
```
@@ -105,11 +105,23 @@ echo '/', $builder->build(['foo?', 'bar*']), '/';
105105
In the following example, we map `\d` (in the input) to `\d` (in the output) to emulate the escape sequence of a regular expression. Note that they do not have to be identical and we may choose to map `*` to `\d` or `\d` to `[0-9]` instead.
106106

107107
```php
108-
$builder = new s9e\RegexpBuilder\Builder(
109-
meta: ['\\d' => '\\d']
110-
);
108+
$builder = new s9e\RegexpBuilder\Builder;
109+
$builder->meta->set('\\d', '\\d');
110+
111111
echo '/', $builder->build(['a', 'b', '\\d']), '/';
112112
```
113113
```
114114
/[ab\d]/
115115
```
116+
117+
Alternatively, the `meta` property can also be set as a promoted constructor parameter as follows.
118+
```php
119+
$builder = new s9e\RegexpBuilder\Builder(
120+
meta: new s9e\RegexpBuilder\Meta(['?' => '.', '*' => '.*'])
121+
);
122+
123+
echo '/', $builder->build(['foo?', 'bar*']), '/';
124+
```
125+
```
126+
/bar.*|foo./
127+
```

src/Builder.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
class Builder
2424
{
25-
public readonly Meta $meta;
2625
public readonly Runner $runner;
2726
public readonly Serializer $serializer;
2827

@@ -34,10 +33,10 @@ class Builder
3433

3534
public function __construct(
3635
public readonly InputInterface $input = new BytesInput,
36+
public readonly Meta $meta = new Meta,
3737
public readonly OutputInterface $output = new BytesOutput
3838
)
3939
{
40-
$this->meta = new Meta;
4140
$this->serializer = new Serializer($this->meta, $this->output);
4241
$this->setRunner();
4342
}

0 commit comments

Comments
 (0)