-
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
227 additions
and
2 deletions.
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