Skip to content

Commit 4346bbd

Browse files
author
Kirill Nesmeyanov
committed
Improve types
1 parent 18b014f commit 4346bbd

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

src/Runtime/Repository/TypeRepository.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ public function __construct(
3131
private readonly ReferencesResolver $references = new ReferencesResolver(),
3232
) {
3333
$this->context = $this;
34-
$this->builders = match (true) {
34+
$this->builders = self::toArrayList($types);
35+
}
36+
37+
/**
38+
* @param iterable<array-key, TypeBuilderInterface<covariant TypeStatement, TypeInterface>> $types
39+
* @return list<TypeBuilderInterface<covariant TypeStatement, TypeInterface>>
40+
*/
41+
private static function toArrayList(iterable $types): array
42+
{
43+
return match (true) {
3544
$types instanceof \Traversable => \iterator_to_array($types, false),
3645
\array_is_list($types) => $types,
3746
default => \array_values($types),
3847
};
3948
}
4049

50+
/**
51+
* @internal internal method for passing the root calling context
52+
*/
4153
public function setTypeRepository(TypeRepositoryInterface $parent): void
4254
{
4355
$this->context = $parent;

src/Runtime/Repository/TypeRepositoryDecorator.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public function __construct(
1515
$this->setTypeRepository($this);
1616
}
1717

18+
/**
19+
* @internal internal method for passing the root calling context
20+
*/
1821
public function setTypeRepository(TypeRepositoryInterface $parent): void
1922
{
2023
if ($this->delegate instanceof TypeRepositoryDecoratorInterface) {

src/Runtime/Repository/TypeRepositoryDecoratorInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
namespace TypeLang\Mapper\Runtime\Repository;
66

77
/**
8+
* Defines a repository that supports embedding/decoration.
9+
*
810
* @internal this is an internal library interface, please do not use it in your code
911
* @psalm-internal TypeLang\Mapper\Runtime\Repository
1012
*/
1113
interface TypeRepositoryDecoratorInterface extends TypeRepositoryInterface
1214
{
15+
/**
16+
* @internal internal method for passing the root calling context
17+
*/
1318
public function setTypeRepository(TypeRepositoryInterface $parent): void;
1419
}

src/Type/ClassStringType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function __construct(
1818

1919
public function match(mixed $value, Context $context): bool
2020
{
21+
if ($context->isNormalization()) {
22+
return \is_string($value);
23+
}
24+
2125
$isValidString = $value !== '' && \is_string($value);
2226

2327
if (!$isValidString) {

src/Type/NumericStringType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class NumericStringType implements TypeInterface
1111
{
1212
public function match(mixed $value, Context $context): bool
1313
{
14+
if ($context->isNormalization()) {
15+
return \is_string($value);
16+
}
17+
1418
return \is_string($value) && \is_numeric($value);
1519
}
1620

0 commit comments

Comments
 (0)