-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Type] add non-empty-string type (#116)
- Loading branch information
Showing
5 changed files
with
140 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type\Internal; | ||
|
||
use Psl\Str; | ||
use Psl\Type; | ||
use Psl\Type\Exception\AssertException; | ||
use Psl\Type\Exception\CoercionException; | ||
|
||
/** | ||
* @extends Type\Type<non-empty-string> | ||
* | ||
* @internal | ||
*/ | ||
final class NonEmptyStringType extends Type\Type | ||
{ | ||
/** | ||
* @psalm-param mixed $value | ||
* | ||
* @psalm-return non-empty-string | ||
* | ||
* @throws CoercionException | ||
*/ | ||
public function coerce($value): string | ||
{ | ||
if (Type\is_string($value) && !Str\is_empty($value)) { | ||
return $value; | ||
} | ||
|
||
if (Type\is_int($value)) { | ||
$str = (string) $value; | ||
if (!Str\is_empty($str)) { | ||
/** @var non-empty-string $str */ | ||
return $str; | ||
} | ||
} | ||
|
||
if (Type\is_object($value) && method_exists($value, '__toString')) { | ||
$str = (string)$value; | ||
if (!Str\is_empty($str)) { | ||
return $str; | ||
} | ||
} | ||
|
||
throw CoercionException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
/** | ||
* @psalm-param mixed $value | ||
* | ||
* @psalm-return non-empty-string | ||
* | ||
* @psalm-assert non-empty-string $value | ||
* | ||
* @throws AssertException | ||
*/ | ||
public function assert($value): string | ||
{ | ||
if (Type\is_string($value) && !Str\is_empty($value)) { | ||
return $value; | ||
} | ||
|
||
throw AssertException::withValue($value, $this->toString(), $this->getTrace()); | ||
} | ||
|
||
public function toString(): string | ||
{ | ||
return 'non-empty-string'; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Type; | ||
|
||
/** | ||
* @psalm-return Type<non-empty-string> | ||
*/ | ||
function non_empty_string(): Type | ||
{ | ||
return new Internal\NonEmptyStringType(); | ||
} |
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Type; | ||
|
||
use Psl\Type; | ||
|
||
/** | ||
* @extends TypeTest<non-empty-string> | ||
*/ | ||
final class NonEmptyStringTypeTest extends TypeTest | ||
{ | ||
/** | ||
* @return Type\Type<non-empty-string> | ||
*/ | ||
public function getType(): Type\Type | ||
{ | ||
return Type\non_empty_string(); | ||
} | ||
|
||
public function getValidCoercions(): iterable | ||
{ | ||
yield ['hello', 'hello']; | ||
yield [$this->stringable('hello'), 'hello']; | ||
yield [123, '123']; | ||
yield [0, '0']; | ||
yield ['0', '0']; | ||
yield ['123', '123']; | ||
yield ['1e23', '1e23']; | ||
yield [$this->stringable('123'), '123']; | ||
} | ||
|
||
public function getInvalidCoercions(): iterable | ||
{ | ||
yield ['']; | ||
yield [1.0]; | ||
yield [1.23]; | ||
yield [[]]; | ||
yield [[1]]; | ||
yield [Type\bool()]; | ||
yield [null]; | ||
yield [false]; | ||
yield [true]; | ||
yield [STDIN]; | ||
} | ||
|
||
public function getToStringExamples(): iterable | ||
{ | ||
yield [$this->getType(), 'non-empty-string']; | ||
} | ||
} |