Skip to content

Commit

Permalink
[Type] add non-empty-string type
Browse files Browse the repository at this point in the history
  • Loading branch information
azjezz committed Feb 1, 2021
1 parent 293047c commit 5aabbb2
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Psl/Internal/Loader.php
Expand Up @@ -316,6 +316,7 @@ final class Loader
'Psl\Type\object',
'Psl\Type\resource',
'Psl\Type\string',
'Psl\Type\non_empty_string',
'Psl\Type\scalar',
'Psl\Type\union',
'Psl\Type\is_array',
Expand Down Expand Up @@ -430,6 +431,7 @@ final class Loader
'Psl\Type\Internal\ObjectType',
'Psl\Type\Internal\ResourceType',
'Psl\Type\Internal\StringType',
'Psl\Type\Internal\NonEmptyStringType',
'Psl\Type\Internal\UnionType',
'Psl\Type\Exception\TypeTrace',
'Psl\Type\Exception\AssertException',
Expand Down
72 changes: 72 additions & 0 deletions src/Psl/Type/Internal/NonEmptyStringType.php
@@ -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';
}
}
13 changes: 13 additions & 0 deletions src/Psl/Type/non_empty_string.php
@@ -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();
}
52 changes: 52 additions & 0 deletions tests/Psl/Type/NonEmptyStringTypeTest.php
@@ -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'];
}
}

0 comments on commit 5aabbb2

Please sign in to comment.