Skip to content

[v6] #405 - Transformers: a common policy for null, array and object inputs - #525

Merged
njoubert-cleverage merged 1 commit into
dev-v6from
405
Dec 3, 2026
Merged

njoubert-cleverage merged 1 commit into
dev-v6from
405

Conversation

@njoubert-cleverage

Copy link
Copy Markdown
Member

Description

Fixes #405: one policy for the inputs the transformers do not expect, instead of a different behaviour by transformer (null, the input, '', an UnexpectedValueException, a raw TypeError / Error, or "Array" with a warning, silent when the PHP warnings are not thrown).

  • String transformers (convert_value with auto_cast, date_parser, explode, hash, multi_replace, preg_filter, preg_match, slugify, trim): null gives null, a scalar or a \Stringable is cast to a string, an array or another object (a \BackedEnum included) throws UnexpectedValueException: The <code> transformer expects a string, <type> given.
  • Array transformers (array_element, array_last, array_unset, implode, instantiate, unset): anything but an array, null included, throws UnexpectedValueException: The <code> transformer expects an array, <type> given. (array_element and array_last threw a TypeError).
  • sprintf and implode reject an argument or an item that is an array or an object.
  • The other transformers keep their own contract (callback, cast, default, mapping, rules, the arguments of sprintf, a date for date_format...). '' stays an ordinary string: the documented behaviours for empty values (date_parser returning a falsy value, explode('') → [], preg_match('') → null) are unchanged.
  • A guard in each transformer, with an @internal helper (InputPolicy). A shared data provider over the registry (TransformerInputPolicyTest) checks the policy for every transformer, and fails when a transformer is added without a row.
  • The "Accepted inputs" / "Possible outputs" sections of the 16 reference pages, the template, and a table of the changes in UPGRADE.md.

Checked locally: make quality, and PHPUnit with pcov (1428 tests, none risky).

Requirements

  • Documentation updates
    • Reference
    • Cookbooks
    • Changelog
  • Unit tests

Breaking changes

  • Major: null gives null with hash (the hash of '' before), slugify (an UnexpectedValueException before), explode ([] before) and multi_replace ('' before). Add a default transformer where an empty string or an empty array is expected (see UPGRADE.md).
  • Major: an array or an object that cannot be cast to a string throws an UnexpectedValueException in the string transformers, sprintf and implode ("Array" with a warning, or an Error, before); anything but an array throws one in array_element / array_last (TypeError before) and date_parser rejects an empty array.
  • Minor: new exception messages for the type errors of the transformers listed above.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@njoubert-cleverage
njoubert-cleverage requested a review from a team as a code owner December 3, 2026 10:45
@njoubert-cleverage
njoubert-cleverage merged commit b6fafeb into dev-v6 Dec 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant