Skip to content

Commit 8b8349d

Browse files
committed
Add type specifiers
1 parent 1aabc8e commit 8b8349d

File tree

69 files changed

+1148
-832
lines changed

Some content is hidden

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

69 files changed

+1148
-832
lines changed

example/01.normalization/08.object-output-normalization.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323

2424
$mapper = new Mapper(
2525
config: new Configuration(
26-
isObjectsAsArrays: true,
26+
objectAsArray: true,
2727
),
2828
);
2929

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use TypeLang\Mapper\Context\Context;
66
use TypeLang\Mapper\Exception\Runtime\InvalidValueException;
77
use TypeLang\Mapper\Mapper;
8-
use TypeLang\Mapper\Platform\DelegatePlatform;
98
use TypeLang\Mapper\Platform\StandardPlatform;
109
use TypeLang\Mapper\Type\Builder\SimpleTypeBuilder;
1110
use TypeLang\Mapper\Type\TypeInterface;
@@ -34,12 +33,10 @@ public function cast(mixed $value, Context $context): string
3433
}
3534
}
3635

37-
$mapper = new Mapper(new DelegatePlatform(
38-
// Extend existing platform (StandardPlatform)
39-
delegate: new StandardPlatform(),
36+
$mapper = new Mapper(new StandardPlatform(
4037
types: [
4138
// Additional type
42-
new SimpleTypeBuilder('custom-string', MyNonEmptyStringType::class)
39+
new SimpleTypeBuilder('custom-string', MyNonEmptyStringType::class),
4340
],
4441
));
4542

example/04.mapping-readers/02.attribute-mapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
);
2323

2424
$mapper = new Mapper($platform, new Configuration(
25-
isStrictTypes: false,
25+
strictTypes: false,
2626
));
2727

2828
var_dump($mapper->denormalize([

example/04.mapping-readers/03.phpdoc-mapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333
);
3434

3535
$mapper = new Mapper($platform, new Configuration(
36-
isStrictTypes: false,
36+
strictTypes: false,
3737
));
3838

3939
var_dump($mapper->denormalize([

example/04.mapping-readers/04.array-mapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(
2626
);
2727

2828
$mapper = new Mapper($platform, new Configuration(
29-
isStrictTypes: false,
29+
strictTypes: false,
3030
));
3131

3232
var_dump($mapper->denormalize([

example/04.mapping-readers/05.php-config-mapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
);
2323

2424
$mapper = new Mapper($platform, new Configuration(
25-
isStrictTypes: false,
25+
strictTypes: false,
2626
));
2727

2828
var_dump($mapper->denormalize([

example/04.mapping-readers/06.yaml-config-mapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
);
2323

2424
$mapper = new Mapper($platform, new Configuration(
25-
isStrictTypes: false,
25+
strictTypes: false,
2626
));
2727

2828
var_dump($mapper->denormalize([

example/04.mapping-readers/07.neon-config-mapping.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
);
2323

2424
$mapper = new Mapper($platform, new Configuration(
25-
isStrictTypes: false,
25+
strictTypes: false,
2626
));
2727

2828
var_dump($mapper->denormalize([

src/Configuration.php

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,37 @@
1010
final class Configuration
1111
{
1212
/**
13-
* Default value for {@see $isObjectsAsArrays} option.
13+
* Default value for {@see $objectAsArray} option.
1414
*/
15-
public const OBJECTS_AS_ARRAYS_DEFAULT_VALUE = true;
15+
public const DEFAULT_OBJECT_AS_ARRAY_OPTION = true;
1616

1717
/**
18-
* Default value for {@see $isStrictTypes} option.
18+
* Default value for {@see $strictTypes} option.
1919
*/
20-
public const STRICT_TYPES_DEFAULT_VALUE = true;
20+
public const DEFAULT_STRICT_TYPES_OPTION = true;
21+
22+
/**
23+
* Default value for {@see $typeSpecifiers} option.
24+
*/
25+
public const DEFAULT_TYPE_SPECIFIERS_OPTION = true;
2126

2227
public function __construct(
2328
/**
2429
* If this option contains {@see true}, then objects are converted to
2530
* associative arrays, otherwise anonymous {@see object} will be
2631
* returned.
2732
*/
28-
private readonly ?bool $isObjectsAsArrays = null,
33+
private readonly ?bool $objectAsArray = null,
2934
/**
3035
* If this option contains {@see true}, then strict types will
3136
* be enabled.
3237
*/
33-
private readonly ?bool $isStrictTypes = null,
38+
private readonly ?bool $strictTypes = null,
39+
/**
40+
* If this option contains {@see true}, then type specifiers will
41+
* be applied.
42+
*/
43+
private readonly ?bool $typeSpecifiers = null,
3444
/**
3545
* If this option contains {@see LoggerInterface}, then logger
3646
* will be enabled.
@@ -55,11 +65,12 @@ public function __construct(
5565
*
5666
* @api
5767
*/
58-
public function withObjectsAsArrays(?bool $enabled = null): self
68+
public function withObjectAsArray(?bool $enabled = null): self
5969
{
6070
return new self(
61-
isObjectsAsArrays: $enabled,
62-
isStrictTypes: $this->isStrictTypes,
71+
objectAsArray: $enabled,
72+
strictTypes: $this->strictTypes,
73+
typeSpecifiers: $this->typeSpecifiers,
6374
logger: $this->logger,
6475
tracer: $this->tracer,
6576
);
@@ -73,18 +84,18 @@ public function withObjectsAsArrays(?bool $enabled = null): self
7384
*/
7485
public function isObjectAsArray(): bool
7586
{
76-
return $this->isObjectsAsArrays
77-
?? self::OBJECTS_AS_ARRAYS_DEFAULT_VALUE;
87+
return $this->objectAsArray
88+
?? self::DEFAULT_OBJECT_AS_ARRAY_OPTION;
7889
}
7990

8091
/**
8192
* Returns {@see true} in case option is user-defined.
8293
*
8394
* @api
8495
*/
85-
public function isObjectsAsArraysOptionDefined(): bool
96+
public function isObjectAsArrayOptionDefined(): bool
8697
{
87-
return $this->isObjectsAsArrays !== null;
98+
return $this->objectAsArray !== null;
8899
}
89100

90101
/**
@@ -97,8 +108,9 @@ public function isObjectsAsArraysOptionDefined(): bool
97108
public function withStrictTypes(?bool $enabled = null): self
98109
{
99110
return new self(
100-
isObjectsAsArrays: $this->isObjectsAsArrays,
101-
isStrictTypes: $enabled,
111+
objectAsArray: $this->objectAsArray,
112+
strictTypes: $enabled,
113+
typeSpecifiers: $this->typeSpecifiers,
102114
logger: $this->logger,
103115
tracer: $this->tracer,
104116
);
@@ -113,8 +125,8 @@ public function withStrictTypes(?bool $enabled = null): self
113125
*/
114126
public function isStrictTypesEnabled(): bool
115127
{
116-
return $this->isStrictTypes
117-
?? self::STRICT_TYPES_DEFAULT_VALUE;
128+
return $this->strictTypes
129+
?? self::DEFAULT_STRICT_TYPES_OPTION;
118130
}
119131

120132
/**
@@ -124,7 +136,45 @@ public function isStrictTypesEnabled(): bool
124136
*/
125137
public function isStrictTypesOptionDefined(): bool
126138
{
127-
return $this->isStrictTypes !== null;
139+
return $this->strictTypes !== null;
140+
}
141+
142+
/**
143+
* Enables or disables type specifiers while casting.
144+
*
145+
* In case of $enabled is {@see null} a default value will be defined.
146+
*
147+
* @api
148+
*/
149+
public function withTypeSpecifiers(?bool $enabled = null): self
150+
{
151+
return new self(
152+
objectAsArray: $this->objectAsArray,
153+
strictTypes: $this->strictTypes,
154+
typeSpecifiers: $enabled,
155+
logger: $this->logger,
156+
tracer: $this->tracer,
157+
);
158+
}
159+
160+
/**
161+
* In case of method returns {@see true}, all types will be checked
162+
* for additional assertions.
163+
*/
164+
public function isTypeSpecifiersEnabled(): bool
165+
{
166+
return $this->typeSpecifiers
167+
?? self::DEFAULT_TYPE_SPECIFIERS_OPTION;
168+
}
169+
170+
/**
171+
* Returns {@see true} in case option is user-defined.
172+
*
173+
* @api
174+
*/
175+
public function isTypeSpecifierOptionDefined(): bool
176+
{
177+
return $this->typeSpecifiers !== null;
128178
}
129179

130180
/**
@@ -136,8 +186,9 @@ public function isStrictTypesOptionDefined(): bool
136186
public function withLogger(?LoggerInterface $logger = null): self
137187
{
138188
return new self(
139-
isObjectsAsArrays: $this->isObjectsAsArrays,
140-
isStrictTypes: $this->isStrictTypes,
189+
objectAsArray: $this->objectAsArray,
190+
strictTypes: $this->strictTypes,
191+
typeSpecifiers: $this->typeSpecifiers,
141192
logger: $logger,
142193
tracer: $this->tracer,
143194
);
@@ -162,8 +213,9 @@ public function findLogger(): ?LoggerInterface
162213
public function withTracer(?TracerInterface $tracer = null): self
163214
{
164215
return new self(
165-
isObjectsAsArrays: $this->isObjectsAsArrays,
166-
isStrictTypes: $this->isStrictTypes,
216+
objectAsArray: $this->objectAsArray,
217+
strictTypes: $this->strictTypes,
218+
typeSpecifiers: $this->typeSpecifiers,
167219
logger: $this->logger,
168220
tracer: $tracer,
169221
);

src/Context/ChildContext.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ public function isObjectAsArray(): bool
4949
?? parent::isObjectAsArray();
5050
}
5151

52+
#[\Override]
53+
public function isTypeSpecifiersEnabled(): bool
54+
{
55+
return $this->override?->isTypeSpecifiersEnabled()
56+
?? parent::isTypeSpecifiersEnabled();
57+
}
58+
5259
public function getIterator(): \Traversable
5360
{
5461
yield $current = $this;

0 commit comments

Comments
 (0)