-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Closure; | ||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
use Psl\Type\TypeInterface; | ||
use Throwable; | ||
|
||
/** | ||
* @template I | ||
* @template O | ||
* | ||
* @ara-extends Type\Type<O> | ||
* | ||
* @extends Type\Type<O> | ||
* | ||
* @internal | ||
*/ | ||
final class ConvertedType extends Type\Type | ||
{ | ||
/** | ||
* @param TypeInterface<I> $from | ||
* @param TypeInterface<O> $into | ||
* @param (\Closure(I): O) $converter | ||
*/ | ||
public function __construct( | ||
private TypeInterface $from, | ||
private TypeInterface $into, | ||
private Closure $converter | ||
) { | ||
} | ||
|
||
/** | ||
* @throws CoercionException | ||
* | ||
* @ara-return O | ||
* | ||
* @return O | ||
*/ | ||
public function coerce(mixed $value): mixed | ||
{ | ||
if ($this->into->matches($value)) { | ||
return $value; | ||
} | ||
|
||
$coercedInput = $this->from->coerce($value); | ||
|
||
try { | ||
$converted = ($this->converter)($coercedInput); | ||
} catch (Throwable $failure) { | ||
throw CoercionException::withConversionFailureOnValue($value, $this->toString(), $this->getTrace(), $failure); | ||
} | ||
|
||
return $this->into->coerce($converted); | ||
} | ||
|
||
/** | ||
* @ara-assert O $value | ||
* | ||
* @psalm-assert O $value | ||
* | ||
* @throws AssertException | ||
* | ||
* @ara-return O | ||
* | ||
* @return O | ||
*/ | ||
public function assert(mixed $value): mixed | ||
{ | ||
return $this->into->assert($value); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return $this->into->toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type; | ||
|
||
use Closure; | ||
|
||
/** | ||
* @template I | ||
* @template O | ||
* | ||
* @param TypeInterface<I> $from | ||
* @param TypeInterface<O> $into | ||
* @param (Closure(I): O) $converter | ||
* | ||
* @ara-return TypeInterface<O> | ||
* | ||
* @return TypeInterface<O> | ||
*/ | ||
function converted(TypeInterface $from, TypeInterface $into, Closure $converter): TypeInterface | ||
{ | ||
return new Internal\ConvertedType($from, $into, $converter); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Type; | ||
|
||
use DateTimeImmutable; | ||
use Psl\Type; | ||
use RuntimeException; | ||
|
||
final class ConvertedTypeTest extends TypeTest | ||
{ | ||
private const DATE_FORMAT = 'Y-m-d H:i:s'; | ||
|
||
public function getType(): Type\TypeInterface | ||
{ | ||
return Type\converted( | ||
Type\string(), | ||
Type\instance_of(DateTimeImmutable::class), | ||
static fn (string $value): DateTimeImmutable => | ||
DateTimeImmutable::createFromFormat(self::DATE_FORMAT, $value) | ||
?: throw new RuntimeException('Unable to parse date format'), | ||
); | ||
} | ||
|
||
public function getValidCoercions(): iterable | ||
{ | ||
yield ['2023-04-27 08:28:00', DateTimeImmutable::createFromFormat(self::DATE_FORMAT, '2023-04-27 08:28:00')]; | ||
yield [$this->stringable('2023-04-27 08:28:00'), DateTimeImmutable::createFromFormat(self::DATE_FORMAT, '2023-04-27 08:28:00')]; | ||
} | ||
|
||
public function getInvalidCoercions(): iterable | ||
{ | ||
yield [1]; | ||
yield [false]; | ||
yield ['']; | ||
yield ['2023-04-27']; | ||
yield ['2023-04-27 08:26']; | ||
yield ['27/04/2023']; | ||
yield [$this->stringable('2023-04-27')]; | ||
} | ||
|
||
/** | ||
* @param DateTimeImmutable|mixed $a | ||
* @param DateTimeImmutable|mixed $b | ||
*/ | ||
protected function equals($a, $b): bool | ||
{ | ||
if (Type\instance_of(DateTimeImmutable::class)->matches($a)) { | ||
$a = $a->format(self::DATE_FORMAT); | ||
} | ||
|
||
if (Type\instance_of(DateTimeImmutable::class)->matches($b)) { | ||
$b = $b->format(self::DATE_FORMAT); | ||
} | ||
|
||
return parent::equals($a, $b); | ||
} | ||
|
||
public function getToStringExamples(): iterable | ||
{ | ||
yield [$this->getType(), DateTimeImmutable::class]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 matchThere 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:
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