Skip to content

Commit a9821f2

Browse files
committed
Simplify context
1 parent 36666da commit a9821f2

File tree

5 files changed

+37
-43
lines changed

5 files changed

+37
-43
lines changed

src/Context/ChildContext.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ final class ChildContext extends Context
1616
{
1717
protected function __construct(
1818
public readonly Context $parent,
19-
private readonly EntryInterface $entry,
19+
public readonly EntryInterface $entry,
2020
mixed $value,
2121
Direction $direction,
2222
Configuration $config,
2323
TypeExtractorInterface $extractor,
2424
TypeParserInterface $parser,
2525
TypeRepositoryInterface $types,
26-
private readonly ?bool $overrideStrictTypes = null,
27-
private readonly ?bool $overrideObjectAsArray = null,
26+
public readonly ?Configuration $override = null,
2827
) {
2928
parent::__construct(
3029
value: $value,
@@ -39,31 +38,18 @@ protected function __construct(
3938
#[\Override]
4039
public function isStrictTypesEnabled(): bool
4140
{
42-
return $this->overrideStrictTypes
41+
return $this->override?->isStrictTypesEnabled()
4342
?? parent::isStrictTypesEnabled();
4443
}
4544

4645
#[\Override]
4746
public function isObjectAsArray(): bool
4847
{
49-
return $this->overrideObjectAsArray
48+
return $this->override?->isObjectAsArray()
5049
?? parent::isObjectAsArray();
5150
}
5251

53-
/**
54-
* Gets parent context
55-
*
56-
* @api
57-
*/
58-
public function getParent(): Context
59-
{
60-
return $this->parent;
61-
}
62-
63-
/**
64-
* @return iterable<array-key, Context>
65-
*/
66-
private function getStack(): iterable
52+
public function getIterator(): \Traversable
6753
{
6854
yield $current = $this;
6955

@@ -76,7 +62,7 @@ public function getPath(): PathInterface
7662
{
7763
$entries = [];
7864

79-
foreach ($this->getStack() as $context) {
65+
foreach ($this as $context) {
8066
if ($context instanceof self) {
8167
$entries[] = $context->entry;
8268
}

src/Context/Context.php

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,30 @@
1414
use TypeLang\Mapper\Type\TypeInterface;
1515
use TypeLang\Parser\Node\Stmt\TypeStatement;
1616

17+
/**
18+
* @template-implements \IteratorAggregate<array-key, Context>
19+
*/
1720
abstract class Context implements
1821
TypeExtractorInterface,
1922
TypeParserInterface,
20-
TypeRepositoryInterface
23+
TypeRepositoryInterface,
24+
\IteratorAggregate,
25+
\Countable
2126
{
2227
protected function __construct(
23-
protected readonly mixed $value,
24-
protected readonly Direction $direction,
25-
protected readonly Configuration $config,
26-
protected readonly TypeExtractorInterface $extractor,
27-
protected readonly TypeParserInterface $parser,
28-
protected readonly TypeRepositoryInterface $types,
28+
public readonly mixed $value,
29+
public readonly Direction $direction,
30+
public readonly Configuration $config,
31+
public readonly TypeExtractorInterface $extractor,
32+
public readonly TypeParserInterface $parser,
33+
public readonly TypeRepositoryInterface $types,
2934
) {}
3035

3136
/**
3237
* Creates new child context.
3338
*/
34-
public function enter(
35-
mixed $value,
36-
EntryInterface $entry,
37-
?bool $strictTypes = null,
38-
?bool $objectAsArray = null,
39-
): self {
39+
public function enter(mixed $value, EntryInterface $entry, ?Configuration $override = null): self
40+
{
4041
return new ChildContext(
4142
parent: $this,
4243
entry: $entry,
@@ -46,16 +47,10 @@ public function enter(
4647
extractor: $this->extractor,
4748
parser: $this->parser,
4849
types: $this->types,
49-
overrideStrictTypes: $strictTypes,
50-
overrideObjectAsArray: $objectAsArray,
50+
override: $override,
5151
);
5252
}
5353

54-
public function getValue(): mixed
55-
{
56-
return $this->value;
57-
}
58-
5954
public function isObjectAsArray(): bool
6055
{
6156
return $this->config->isObjectAsArray();
@@ -127,7 +122,15 @@ public function getStatementByValue(mixed $value): TypeStatement
127122
public function getTypeByValue(mixed $value): TypeInterface
128123
{
129124
return $this->types->getTypeByStatement(
130-
statement: $this->getStatementByValue($value)
125+
statement: $this->getStatementByValue($value),
131126
);
132127
}
128+
129+
/**
130+
* @return int<1, max>
131+
*/
132+
public function count(): int
133+
{
134+
return \iterator_count($this);
135+
}
133136
}

src/Context/RootContext.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,9 @@ public function getPath(): PathInterface
7171
{
7272
return $this->path ??= new Path();
7373
}
74+
75+
public function getIterator(): \Traversable
76+
{
77+
yield $this;
78+
}
7479
}

src/Type/ClassTypeFromArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private function denormalizeObject(array $value, object $object, Context $contex
121121
$entrance = $context->enter(
122122
value: $value,
123123
entry: new ObjectPropertyEntry($meta->alias),
124-
strictTypes: $meta->write->strict,
124+
override: $context->config->withStrictTypes($meta->write->strict)
125125
);
126126

127127
// Skip the property when not writable

src/Type/ClassTypeToArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function normalizeObject(object $object, Context $context): array
7777
$entrance = $context->enter(
7878
value: $object,
7979
entry: new ObjectPropertyEntry($meta->name),
80-
strictTypes: $meta->read->strict,
80+
override: $context->config->withStrictTypes($meta->read->strict),
8181
);
8282

8383
// Skip the property when not readable

0 commit comments

Comments
 (0)