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
65 changed files
with
2,782 additions
and
14 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
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
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Exception; | ||
|
||
use Psl\Str; | ||
|
||
final class TypeAssertException extends TypeException | ||
{ | ||
private string $expected; | ||
|
||
public function __construct(string $actual, string $expected, TypeTrace $typeTrace) | ||
{ | ||
parent::__construct(Str\format('Expected "%s", got "%s".', $expected, $actual), $actual, $typeTrace); | ||
|
||
$this->expected = $expected; | ||
} | ||
|
||
public function getExpectedType(): string | ||
{ | ||
return $this->expected; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public static function withValue( | ||
$value, | ||
string $expected_type, | ||
TypeTrace $trace | ||
): self { | ||
return new self(static::getDebugType($value), $expected_type, $trace); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Exception; | ||
|
||
use Psl\Str; | ||
|
||
final class TypeCoercionException extends TypeException | ||
{ | ||
private string $target; | ||
|
||
public function __construct(string $actual, string $target, TypeTrace $typeTrace) | ||
{ | ||
parent::__construct( | ||
Str\format('Could not coerce "%s" to type "%s".', $actual, $target), | ||
$actual, | ||
$typeTrace, | ||
); | ||
|
||
$this->target = $target; | ||
} | ||
|
||
public function getTargetType(): string | ||
{ | ||
return $this->target; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public static function withValue( | ||
$value, | ||
string $target, | ||
TypeTrace $typeTrace | ||
): self { | ||
return new self(static::getDebugType($value), $target, $typeTrace); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Exception; | ||
|
||
use Psl\Exception\RuntimeException; | ||
use Psl\Str; | ||
|
||
abstract class TypeException extends RuntimeException | ||
{ | ||
private TypeTrace $typeTrace; | ||
private string $actual; | ||
|
||
public function __construct( | ||
string $message, | ||
string $actual, | ||
TypeTrace $typeTrace | ||
) { | ||
parent::__construct($message); | ||
|
||
$this->actual = $actual; | ||
$this->typeTrace = $typeTrace; | ||
} | ||
|
||
public function getActualType(): string | ||
{ | ||
return $this->actual; | ||
} | ||
|
||
public function getTypeTrace(): TypeTrace | ||
{ | ||
return $this->typeTrace; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
protected static function getDebugType($value): string | ||
{ | ||
if (is_object($value)) { | ||
$actual_type = get_class($value); | ||
} elseif (is_resource($value)) { | ||
$actual_type = Str\format('resource<%s>', get_resource_type($value)); | ||
} elseif (is_int($value)) { | ||
$actual_type = 'int'; | ||
} elseif (is_float($value)) { | ||
$actual_type = 'float'; | ||
} else { | ||
$actual_type = gettype($value); | ||
} | ||
|
||
return $actual_type; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Exception; | ||
|
||
final class TypeTrace | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
private array $frames = []; | ||
|
||
public function withFrame(string $frame): self | ||
{ | ||
$self = clone $this; | ||
$self->frames[] = $frame; | ||
|
||
return $self; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getFrames(): array | ||
{ | ||
return $this->frames; | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
/** | ||
* @extends UnionType<string, int> | ||
* | ||
* @internal | ||
*/ | ||
final class ArrayKeyType extends UnionType | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(new StringType(), new IntType()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'array-key'; | ||
} | ||
} |
Oops, something went wrong.