bd-hydration-coercion
better-data: Hydration and coercion
For library maintainers fixing or extending how stored / incoming values become typed property values on a DataObject. The coercion layer sits between the source's raw fetch and the constructor's typed parameters; modifying it touches every DTO that goes through ::fromArray.
Misconception this skill corrects
"I'll just
settype($value, 'int')or(int) $valueinside the hydrator — same effect."
Wrong. PHP's silent casts paper over invalid input — (int) 'abc' === 0, (int) '12foo' === 12, (bool) 'false' === true. better-data's coercion is intentionally strict: surprising input becomes TypeCoercionException with the field name, expected type, and offending value. Verified at src/Internal/TypeCoercer.php:46-58 and the per-helper throws (toString:197, toInt:218, toFloat:235, toBool:254).
The discipline is:
// WRONG inside coercion code
$intValue = (int) $value;