|
| 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