-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathPregMatchTransformer.php
More file actions
58 lines (49 loc) · 1.66 KB
/
PregMatchTransformer.php
File metadata and controls
58 lines (49 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
declare(strict_types=1);
/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CleverAge\ProcessBundle\Transformer\String;
use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Perform a regular expression match.
*/
class PregMatchTransformer implements ConfigurableTransformerInterface
{
public function transform(mixed $value, array $options = []): ?array
{
if (null === $value || '' === $value) {
return null;
}
if ($options['mode_all']) {
preg_match_all($options['pattern'], (string) $value, $matches, $options['flags'], $options['offset']);
} else {
preg_match($options['pattern'], (string) $value, $matches, $options['flags'], $options['offset']);
}
return $matches;
}
/**
* Returns the unique code to identify the transformer.
*/
public function getCode(): string
{
return 'preg_match';
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired(['pattern']);
$resolver->setAllowedTypes('pattern', ['string']);
$resolver->setDefault('flags', 0);
$resolver->setAllowedTypes('flags', ['int']);
$resolver->setDefault('offset', 0);
$resolver->setAllowedTypes('offset', ['int']);
$resolver->setDefault('mode_all', false);
$resolver->setAllowedTypes('mode_all', ['boolean']);
}
}