Skip to content

Commit d0cb496

Browse files
committed
Add mapping and building contexts
1 parent 880ce49 commit d0cb496

30 files changed

Lines changed: 351 additions & 196 deletions

src/Context/BuildingContext.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Context;
6+
7+
use JetBrains\PhpStorm\Language;
8+
use TypeLang\Mapper\Configuration;
9+
use TypeLang\Mapper\Type\Extractor\TypeExtractorInterface;
10+
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
11+
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
12+
use TypeLang\Mapper\Type\TypeInterface;
13+
use TypeLang\Parser\Node\Stmt\TypeStatement;
14+
15+
class BuildingContext extends MapperContext implements
16+
TypeRepositoryInterface
17+
{
18+
protected function __construct(
19+
/**
20+
* Gets data transformation direction.
21+
*/
22+
public readonly DirectionInterface $direction,
23+
/**
24+
* Responsible for obtaining the type ({@see TypeInterface}) instances
25+
* by the type statement.
26+
*
27+
* This repository belongs to the current context and may differ from
28+
* the initial (mappers) one.
29+
*
30+
* You can safely use all the methods of this interface, but for ease of
31+
* use, the following methods are available to you:
32+
*
33+
* - {@see RuntimeContext::getTypeByValue()} - returns type instance by the
34+
* passed value.
35+
* - {@see RuntimeContext::getTypeByDefinition()} - returns type instance by
36+
* the type definition string.
37+
* - {@see RuntimeContext::getTypeByStatement()} - returns type instance by
38+
* the type statement.
39+
*/
40+
public readonly TypeRepositoryInterface $types,
41+
TypeExtractorInterface $extractor,
42+
TypeParserInterface $parser,
43+
Configuration $config,
44+
) {
45+
parent::__construct(
46+
extractor: $extractor,
47+
parser: $parser,
48+
config: $config,
49+
);
50+
}
51+
52+
public static function createFromMapperContext(
53+
MapperContext $context,
54+
DirectionInterface $direction,
55+
TypeRepositoryInterface $types,
56+
): self {
57+
return new self(
58+
direction: $direction,
59+
types: $types,
60+
extractor: $context->extractor,
61+
parser: $context->parser,
62+
config: $context->config,
63+
);
64+
}
65+
66+
/**
67+
* Returns the {@see TypeInterface} instance associated with passed value.
68+
*
69+
* This method can be used, for example, when implementing a {@see mixed}
70+
* type, where the type receives an arbitrary value that should be
71+
* associated with a specific type.
72+
*
73+
* @throws \Throwable
74+
*/
75+
public function getTypeByValue(mixed $value): TypeInterface
76+
{
77+
return $this->types->getTypeByStatement(
78+
statement: $this->parser->getStatementByDefinition(
79+
definition: $this->extractor->getDefinitionByValue(
80+
value: $value,
81+
),
82+
),
83+
);
84+
}
85+
86+
/**
87+
* Returns the {@see TypeInterface} instance by the type definition string.
88+
*
89+
* @param non-empty-string $definition
90+
*
91+
* @throws \Throwable
92+
*/
93+
public function getTypeByDefinition(#[Language('PHP')] string $definition): TypeInterface
94+
{
95+
return $this->types->getTypeByStatement(
96+
statement: $this->parser->getStatementByDefinition(
97+
definition: $definition,
98+
),
99+
);
100+
}
101+
102+
public function getTypeByStatement(TypeStatement $statement): TypeInterface
103+
{
104+
return $this->types->getTypeByStatement(
105+
statement: $statement,
106+
);
107+
}
108+
}

src/Context/ChildRuntimeContext.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ protected function __construct(
1919
public readonly EntryInterface $entry,
2020
mixed $value,
2121
DirectionInterface $direction,
22-
Configuration $config,
22+
TypeRepositoryInterface $types,
2323
TypeExtractorInterface $extractor,
2424
TypeParserInterface $parser,
25-
TypeRepositoryInterface $types,
25+
Configuration $config,
2626
?Configuration $original = null,
2727
) {
2828
parent::__construct(
2929
value: $value,
3030
direction: $direction,
31-
config: $config,
31+
types: $types,
3232
extractor: $extractor,
3333
parser: $parser,
34-
types: $types,
34+
config: $config,
3535
original: $original,
3636
);
3737
}
Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,13 @@
88
use TypeLang\Mapper\Configuration;
99
use TypeLang\Mapper\Type\Extractor\TypeExtractorInterface;
1010
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
11-
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
12-
use TypeLang\Mapper\Type\TypeInterface;
1311
use TypeLang\Parser\Node\Stmt\TypeStatement;
1412

15-
abstract class Context implements
13+
class MapperContext implements
1614
TypeExtractorInterface,
17-
TypeParserInterface,
18-
TypeRepositoryInterface
15+
TypeParserInterface
1916
{
20-
public function __construct(
21-
/**
22-
* Gets current configuration.
23-
*
24-
* If you need to retrieve configuration's settings, it is recommended
25-
* to use the following methods:
26-
*
27-
* - {@see RuntimeContext::isObjectAsArray()}
28-
* - {@see RuntimeContext::isStrictTypesEnabled()}
29-
*/
30-
public readonly Configuration $config,
17+
protected function __construct(
3118
/**
3219
* Responsible for obtaining the type declaration from its value.
3320
*
@@ -58,25 +45,29 @@ public function __construct(
5845
*/
5946
public readonly TypeParserInterface $parser,
6047
/**
61-
* Responsible for obtaining the type ({@see TypeInterface}) instances
62-
* by the type statement.
63-
*
64-
* This repository belongs to the current context and may differ from
65-
* the initial (mappers) one.
48+
* Gets current configuration.
6649
*
67-
* You can safely use all the methods of this interface, but for ease of
68-
* use, the following methods are available to you:
50+
* If you need to retrieve configuration's settings, it is recommended
51+
* to use the following methods:
6952
*
70-
* - {@see RuntimeContext::getTypeByValue()} - returns type instance by the
71-
* passed value.
72-
* - {@see RuntimeContext::getTypeByDefinition()} - returns type instance by
73-
* the type definition string.
74-
* - {@see RuntimeContext::getTypeByStatement()} - returns type instance by
75-
* the type statement.
53+
* - {@see RuntimeContext::isObjectAsArray()}
54+
* - {@see RuntimeContext::isStrictTypesEnabled()}
7655
*/
77-
public readonly TypeRepositoryInterface $types,
56+
public readonly Configuration $config,
7857
) {}
7958

59+
public static function create(
60+
Configuration $config,
61+
TypeExtractorInterface $extractor,
62+
TypeParserInterface $parser,
63+
): self {
64+
return new self(
65+
extractor: $extractor,
66+
parser: $parser,
67+
config: $config,
68+
);
69+
}
70+
8071
/**
8172
* A more convenient and correct way to get current "object as array"
8273
* configuration value.
@@ -142,47 +133,4 @@ public function getStatementByDefinition(#[Language('PHP')] string $definition):
142133
definition: $definition,
143134
);
144135
}
145-
146-
/**
147-
* Returns the {@see TypeInterface} instance associated with passed value.
148-
*
149-
* This method can be used, for example, when implementing a {@see mixed}
150-
* type, where the type receives an arbitrary value that should be
151-
* associated with a specific type.
152-
*
153-
* @throws \Throwable
154-
*/
155-
public function getTypeByValue(mixed $value): TypeInterface
156-
{
157-
return $this->types->getTypeByStatement(
158-
statement: $this->parser->getStatementByDefinition(
159-
definition: $this->extractor->getDefinitionByValue(
160-
value: $value,
161-
),
162-
),
163-
);
164-
}
165-
166-
/**
167-
* Returns the {@see TypeInterface} instance by the type definition string.
168-
*
169-
* @param non-empty-string $definition
170-
*
171-
* @throws \Throwable
172-
*/
173-
public function getTypeByDefinition(#[Language('PHP')] string $definition): TypeInterface
174-
{
175-
return $this->types->getTypeByStatement(
176-
statement: $this->parser->getStatementByDefinition(
177-
definition: $definition,
178-
),
179-
);
180-
}
181-
182-
public function getTypeByStatement(TypeStatement $statement): TypeInterface
183-
{
184-
return $this->types->getTypeByStatement(
185-
statement: $statement,
186-
);
187-
}
188136
}

src/Context/RootRuntimeContext.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class RootRuntimeContext extends RuntimeContext
1919
{
2020
private PathInterface $path;
2121

22-
public static function create(
22+
public static function createContext(
2323
mixed $value,
2424
DirectionInterface $direction,
2525
Configuration $config,
@@ -36,10 +36,34 @@ public static function create(
3636
return new self(
3737
value: $value,
3838
direction: $direction,
39-
config: $config,
39+
types: $types,
4040
extractor: $extractor,
4141
parser: $parser,
42+
config: $config,
43+
);
44+
}
45+
46+
public static function createFromMapperContext(
47+
MapperContext $context,
48+
mixed $value,
49+
DirectionInterface $direction,
50+
TypeRepositoryInterface $types,
51+
): self {
52+
$config = $context->config;
53+
54+
if (!$config->isStrictTypesOptionDefined()) {
55+
$config = $config->withStrictTypes(
56+
enabled: $direction->isSafeTypes(),
57+
);
58+
}
59+
60+
return new self(
61+
value: $value,
62+
direction: $direction,
4263
types: $types,
64+
extractor: $context->extractor,
65+
parser: $context->parser,
66+
config: $config,
4367
);
4468
}
4569

0 commit comments

Comments
 (0)