-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[type] introduce converted
type.
#405
Conversation
Pull Request Test Coverage Report for Build 4818281220
💛 - Coveralls |
return $value; | ||
} | ||
|
||
$coercedInput = $this->from->coerce($value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we try to coerce it into the into
type first? maybe that is possible, even if it doesn't match
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'dd say that would be a strange limitation. If you explicitly pass a converter function, it is strange to do some auto-conversion internal first?
For example: It would also block this case:
if you want to have a string that has a specific format, you could parse it like this now:
$scalarEmailType = Type\converted(
Type\string(),
Type\non_empty_string(),
static function (string $value): string {
if (!Regex\matches($value, $emailPattern)) {
throw new \RuntimeException('The provided email is not valid');
}
return $value;
}
);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way, it could solve scenarios like these:
#404
$customBoolType = Type\converted(
Type\string(),
Type\bool(),
static function (string $value): bool {
return !in_array($value, ['', 'False', 'No', '0'], true);
}
);
converted
type.
This PR introduces a new type that can be used to convert a given input into an expected output whilst coecing.
Example use-cases:
DateTimeImmutable
in your code.Email
value object.As a reference, Zod (a typescript equivalent of psl\type), has this:
https://zod.dev/?id=preprocess
This PR introduces:
It can be used like this:
Example
CoerceException
: