Skip to content

Commit 943907e

Browse files
committed
Rename "context" to "mapping context"
1 parent fdcd65a commit 943907e

62 files changed

Lines changed: 389 additions & 363 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

example/03.types/02.custom-type.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use TypeLang\Mapper\Context\Context;
5+
use TypeLang\Mapper\Context\MappingContext;
66
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
77
use TypeLang\Mapper\Mapper;
88
use TypeLang\Mapper\Platform\StandardPlatform;
@@ -14,12 +14,12 @@
1414
// Add new type (must implement TypeInterface)
1515
class MyNonEmptyStringType implements TypeInterface
1616
{
17-
public function match(mixed $value, Context $context): bool
17+
public function match(mixed $value, MappingContext $context): bool
1818
{
1919
return \is_string($value) && $value !== '';
2020
}
2121

22-
public function cast(mixed $value, Context $context): string
22+
public function cast(mixed $value, MappingContext $context): string
2323
{
2424
if (\is_string($value) && $value !== '') {
2525
return $value;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
use TypeLang\Mapper\Context\Context;
5+
use TypeLang\Mapper\Context\MappingContext;
66
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
77
use TypeLang\Mapper\Mapper;
88
use TypeLang\Mapper\Platform\DelegatePlatform;
@@ -52,12 +52,12 @@ public function __construct(
5252
private readonly TypeInterface $type,
5353
) {}
5454

55-
public function match(mixed $value, Context $context): bool
55+
public function match(mixed $value, MappingContext $context): bool
5656
{
5757
return !empty($value);
5858
}
5959

60-
public function cast(mixed $value, Context $context): mixed
60+
public function cast(mixed $value, MappingContext $context): mixed
6161
{
6262
if (!empty($value)) {
6363
return $this->type->cast($value, $context);

example/03.types/05.custom-type-callable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Psr\Container\ContainerInterface;
6-
use TypeLang\Mapper\Context\Context;
6+
use TypeLang\Mapper\Context\MappingContext;
77
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
88
use TypeLang\Mapper\Mapper;
99
use TypeLang\Mapper\Platform\DelegatePlatform;
@@ -45,12 +45,12 @@ public function has(string $id): bool
4545
// Add new type (must implement TypeInterface)
4646
class MyNonEmptyStringType implements TypeInterface
4747
{
48-
public function match(mixed $value, Context $context): bool
48+
public function match(mixed $value, MappingContext $context): bool
4949
{
5050
return \is_string($value) && $value !== '';
5151
}
5252

53-
public function cast(mixed $value, Context $context): string
53+
public function cast(mixed $value, MappingContext $context): string
5454
{
5555
if (\is_string($value) && $value !== '') {
5656
return $value;

example/03.types/06.custom-type-psr-container.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55
use Psr\Container\ContainerInterface;
6-
use TypeLang\Mapper\Context\Context;
6+
use TypeLang\Mapper\Context\MappingContext;
77
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
88
use TypeLang\Mapper\Mapper;
99
use TypeLang\Mapper\Platform\DelegatePlatform;
@@ -43,12 +43,12 @@ public function has(string $id): bool
4343
// Add new type (must implement TypeInterface)
4444
class MyNonEmptyStringType implements TypeInterface
4545
{
46-
public function match(mixed $value, Context $context): bool
46+
public function match(mixed $value, MappingContext $context): bool
4747
{
4848
return \is_string($value) && $value !== '';
4949
}
5050

51-
public function cast(mixed $value, Context $context): string
51+
public function cast(mixed $value, MappingContext $context): string
5252
{
5353
if (\is_string($value) && $value !== '') {
5454
return $value;

src/Context/BuildContext.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypeLang\Mapper\Context;
6+
7+
final class BuildContext extends Context {}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
1313
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
1414

15-
final class ChildContext extends Context
15+
final class ChildMappingContext extends MappingContext
1616
{
1717
protected function __construct(
18-
public readonly Context $parent,
18+
public readonly MappingContext $parent,
1919
public readonly EntryInterface $entry,
2020
mixed $value,
2121
Direction $direction,

src/Context/Context.php

Lines changed: 10 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,26 @@
66

77
use JetBrains\PhpStorm\Language;
88
use TypeLang\Mapper\Configuration;
9-
use TypeLang\Mapper\Context\Path\Entry\ArrayIndexEntry;
10-
use TypeLang\Mapper\Context\Path\Entry\EntryInterface;
11-
use TypeLang\Mapper\Context\Path\Entry\ObjectEntry;
12-
use TypeLang\Mapper\Context\Path\Entry\ObjectPropertyEntry;
13-
use TypeLang\Mapper\Context\Path\Entry\UnionLeafEntry;
14-
use TypeLang\Mapper\Context\Path\PathInterface;
15-
use TypeLang\Mapper\Type\Coercer\TypeCoercerInterface;
169
use TypeLang\Mapper\Type\Extractor\TypeExtractorInterface;
1710
use TypeLang\Mapper\Type\Parser\TypeParserInterface;
1811
use TypeLang\Mapper\Type\Repository\TypeRepositoryInterface;
1912
use TypeLang\Mapper\Type\TypeInterface;
2013
use TypeLang\Parser\Node\Stmt\TypeStatement;
2114

22-
/**
23-
* @template-implements \IteratorAggregate<array-key, Context>
24-
*/
2515
abstract class Context implements
2616
TypeExtractorInterface,
2717
TypeParserInterface,
28-
TypeRepositoryInterface,
29-
\IteratorAggregate,
30-
\Countable
18+
TypeRepositoryInterface
3119
{
32-
protected function __construct(
33-
/**
34-
* Gets original (unmodified) value.
35-
*
36-
* Please note that the value may be changed during type manipulation
37-
* (casting), for example, using {@see TypeCoercerInterface}.
38-
*
39-
* In this case, the `$value` in the {@see Context} remains the original
40-
* value, without any mutations from type coercions.
41-
*/
42-
public readonly mixed $value,
43-
/**
44-
* Gets data transformation direction.
45-
*/
46-
public readonly Direction $direction,
20+
public function __construct(
4721
/**
4822
* Gets current configuration.
4923
*
50-
* Please note that types can modify configuration settings while
51-
* processing a value and pass this modified configuration down the
52-
* mapping stack.
53-
*
54-
* In this case, the original configuration settings will be located in
55-
* the {@see $original} context's field.
56-
*
57-
* During further processing, the configuration values will be reset to
58-
* their initial configuration values.
59-
*
6024
* If you need to retrieve configuration's settings, it is recommended
6125
* to use the following methods:
6226
*
63-
* - {@see Context::isObjectAsArray()}
64-
* - {@see Context::isStrictTypesEnabled()}
65-
*
66-
* To modify the configuration's settings, it is recommended to use the
67-
* following methods, which guarantees correct operation:
68-
*
69-
* - {@see Context::withObjectAsArray()}
70-
* - {@see Context::withStrictTypes()}
27+
* - {@see MappingContext::isObjectAsArray()}
28+
* - {@see MappingContext::isStrictTypesEnabled()}
7129
*/
7230
public readonly Configuration $config,
7331
/**
@@ -79,7 +37,7 @@ protected function __construct(
7937
* You can safely use all the methods of this interface, but for ease of
8038
* use, the following methods are available to you:
8139
*
82-
* - {@see Context::getDefinitionByValue()} - returns definition string
40+
* - {@see MappingContext::getDefinitionByValue()} - returns definition string
8341
* by the passed value.
8442
*/
8543
public readonly TypeExtractorInterface $extractor,
@@ -93,9 +51,9 @@ protected function __construct(
9351
* You can safely use all the methods of this interface, but for ease of
9452
* use, the following methods are available to you:
9553
*
96-
* - {@see Context::getStatementByValue()} - returns statement node by
54+
* - {@see MappingContext::getStatementByValue()} - returns statement node by
9755
* the value.
98-
* - {@see Context::getStatementByDefinition()} - returns statement node
56+
* - {@see MappingContext::getStatementByDefinition()} - returns statement node
9957
* by the definition string.
10058
*/
10159
public readonly TypeParserInterface $parser,
@@ -109,94 +67,16 @@ protected function __construct(
10967
* You can safely use all the methods of this interface, but for ease of
11068
* use, the following methods are available to you:
11169
*
112-
* - {@see Context::getTypeByValue()} - returns type instance by the
70+
* - {@see MappingContext::getTypeByValue()} - returns type instance by the
11371
* passed value.
114-
* - {@see Context::getTypeByDefinition()} - returns type instance by
72+
* - {@see MappingContext::getTypeByDefinition()} - returns type instance by
11573
* the type definition string.
116-
* - {@see Context::getTypeByStatement()} - returns type instance by
74+
* - {@see MappingContext::getTypeByStatement()} - returns type instance by
11775
* the type statement.
11876
*/
11977
public readonly TypeRepositoryInterface $types,
120-
/**
121-
* Contains a reference to the original config created during this
122-
* context initialization.
123-
*
124-
* If there is no reference ({@see null}), then the current config in
125-
* the {@see $config} context's field is the original.
126-
*/
127-
public readonly ?Configuration $original = null,
12878
) {}
12979

130-
/**
131-
* Creates new child context.
132-
*/
133-
public function enter(mixed $value, EntryInterface $entry, ?Configuration $config = null): self
134-
{
135-
// Original configuration
136-
$original = $this->original ?? $this->config;
137-
138-
// Configuration of the current context
139-
$current = $config ?? $original;
140-
141-
// Do not set "previous" config in case of
142-
// "current" config is original
143-
if ($current === $original) {
144-
$original = null;
145-
}
146-
147-
return new ChildContext(
148-
parent: $this,
149-
entry: $entry,
150-
value: $value,
151-
direction: $this->direction,
152-
config: $current,
153-
extractor: $this->extractor,
154-
parser: $this->parser,
155-
types: $this->types,
156-
original: $current === $original ? null : $original,
157-
);
158-
}
159-
160-
/**
161-
* Creates an "array index" child context
162-
*
163-
* @param array-key $index
164-
*/
165-
public function enterIntoArrayIndex(mixed $value, int|string $index, ?Configuration $config = null): self
166-
{
167-
return $this->enter($value, new ArrayIndexEntry($index), $config);
168-
}
169-
170-
/**
171-
* Creates an "object" child context
172-
*
173-
* @param class-string $class
174-
*/
175-
public function enterIntoObject(mixed $value, string $class, ?Configuration $config = null): self
176-
{
177-
return $this->enter($value, new ObjectEntry($class), $config);
178-
}
179-
180-
/**
181-
* Creates an "object's property" child context
182-
*
183-
* @param non-empty-string $name
184-
*/
185-
public function enterIntoObjectProperty(mixed $value, string $name, ?Configuration $override = null): self
186-
{
187-
return $this->enter($value, new ObjectPropertyEntry($name), $override);
188-
}
189-
190-
/**
191-
* Creates an "union leaf" child context
192-
*
193-
* @param int<0, max> $index
194-
*/
195-
public function enterIntoUnionLeaf(mixed $value, int $index, ?Configuration $override = null): self
196-
{
197-
return $this->enter($value, new UnionLeafEntry($index), $override);
198-
}
199-
20080
/**
20181
* A more convenient and correct way to get current "object as array"
20282
* configuration value.
@@ -210,25 +90,6 @@ public function isObjectAsArray(): bool
21090
return $this->config->isObjectAsArray();
21191
}
21292

213-
/**
214-
* Sets the value of the "object as array" configuration settings using
215-
* the original configuration rules.
216-
*
217-
* Note that the {@see $config} property contains the **current** context
218-
* configuration settings, which may differ from the original ones.
219-
* Therefore, method {@see Context::withObjectAsArray()} is not equivalent
220-
* to calling {@see Configuration::withObjectAsArray()}.
221-
*/
222-
public function withObjectAsArray(?bool $enabled): Configuration
223-
{
224-
if ($enabled === null) {
225-
return $this->original ?? $this->config;
226-
}
227-
228-
return ($this->original ?? $this->config)
229-
->withObjectAsArray($enabled);
230-
}
231-
23293
/**
23394
* A more convenient and correct way to get current "strict types"
23495
* configuration value.
@@ -242,25 +103,6 @@ public function isStrictTypesEnabled(): bool
242103
return $this->config->isStrictTypesEnabled();
243104
}
244105

245-
/**
246-
* Sets the value of the "strict types" configuration settings using
247-
* the original configuration rules.
248-
*
249-
* Note that the {@see $config} property contains the **current** context
250-
* configuration settings, which may differ from the original ones.
251-
* Therefore, method {@see Context::withStrictTypes()} is not equivalent
252-
* to calling {@see Configuration::withStrictTypes()}.
253-
*/
254-
public function withStrictTypes(?bool $enabled): Configuration
255-
{
256-
if ($enabled === null) {
257-
return $this->original ?? $this->config;
258-
}
259-
260-
return ($this->original ?? $this->config)
261-
->withStrictTypes($enabled);
262-
}
263-
264106
public function getDefinitionByValue(mixed $value): string
265107
{
266108
return $this->extractor->getDefinitionByValue($value);
@@ -343,20 +185,4 @@ public function getTypeByStatement(TypeStatement $statement): TypeInterface
343185
statement: $statement,
344186
);
345187
}
346-
347-
/**
348-
* Returns current context path.
349-
*
350-
* The {@see PathInterface} contains all occurrences used in "parent"
351-
* (composite) types when calling {@see enter()} method.
352-
*/
353-
abstract public function getPath(): PathInterface;
354-
355-
/**
356-
* @return int<1, max>
357-
*/
358-
public function count(): int
359-
{
360-
return \max(1, \iterator_count($this));
361-
}
362188
}

0 commit comments

Comments
 (0)