Skip to content

Commit ed679b6

Browse files
Merge pull request #167 from cleverage/166
#166 Add PregMatchTransformer
2 parents d68c45b + 90e2f7e commit ed679b6

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
- [ExplodeTransformer]
125125
- [HashTransformer]
126126
- [ImplodeTransformer](reference/transformers/implode_transformer.md)
127+
- [PregMatchTransformer](reference/transformers/preg_match_transformer.md)
127128
- [SlugifyTransformer](reference/transformers/slugify_transformer.md)
128129
- [SprintfTransformer]
129130
- [TrimTransformer](reference/transformers/trim_transformer.md)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
PregMatchTransformer
2+
=========================
3+
4+
Perform a regular expression match
5+
6+
This transformer uses the php internal function: https://www.php.net/manual/en/function.preg-match.php
7+
8+
Task reference
9+
--------------
10+
11+
* **Service**: `CleverAge\ProcessBundle\Transformer\String\PregMatchTransformer`
12+
* **Transformer code**: `preg_match`
13+
14+
Accepted inputs
15+
---------------
16+
17+
`string`
18+
19+
Possible outputs
20+
----------------
21+
22+
`array` or `null`
23+
24+
Options
25+
-------
26+
27+
| Code | Type | Required | Default | Description |
28+
|------------|-----------|:--------:|---------|------------------------------------------|
29+
| `pattern` | `string` | **X** | | |
30+
| `flags` | `int` | | 0 | |
31+
| `offset` | `int` | | 0 | |
32+
| `mode_all` | `boolean` | | false | Use preg_match_all instead of preg_match |
33+
34+
Examples
35+
--------
36+
37+
```yaml
38+
# Transformer options level
39+
entry:
40+
service: '@CleverAge\ProcessBundle\Task\ConstantIterableOutputTask'
41+
outputs: [ preg_match ]
42+
options:
43+
output: 'foobarbaz'
44+
preg_match:
45+
service: '@CleverAge\ProcessBundle\Task\TransformerTask'
46+
options:
47+
transformers:
48+
preg_match:
49+
pattern: '/(foo)(bar)(baz)/'
50+
flags: !php/const PREG_OFFSET_CAPTURE
51+
property_accessor:
52+
property_path: '[2]'
53+
```
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the CleverAge/ProcessBundle package.
7+
*
8+
* Copyright (c) Clever-Age
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace CleverAge\ProcessBundle\Transformer\String;
15+
16+
use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
19+
/**
20+
* Perform a regular expression match.
21+
*/
22+
class PregMatchTransformer implements ConfigurableTransformerInterface
23+
{
24+
public function transform(mixed $value, array $options = []): ?array
25+
{
26+
if (null === $value || '' === $value) {
27+
return null;
28+
}
29+
30+
if ($options['mode_all']) {
31+
preg_match_all($options['pattern'], (string) $value, $matches, $options['flags'], $options['offset']);
32+
} else {
33+
preg_match($options['pattern'], (string) $value, $matches, $options['flags'], $options['offset']);
34+
}
35+
36+
return $matches;
37+
}
38+
39+
/**
40+
* Returns the unique code to identify the transformer.
41+
*/
42+
public function getCode(): string
43+
{
44+
return 'preg_match';
45+
}
46+
47+
public function configureOptions(OptionsResolver $resolver): void
48+
{
49+
$resolver->setRequired(['pattern']);
50+
$resolver->setAllowedTypes('pattern', ['string']);
51+
$resolver->setDefault('flags', 0);
52+
$resolver->setAllowedTypes('flags', ['int']);
53+
$resolver->setDefault('offset', 0);
54+
$resolver->setAllowedTypes('offset', ['int']);
55+
$resolver->setDefault('mode_all', false);
56+
$resolver->setAllowedTypes('mode_all', ['boolean']);
57+
}
58+
}

0 commit comments

Comments
 (0)