|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/* |
| 3 | + * This file is part of the CleverAge/ProcessBundle package. |
| 4 | + * |
| 5 | + * Copyright (C) 2017-2019 Clever-Age |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +namespace CleverAge\ProcessBundle\Transformer; |
| 12 | + |
| 13 | +use Symfony\Component\ExpressionLanguage\ExpressionLanguage; |
| 14 | +use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; |
| 15 | +use Symfony\Component\OptionsResolver\Options; |
| 16 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 17 | + |
| 18 | +/** |
| 19 | + * Parse an input using the Expression Language and returning a specific value upon a specific condition |
| 20 | + * |
| 21 | + * @author Vincent Chalnot <[email protected]> |
| 22 | + */ |
| 23 | +class ExpressionLanguageMapTransformer implements ConfigurableTransformerInterface |
| 24 | +{ |
| 25 | + /** @var ExpressionLanguage */ |
| 26 | + protected $language; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param ExpressionLanguage $language |
| 30 | + */ |
| 31 | + public function __construct(ExpressionLanguage $language) |
| 32 | + { |
| 33 | + $this->language = $language; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param OptionsResolver $resolver |
| 38 | + * |
| 39 | + * @throws ExceptionInterface |
| 40 | + */ |
| 41 | + public function configureOptions(OptionsResolver $resolver): void |
| 42 | + { |
| 43 | + $resolver->setRequired( |
| 44 | + [ |
| 45 | + 'map', |
| 46 | + ] |
| 47 | + ); |
| 48 | + $resolver->setAllowedTypes('map', ['array']); |
| 49 | + $resolver->setDefaults( |
| 50 | + [ |
| 51 | + 'ignore_missing' => false, |
| 52 | + 'keep_missing' => false, |
| 53 | + ] |
| 54 | + ); |
| 55 | + $resolver->setAllowedTypes('ignore_missing', ['boolean']); |
| 56 | + $resolver->setAllowedTypes('keep_missing', ['boolean']); |
| 57 | + $resolver->setNormalizer( |
| 58 | + 'map', |
| 59 | + function (Options $options, $values) { |
| 60 | + if (!is_array($values)) { |
| 61 | + throw new \UnexpectedValueException('The map must be an array'); |
| 62 | + } |
| 63 | + $resolver = new OptionsResolver(); |
| 64 | + $resolver->setRequired( |
| 65 | + [ |
| 66 | + 'condition', |
| 67 | + 'output', |
| 68 | + ] |
| 69 | + ); |
| 70 | + $resolver->setNormalizer( |
| 71 | + 'condition', |
| 72 | + function (Options $options, $value) { |
| 73 | + return $this->language->parse($value, ['data']); |
| 74 | + } |
| 75 | + ); |
| 76 | + $resolver->setNormalizer( |
| 77 | + 'output', |
| 78 | + function (Options $options, $value) { |
| 79 | + return $this->language->parse($value, ['data']); |
| 80 | + } |
| 81 | + ); |
| 82 | + $parsedValues = []; |
| 83 | + foreach ($values as $value) { |
| 84 | + $parsedValues[] = $resolver->resolve($value); |
| 85 | + } |
| 86 | + |
| 87 | + return $parsedValues; |
| 88 | + } |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Must return the transformed $value |
| 94 | + * |
| 95 | + * @param mixed $value |
| 96 | + * @param array $options |
| 97 | + * |
| 98 | + * @return mixed $value |
| 99 | + */ |
| 100 | + public function transform($value, array $options = []) |
| 101 | + { |
| 102 | + $input = ['data' => $value]; |
| 103 | + foreach ($options['map'] as $mapItem) { |
| 104 | + if ($this->language->evaluate($mapItem['condition'], $input)) { |
| 105 | + return $this->language->evaluate($mapItem['output'], $input); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if ($options['keep_missing']) { |
| 110 | + return $value; |
| 111 | + } |
| 112 | + if (!$options['ignore_missing']) { |
| 113 | + throw new \UnexpectedValueException("No expression accepting value '{$value}' in map"); |
| 114 | + } |
| 115 | + |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the unique code to identify the transformer |
| 121 | + * |
| 122 | + * @return string |
| 123 | + */ |
| 124 | + public function getCode(): string |
| 125 | + { |
| 126 | + return 'expression_language_map'; |
| 127 | + } |
| 128 | +} |
0 commit comments