Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Latest
* [#505](https://github.com/cleverage/process-bundle/issues/505) Consistent names for the equivalent options of the tasks: `filename` is renamed `file_path` (`file_reader`, `file_writer`, `line_reader`) and `base_folder_path` is renamed `base_path` (`input_folder_browser`), without deprecation in 5.x; `input_folder_browser` adds a single `/` between a non-empty `base_path` and the input, as `input_csv_reader` does (see `UPGRADE.md`). Update documentation and tests.
* [#380](https://github.com/cleverage/process-bundle/issues/380) Executing a process while it is already running, by itself or through its sub-processes (`A → B → A` with `process_executor`), throws a `RecursiveProcessExecutionException` naming the chain of processes, before any event is dispatched for the rejected execution (deprecated since 5.6, it reset the states of the running process). The `error_strategy` of the `process_executor` task applies (see `UPGRADE.md`). Update documentation and tests.
* [#404](https://github.com/cleverage/process-bundle/issues/404) `slugify`: the default `transliterator` is `Any-Latin; Latin-ASCII` (instead of `NFD; [:Nonspacing Mark:] Remove; NFC`), which transliterates the letters that are not decomposed (`Cœur` gives `coeur` instead of `c_ur`) and the other scripts; `strip_tags` is `false` by default (a `<` truncated the value). The slugs of existing data change, set both options to keep the 5.x slugs (see `UPGRADE.md`). Update documentation and tests.
* [#405](https://github.com/cleverage/process-bundle/issues/405) Transformers: one policy for the inputs they do not expect. A string transformer (`convert_value` with `auto_cast`, `date_parser`, `explode`, `hash`, `multi_replace`, `preg_filter`, `preg_match`, `slugify`, `trim`) gives `null` for `null` (`hash` gave the hash of an empty string, `slugify` threw an `UnexpectedValueException`, `explode` gave `[]`, `multi_replace` `''`), casts a scalar or a `\Stringable` to a string, and throws an `UnexpectedValueException` for an array (cast to `'Array'` with a warning) or another object (raw `Error`); an array transformer (`array_element`, `array_last`, `array_unset`, `implode`, `instantiate`, `unset`) throws an `UnexpectedValueException` for anything but an array (`array_element` and `array_last` threw a `TypeError`); `sprintf` and `implode` reject an array or an object argument or item, `date_parser` an empty array (returned unchanged). New exception messages (see `UPGRADE.md`). Update documentation, add tests.

## Changes
* [#428](https://github.com/cleverage/process-bundle/issues/428) Tests on PHPUnit 12.5 and 13 (PHPUnit 10 and 11 dropped), with the lowest dependencies on PHP 8.3 and 8.5.
Expand Down
43 changes: 43 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,49 @@ slugify:
strip_tags: true
```

### Inputs of the transformers

The transformers share one policy for the inputs they do not expect, documented in the "Accepted inputs" section of
their reference page:

* a string transformer (`convert_value` with `auto_cast`, `date_parser`, `explode`, `hash`, `multi_replace`,
`preg_filter`, `preg_match`, `slugify`, `trim`) gives `null` for `null`, casts a scalar or a `\Stringable` to a
string, and throws an `UnexpectedValueException` for an array or another object (a `\BackedEnum` included);
* an array transformer (`array_element`, `array_last`, `array_unset`, `implode`, `instantiate`, `unset`) throws an
`UnexpectedValueException` for anything but an array, `null` included;
* the other transformers keep their own contract, unchanged (`callback`, `cast`, `default`, `mapping`, `rules`...,
the arguments of `sprintf`, a date for `date_format`...): see their reference page.

The results change for these inputs:

| Transformer | Input | 5.x | 6.0 |
|----------------------------------|------------------------------------|----------------------------------------|----------------------------|
| `hash` | `null` | hash of `''` | `null` |
| `slugify` | `null` | `UnexpectedValueException` | `null` |
| `explode` | `null` | `[]` | `null` |
| `multi_replace` | `null` | `''` (`null` if `replace_mapping` is empty) | `null` |
| `preg_filter` | `null` | `null` or the replaced `''` | `null` |
| `multi_replace` | scalar, empty `replace_mapping` | returned unchanged | cast to a string |
| `multi_replace` | array, empty `replace_mapping` | returned unchanged | `UnexpectedValueException` |
| `trim`, `explode`, `multi_replace`, `preg_filter`, `preg_match` | array | `'Array'` (a warning, an exception only when the PHP warnings are thrown) | `UnexpectedValueException` |
| same, and `convert_value` with `auto_cast` | object without `__toString()`, enum | `Error` | `UnexpectedValueException` |
| `sprintf`, `implode` | array or object without `__toString()` as an argument or an item | `'Array'` with a warning, or `Error` | `UnexpectedValueException` |
| `array_element`, `array_last` | anything but an array | `TypeError` | `UnexpectedValueException` |
| `date_parser` | `[]` | returned unchanged | `UnexpectedValueException` |
| `explode` | `false` | `['']` | `[]` (as `''`) |
| `preg_match` | `false` | the matches of `''` | `null` (as `''`) |

The messages of the exceptions change too: `The <code> transformer expects a string, <type> given.`,
`The <code> transformer expects an array, <type> given.`, `The sprintf transformer expects strings, <type> given.` and
`The implode transformer expects an array of strings, <type> item given.`; `convert_value` without `auto_cast` gives
the type with `get_debug_type()` (`float`, `bool`, `stdClass` instead of `double`, `boolean`, `object`).
Within a process, these exceptions are wrapped in a `TransformerException` and the `error_strategy` of the task
applies, as before.

Check the processes that hash, slugify, explode or replace an optional value: a missing value now stays `null`. Add a
`default` transformer after them where an empty string or an empty array is expected (`explode` followed by
`array_map` or `implode`, which reject `null`).

### Error output of the CSV readers

A line that `csv_reader` or `input_csv_reader` cannot read (number of columns not matching the headers, line that
Expand Down
4 changes: 3 additions & 1 deletion docs/reference/transformers/_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ Transformer reference
Accepted inputs
---------------

_Description of allowed types._
_Description of allowed types. A string transformer gives `null` for `null`, casts a scalar or a `\Stringable` to a
string and throws an `UnexpectedValueException` for an array or another object; an array transformer throws an
`UnexpectedValueException` for anything but an array._

Possible outputs
----------------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/transformers/array_element_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Transformer reference
Accepted inputs
---------------

`array`
`array`. Any other value, `null` included, throws an `UnexpectedValueException`.

Possible outputs
----------------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/transformers/array_last_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Transformer reference
Accepted inputs
---------------

`array`
`array`. Any other value, `null` included, throws an `UnexpectedValueException`.

Possible outputs
----------------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/transformers/array_unset_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Transformer reference
Accepted inputs
---------------

`array`. Any other value throws an `UnexpectedValueException`.
`array`. Any other value, `null` included, throws an `UnexpectedValueException`.

Possible outputs
----------------
Expand Down
5 changes: 3 additions & 2 deletions docs/reference/transformers/convert_value_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Accepted inputs

* `string` or `int`: used as a key of `map`
* `null`: always returns `null`, without lookup
* other scalars or objects: only if `auto_cast` is `true` (cast to string), otherwise an `UnexpectedValueException` is
thrown. Arrays are always rejected.
* another scalar or a `\Stringable`: only if `auto_cast` is `true` (cast to string), otherwise an
`UnexpectedValueException` is thrown
* an array or another object (a `\BackedEnum` included): always an `UnexpectedValueException`

Possible outputs
----------------
Expand Down
5 changes: 3 additions & 2 deletions docs/reference/transformers/date_parser_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ Accepted inputs
* another scalar (e.g. an `int` timestamp with the `U` format) or a `\Stringable`, cast to string
* `\DateTime`, returned unchanged
* `\DateTimeImmutable`, converted to a `\DateTime` (same date, time and timezone)
* Any falsy value (`null`, `''`, `false`…), returned unchanged
* Any falsy value (`null`, `''`, `false`…) but an empty array, returned unchanged

A string that cannot be parsed with the given format throws an `UnexpectedValueException`, and so does a date or a
time out of range (e.g. `2024-02-31` or `25:61`, that `\DateTime::createFromFormat()` parses with a warning, the date
overflowing to `2024-03-02`). The trailing data allowed by a `+` in the format (e.g. `!Y-m-d+` for
`2024-02-29T10:00:00Z`) is ignored. An array or another object throws an `UnexpectedValueException`.
`2024-02-29T10:00:00Z`) is ignored. An array or another object (a `\BackedEnum` included) throws an
`UnexpectedValueException`.

Possible outputs
----------------
Expand Down
6 changes: 4 additions & 2 deletions docs/reference/transformers/explode_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ Transformer reference
Accepted inputs
---------------

Any value that can be cast to `string`, or `null`.
`string`, or a scalar or a `\Stringable` cast to a string. `null` gives `null`. An array or another object (a
`\BackedEnum` included) throws an `UnexpectedValueException`.

Possible outputs
----------------

`array`: the list of parts; an empty array if the input is `null` or an empty string
* `array`: the list of parts; an empty array for an empty string
* `null` if the input is `null`

Options
-------
Expand Down
8 changes: 5 additions & 3 deletions docs/reference/transformers/hash_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ Transformer reference
Accepted inputs
---------------

Any value that can be cast to `string` (`null` gives the hash of an empty string). An array or an object that is not
`\Stringable` throws an `UnexpectedValueException` (an array was hashed as the string `Array`).
`string`, or a scalar or a `\Stringable` cast to a string. `null` gives `null`. An array or another object (a
`\BackedEnum` included) throws an `UnexpectedValueException`. `null` is not hashed (it gave the
hash of an empty string, the same for every missing value).

Possible outputs
----------------

`string`: the hash, as lowercase hexits by default, or raw binary data if `raw_output` is `true`
* `string`: the hash, as lowercase hexits by default, or raw binary data if `raw_output` is `true`
* `null` if the input is `null`

Options
-------
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/transformers/implode_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Transformer reference
Accepted inputs
---------------

`array` of values that can be cast to `string`. Any other input throws an `UnexpectedValueException`.
`array` of scalars, `null` or `\Stringable` values, cast to strings. Any other input, `null` included, or an item that
is an array or another object throws an `UnexpectedValueException`.

Possible outputs
----------------
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/transformers/instantiate_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Accepted inputs
---------------

`array`: the constructor arguments. A list is passed positionally; string keys are used as named arguments. Any other
value throws an `UnexpectedValueException`.
value, `null` included, throws an `UnexpectedValueException`.

Possible outputs
----------------
Expand Down
7 changes: 4 additions & 3 deletions docs/reference/transformers/multi_replace_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ Transformer reference
Accepted inputs
---------------

Any value that can be cast to string.
`string`, or a scalar or a `\Stringable` cast to a string. `null` gives `null`. An array or another object (a
`\BackedEnum` included) throws an `UnexpectedValueException`.

Possible outputs
----------------

`string`: the input with all replacements applied. If `replace_mapping` is empty, the input is returned unchanged
(not cast).
* `string`: the input with all replacements applied (cast to a string, even if `replace_mapping` is empty)
* `null` if the input is `null`

Options
-------
Expand Down
5 changes: 3 additions & 2 deletions docs/reference/transformers/preg_filter_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ Transformer reference
Accepted inputs
---------------

Any value that can be cast to string.
`string`, or a scalar or a `\Stringable` cast to a string. `null` gives `null`. An array or another object (a
`\BackedEnum` included) throws an `UnexpectedValueException`.

Possible outputs
----------------

`string|null`: the replaced string, or `null` if no pattern matches.
`string|null`: the replaced string, or `null` if no pattern matches or if the input is `null`.

An invalid regular expression is rejected with an `InvalidOptionsException` when the options are resolved. A PCRE
error while matching (e.g. invalid UTF-8 with the `u` modifier, backtracking limit reached) throws an
Expand Down
3 changes: 2 additions & 1 deletion docs/reference/transformers/preg_match_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Transformer reference
Accepted inputs
---------------

Any value that can be cast to `string`, or `null`.
`string`, or a scalar or a `\Stringable` cast to a string. `null` gives `null`. An array or another object (a
`\BackedEnum` included) throws an `UnexpectedValueException`.

Possible outputs
----------------
Expand Down
7 changes: 4 additions & 3 deletions docs/reference/transformers/slugify_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ Transformer reference
Accepted inputs
---------------

`string`, or a scalar or `\Stringable` cast to string. `null`, an array or another object throws an
`UnexpectedValueException`.
`string`, or a scalar or a `\Stringable` cast to a string. `null` gives `null`. An array or another object (a
`\BackedEnum` included) throws an `UnexpectedValueException`.

Possible outputs
----------------

`string`
* `string`
* `null` if the input is `null`

Options
-------
Expand Down
2 changes: 2 additions & 0 deletions docs/reference/transformers/sprintf_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Accepted inputs
* `array`: each element is used as an argument of the format, in order
* any other value (scalar, `null`, `\Stringable`): used as the single argument of the format

An argument that is an array or another object (a `\BackedEnum` included) throws an `UnexpectedValueException`.

Possible outputs
----------------

Expand Down
3 changes: 2 additions & 1 deletion docs/reference/transformers/trim_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Transformer reference
Accepted inputs
---------------

Any value that can be cast to `string`, or `null`.
`string`, or a scalar or a `\Stringable` cast to a string. `null` gives `null`. An array or another object (a
`\BackedEnum` included) throws an `UnexpectedValueException`.

Possible outputs
----------------
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/transformers/unset_transformer.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ Transformer reference
Accepted inputs
---------------

`array` containing the `property` key. An `UnexpectedValueException` is thrown if the input is not an array, or if the
key does not exist (even when the condition is not met).
`array` containing the `property` key. An `UnexpectedValueException` is thrown if the input is not an array (`null`
included), or if the key does not exist (even when the condition is not met).

Possible outputs
----------------
Expand Down
2 changes: 2 additions & 0 deletions src/Transformer/Array/ArrayElementTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CleverAge\ProcessBundle\Transformer\Array;

use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
use CleverAge\ProcessBundle\Transformer\InputPolicy;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -26,6 +27,7 @@ final class ArrayElementTransformer implements ConfigurableTransformerInterface
*/
public function transform(mixed $value, array $options = []): mixed
{
$value = InputPolicy::toArray($value, 'array_element');
$index = $options['index'];
// null when the index is outside the array, at either end: no "Undefined array key" warning after the end, and
// not the first element (as array_slice() would give) before the start
Expand Down
3 changes: 3 additions & 0 deletions src/Transformer/Array/ArrayLastTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CleverAge\ProcessBundle\Transformer\Array;

use CleverAge\ProcessBundle\Transformer\InputPolicy;
use CleverAge\ProcessBundle\Transformer\TransformerInterface;

/**
Expand All @@ -25,6 +26,8 @@ final class ArrayLastTransformer implements TransformerInterface
*/
public function transform(mixed $value, array $options = []): mixed
{
$value = InputPolicy::toArray($value, 'array_last');

// null for an empty array (no "Undefined array key" warning)
return array_values(\array_slice($value, -1))[0] ?? null;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Transformer/Array/ArrayUnsetTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace CleverAge\ProcessBundle\Transformer\Array;

use CleverAge\ProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
use CleverAge\ProcessBundle\Transformer\InputPolicy;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -27,9 +27,7 @@ final class ArrayUnsetTransformer implements ConfigurableTransformerInterface
*/
public function transform(mixed $value, array $options = []): mixed
{
if (!\is_array($value)) {
throw new UnexpectedValueException('Given value is not an array');
}
$value = InputPolicy::toArray($value, 'array_unset');
unset($value[$options['key']]);

return $value;
Expand Down
8 changes: 3 additions & 5 deletions src/Transformer/ConvertValueTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ public function transform(mixed $value, array $options = []): mixed
$input = $value;
if (!\is_string($value) && !\is_int($value)) { // If not a valid array index
if (!$options['auto_cast']) {
$type = \gettype($value);
$type = get_debug_type($value);
throw new UnexpectedValueException("Value of type {$type} is not a valid array index, set auto_cast to true to cast it to a string");
}
if (\is_array($value)) { // Array to string conversion is a simple notice so we need to catch it here
throw new UnexpectedValueException("Unexpected input of type 'array' in convert_value transformer");
}
$value = (string) $value; // Let's cast it to string
// An array or an object that cannot be cast to a string is rejected
$value = InputPolicy::toString($value, 'convert_value');
}

if (!\array_key_exists($value, $options['map'])) {
Expand Down
10 changes: 4 additions & 6 deletions src/Transformer/Date/DateParserTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CleverAge\ProcessBundle\Exception\UnexpectedValueException;
use CleverAge\ProcessBundle\Transformer\ConfigurableTransformerInterface;
use CleverAge\ProcessBundle\Transformer\InputPolicy;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -32,19 +33,16 @@ final class DateParserTransformer implements ConfigurableTransformerInterface
*/
public function transform(mixed $value, array $options = []): mixed
{
if (!$value || $value instanceof \DateTime) {
// An empty array is rejected, as any array
if ((!$value && [] !== $value) || $value instanceof \DateTime) {
return $value;
}

if ($value instanceof \DateTimeImmutable) {
return \DateTime::createFromImmutable($value);
}

if (\is_int($value) || \is_float($value) || \is_bool($value) || $value instanceof \Stringable) {
$value = (string) $value;
} elseif (!\is_string($value)) {
throw new UnexpectedValueException(\sprintf('The date_parser transformer expects a string, %s given.', get_debug_type($value)));
}
$value = InputPolicy::toString($value, 'date_parser');

$date = \DateTime::createFromFormat($options['format'], $value);

Expand Down
Loading