Skip to content

Commit 7607ea1

Browse files
committed
Add building context to type builders
1 parent d0cb496 commit 7607ea1

23 files changed

+98
-203
lines changed

example/03.types/03.custom-type-template-arguments.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
declare(strict_types=1);
44

5+
use TypeLang\Mapper\Context\BuildingContext;
56
use TypeLang\Mapper\Context\RuntimeContext;
67
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
78
use TypeLang\Mapper\Mapper;
8-
use TypeLang\Mapper\Platform\DelegatePlatform;
99
use TypeLang\Mapper\Platform\StandardPlatform;
1010
use TypeLang\Mapper\Type\Builder\Builder;
11-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
12-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
1311
use TypeLang\Mapper\Type\TypeInterface;
1412
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
1513
use TypeLang\Parser\Node\Stmt\TypeStatement;
@@ -26,11 +24,8 @@ public function isSupported(TypeStatement $statement): bool
2624
&& $statement->name->toLowerString() === 'non-empty';
2725
}
2826

29-
public function build(
30-
TypeStatement $statement,
31-
TypeRepositoryInterface $types,
32-
TypeParserInterface $parser,
33-
): TypeInterface {
27+
public function build(TypeStatement $statement, BuildingContext $context): TypeInterface
28+
{
3429
// Shape fields not allowed (like: "non-empty{...}")
3530
$this->expectNoShapeFields($statement);
3631
// Expects only template argument (like: "non-empty<T>", but NOT "non-empty<T, U>")
@@ -39,7 +34,7 @@ public function build(
3934
$innerArgument = $statement->arguments->first();
4035

4136
// inner type of TypeInterface
42-
$type = $types->getTypeByStatement($innerArgument->value);
37+
$type = $context->types->getTypeByStatement($innerArgument->value);
4338

4439
return new MyNonEmptyType($type);
4540
}
@@ -67,8 +62,7 @@ public function cast(mixed $value, RuntimeContext $context): mixed
6762
}
6863
}
6964

70-
$mapper = new Mapper(new DelegatePlatform(
71-
delegate: new StandardPlatform(),
65+
$mapper = new Mapper(new StandardPlatform(
7266
// Extend by custom "MyNonEmptyTypeBuilder" type builder
7367
types: [new MyNonEmptyTypeBuilder()],
7468
));

src/Type/Builder/BackedEnumTypeBuilder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Exception\Definition\InternalTypeException;
8-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
9-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
109
use TypeLang\Mapper\Type\TypeInterface;
1110
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
1211
use TypeLang\Parser\Node\Stmt\TypeStatement;
@@ -54,8 +53,7 @@ private function getBackedEnumType(\ReflectionEnum $reflection, NamedTypeNode $s
5453

5554
public function build(
5655
TypeStatement $statement,
57-
TypeRepositoryInterface $types,
58-
TypeParserInterface $parser,
56+
BuildingContext $context,
5957
): TypeInterface {
6058
$this->expectNoShapeFields($statement);
6159
$this->expectNoTemplateArguments($statement);
@@ -69,9 +67,7 @@ public function build(
6967
class: $statement->name->toString(),
7068
definition: $definition,
7169
/** @phpstan-ignore-next-line : The "getTypeByStatement" returns TypeInterface<value-of<TEnum>> */
72-
type: $types->getTypeByStatement(
73-
statement: $parser->getStatementByDefinition($definition),
74-
),
70+
type: $context->getTypeByDefinition($definition),
7571
);
7672
}
7773

src/Type/Builder/BoolLiteralTypeBuilder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Type\BoolLiteralType;
8-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
9-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
109
use TypeLang\Parser\Node\Literal\BoolLiteralNode;
1110
use TypeLang\Parser\Node\Stmt\TypeStatement;
1211

@@ -20,11 +19,8 @@ public function isSupported(TypeStatement $statement): bool
2019
return $statement instanceof BoolLiteralNode;
2120
}
2221

23-
public function build(
24-
TypeStatement $statement,
25-
TypeRepositoryInterface $types,
26-
TypeParserInterface $parser,
27-
): BoolLiteralType {
22+
public function build(TypeStatement $statement, BuildingContext $context): BoolLiteralType
23+
{
2824
return new BoolLiteralType($statement->value);
2925
}
3026
}

src/Type/Builder/CallableTypeBuilder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Exception\Definition\InternalTypeException;
8-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
9-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
109
use TypeLang\Mapper\Type\TypeInterface;
1110
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
1211
use TypeLang\Parser\Node\Stmt\TypeStatement;
@@ -38,11 +37,8 @@ public function __construct(
3837
parent::__construct($names);
3938
}
4039

41-
public function build(
42-
TypeStatement $statement,
43-
TypeRepositoryInterface $types,
44-
TypeParserInterface $parser,
45-
): TypeInterface {
40+
public function build(TypeStatement $statement, BuildingContext $context): TypeInterface
41+
{
4642
/** @phpstan-ignore-next-line : Additional DbC assertion */
4743
assert($statement instanceof NamedTypeNode);
4844

src/Type/Builder/ClassTypeBuilder.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Instantiator\ClassInstantiatorInterface;
89
use TypeLang\Mapper\Instantiator\DoctrineClassInstantiator;
910
use TypeLang\Mapper\Instantiator\ReflectionClassInstantiator;
1011
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
1112
use TypeLang\Mapper\Mapping\Provider\ProviderInterface;
1213
use TypeLang\Mapper\PropertyAccessor\PropertyAccessorInterface;
1314
use TypeLang\Mapper\PropertyAccessor\ReflectionPropertyAccessor;
14-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
15-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
1615
use TypeLang\Mapper\Type\TypeInterface;
1716
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
1817
use TypeLang\Parser\Node\Stmt\TypeStatement;
@@ -75,11 +74,8 @@ public function isSupported(TypeStatement $statement): bool
7574
|| $reflection->isInterface();
7675
}
7776

78-
public function build(
79-
TypeStatement $statement,
80-
TypeRepositoryInterface $types,
81-
TypeParserInterface $parser,
82-
): TypeInterface {
77+
public function build(TypeStatement $statement, BuildingContext $context): TypeInterface
78+
{
8379
$this->expectNoShapeFields($statement);
8480
$this->expectNoTemplateArguments($statement);
8581

@@ -89,8 +85,8 @@ public function build(
8985
return $this->create(
9086
metadata: $this->driver->getClassMetadata(
9187
class: new \ReflectionClass($class),
92-
types: $types,
93-
parser: $parser,
88+
types: $context->types,
89+
parser: $context->parser,
9490
),
9591
);
9692
}

src/Type/Builder/DateTimeTypeBuilder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Exception\Definition\Template\InvalidTemplateArgumentException;
8-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
9-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
109
use TypeLang\Mapper\Type\TypeInterface;
1110
use TypeLang\Parser\Node\Literal\StringLiteralNode;
1211
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
@@ -25,11 +24,8 @@ public function isSupported(TypeStatement $statement): bool
2524
&& \is_a($statement->name->toLowerString(), \DateTimeInterface::class, true);
2625
}
2726

28-
public function build(
29-
TypeStatement $statement,
30-
TypeRepositoryInterface $types,
31-
TypeParserInterface $parser,
32-
): TypeInterface {
27+
public function build(TypeStatement $statement, BuildingContext $context): TypeInterface
28+
{
3329
$this->expectNoShapeFields($statement);
3430
$this->expectTemplateArgumentsLessOrEqualThan($statement, 1);
3531

src/Type/Builder/FloatLiteralTypeBuilder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Type\FloatLiteralType;
8-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
9-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
109
use TypeLang\Parser\Node\Literal\FloatLiteralNode;
1110
use TypeLang\Parser\Node\Stmt\TypeStatement;
1211

@@ -20,11 +19,8 @@ public function isSupported(TypeStatement $statement): bool
2019
return $statement instanceof FloatLiteralNode;
2120
}
2221

23-
public function build(
24-
TypeStatement $statement,
25-
TypeRepositoryInterface $types,
26-
TypeParserInterface $parser,
27-
): FloatLiteralType {
22+
public function build(TypeStatement $statement, BuildingContext $context): FloatLiteralType
23+
{
2824
return new FloatLiteralType($statement->value);
2925
}
3026
}

src/Type/Builder/IntLiteralTypeBuilder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Type\IntLiteralType;
8-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
9-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
109
use TypeLang\Parser\Node\Literal\IntLiteralNode;
1110
use TypeLang\Parser\Node\Stmt\TypeStatement;
1211

@@ -20,11 +19,8 @@ public function isSupported(TypeStatement $statement): bool
2019
return $statement instanceof IntLiteralNode;
2120
}
2221

23-
public function build(
24-
TypeStatement $statement,
25-
TypeRepositoryInterface $types,
26-
TypeParserInterface $parser,
27-
): IntLiteralType {
22+
public function build(TypeStatement $statement, BuildingContext $context): IntLiteralType
23+
{
2824
return new IntLiteralType($statement->value);
2925
}
3026
}

src/Type/Builder/IntRangeTypeBuilder.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Exception\Definition\Shape\ShapeFieldsNotSupportedException;
89
use TypeLang\Mapper\Exception\Definition\Template\Hint\TemplateArgumentHintNotSupportedException;
910
use TypeLang\Mapper\Exception\Definition\Template\InvalidTemplateArgumentException;
1011
use TypeLang\Mapper\Exception\Definition\Template\OneOfTemplateArgumentsCountException;
1112
use TypeLang\Mapper\Exception\Definition\Template\TooManyTemplateArgumentsInRangeException;
1213
use TypeLang\Mapper\Type\IntRangeType;
1314
use TypeLang\Mapper\Type\IntType;
14-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
15-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
1615
use TypeLang\Mapper\Type\TypeInterface;
1716
use TypeLang\Parser\Node\Literal\IntLiteralNode;
1817
use TypeLang\Parser\Node\Literal\StringLiteralNode;
@@ -32,11 +31,8 @@ class IntRangeTypeBuilder extends NamedTypeBuilder
3231
* @throws TooManyTemplateArgumentsInRangeException
3332
* @throws ShapeFieldsNotSupportedException
3433
*/
35-
public function build(
36-
TypeStatement $statement,
37-
TypeRepositoryInterface $types,
38-
TypeParserInterface $parser,
39-
): TypeInterface {
34+
public function build(TypeStatement $statement, BuildingContext $context): TypeInterface
35+
{
4036
/** @phpstan-ignore-next-line : Additional DbC assertion */
4137
assert($statement instanceof NamedTypeNode);
4238

src/Type/Builder/ListTypeBuilder.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@
44

55
namespace TypeLang\Mapper\Type\Builder;
66

7+
use TypeLang\Mapper\Context\BuildingContext;
78
use TypeLang\Mapper\Exception\Definition\Shape\ShapeFieldsNotSupportedException;
89
use TypeLang\Mapper\Exception\Definition\Template\Hint\TemplateArgumentHintNotSupportedException;
910
use TypeLang\Mapper\Exception\Definition\Template\TooManyTemplateArgumentsInRangeException;
1011
use TypeLang\Mapper\Exception\Definition\TypeNotFoundException;
1112
use TypeLang\Mapper\Type\ListType;
12-
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
13-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
1413
use TypeLang\Mapper\Type\TypeInterface;
1514
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
1615
use TypeLang\Parser\Node\Stmt\Template\TemplateArgumentNode;
@@ -44,11 +43,8 @@ public function __construct(
4443
* @throws TypeNotFoundException
4544
* @throws \Throwable
4645
*/
47-
public function build(
48-
TypeStatement $statement,
49-
TypeRepositoryInterface $types,
50-
TypeParserInterface $parser,
51-
): TypeInterface {
46+
public function build(TypeStatement $statement, BuildingContext $context): TypeInterface
47+
{
5248
/** @phpstan-ignore-next-line : Additional DbC assertion */
5349
assert($statement instanceof NamedTypeNode);
5450

@@ -57,8 +53,8 @@ public function build(
5753
$arguments = $statement->arguments->items ?? [];
5854

5955
return match (\count($arguments)) {
60-
0 => $this->buildWithNoValue($types, $parser),
61-
1 => $this->buildWithValue($statement, $types),
56+
0 => $this->buildWithNoValue($context),
57+
1 => $this->buildWithValue($statement, $context),
6258
default => throw TooManyTemplateArgumentsInRangeException::becauseArgumentsCountRequired(
6359
minArgumentsCount: 0,
6460
maxArgumentsCount: 1,
@@ -71,13 +67,11 @@ public function build(
7167
* @throws TypeNotFoundException
7268
* @throws \Throwable
7369
*/
74-
private function buildWithNoValue(TypeRepositoryInterface $types, TypeParserInterface $parser): TypeInterface
70+
private function buildWithNoValue(BuildingContext $context): TypeInterface
7571
{
7672
return new ListType(
77-
value: $types->getTypeByStatement(
78-
statement: $parser->getStatementByDefinition(
79-
definition: $this->defaultValueType,
80-
),
73+
value: $context->getTypeByDefinition(
74+
definition: $this->defaultValueType,
8175
),
8276
);
8377
}
@@ -87,7 +81,7 @@ private function buildWithNoValue(TypeRepositoryInterface $types, TypeParserInte
8781
* @throws TypeNotFoundException
8882
* @throws \Throwable
8983
*/
90-
private function buildWithValue(NamedTypeNode $statement, TypeRepositoryInterface $types): TypeInterface
84+
private function buildWithValue(NamedTypeNode $statement, BuildingContext $context): TypeInterface
9185
{
9286
$arguments = $statement->arguments->items ?? [];
9387

@@ -99,7 +93,7 @@ private function buildWithValue(NamedTypeNode $statement, TypeRepositoryInterfac
9993
$this->expectNoTemplateArgumentHint($statement, $value);
10094

10195
return new ListType(
102-
value: $types->getTypeByStatement(
96+
value: $context->getTypeByStatement(
10397
statement: $value->value,
10498
),
10599
);

0 commit comments

Comments
 (0)