Skip to content

Commit 829c46d

Browse files
authored
Merge pull request #5 from php-type-language/feature/second-level-meta
Added second level metadata
2 parents 9915e42 + 5387d90 commit 829c46d

File tree

263 files changed

+5473
-3932
lines changed

Some content is hidden

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

263 files changed

+5473
-3932
lines changed

bench/src/Serializers/TypeLangAttributesBench.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
use PhpBench\Attributes\Warmup;
1111
use TypeLang\Mapper\Bench\Stub\ExampleRequestDTO;
1212
use TypeLang\Mapper\Mapper;
13-
use TypeLang\Mapper\Mapping\Driver\AttributeDriver;
14-
use TypeLang\Mapper\Mapping\Driver\Psr16CachedDriver;
15-
use TypeLang\Mapper\Mapping\Driver\ReflectionDriver;
13+
use TypeLang\Mapper\Mapping\Provider\Psr16CacheProvider;
14+
use TypeLang\Mapper\Mapping\Reader\AttributeReader;
1615
use TypeLang\Mapper\Platform\StandardPlatform;
1716

1817
#[Revs(30), Warmup(3), Iterations(5), BeforeMethods('prepare')]
@@ -25,22 +24,20 @@ public function prepare(): void
2524
{
2625
parent::prepare();
2726

28-
$driver = new AttributeDriver(
29-
delegate: new ReflectionDriver(),
30-
);
27+
$driver = new AttributeReader();
3128

3229
$this->cached = new Mapper(
3330
platform: new StandardPlatform(
34-
driver: new Psr16CachedDriver(
35-
cache: $this->psr16,
31+
meta: new Psr16CacheProvider(
32+
psr16: $this->psr16,
3633
delegate: $driver,
3734
),
3835
),
3936
);
4037

4138
$this->raw = new Mapper(
4239
platform: new StandardPlatform(
43-
driver: $driver,
40+
meta: $driver,
4441
),
4542
);
4643
}

bench/src/Serializers/TypeLangDocBlockBench.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
use PhpBench\Attributes\Warmup;
1111
use TypeLang\Mapper\Bench\Stub\ExampleRequestDTO;
1212
use TypeLang\Mapper\Mapper;
13-
use TypeLang\Mapper\Mapping\Driver\DocBlockDriver;
14-
use TypeLang\Mapper\Mapping\Driver\Psr16CachedDriver;
15-
use TypeLang\Mapper\Mapping\Driver\ReflectionDriver;
13+
use TypeLang\Mapper\Mapping\Provider\Psr16CacheProvider;
14+
use TypeLang\Mapper\Mapping\Reader\PhpDocReader;
15+
use TypeLang\Mapper\Mapping\Reader\ReflectionReader;
1616
use TypeLang\Mapper\Platform\StandardPlatform;
1717

1818
#[Revs(30), Warmup(3), Iterations(5), BeforeMethods('prepare')]
@@ -25,22 +25,20 @@ public function prepare(): void
2525
{
2626
parent::prepare();
2727

28-
$driver = new DocBlockDriver(
29-
delegate: new ReflectionDriver(),
30-
);
28+
$driver = new PhpDocReader();
3129

3230
$this->cached = new Mapper(
3331
platform: new StandardPlatform(
34-
driver: new Psr16CachedDriver(
35-
cache: $this->psr16,
32+
meta: new Psr16CacheProvider(
33+
psr16: $this->psr16,
3634
delegate: $driver,
3735
),
3836
),
3937
);
4038

4139
$this->raw = new Mapper(
4240
platform: new StandardPlatform(
43-
driver: $driver,
41+
meta: $driver,
4442
),
4543
);
4644
}

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
},
1111
"require": {
1212
"php": "^8.1",
13+
"psr/clock": "^1.0",
1314
"psr/log": "^1.0|^2.0|^3.0",
1415
"psr/simple-cache": "^1.0|^2.0|^3.0",
1516
"type-lang/parser": "^1.4",
@@ -33,6 +34,8 @@
3334
"symfony/property-access": "^5.4|^6.0|^7.0",
3435
"symfony/stopwatch": "^5.4|^6.0|^7.0",
3536
"symfony/var-dumper": "^5.4|^6.0|^7.0",
37+
"symfony/yaml": "^5.4|^6.0|^7.0",
38+
"nette/neon": "^3.0",
3639
"type-lang/phpdoc": "^1.0",
3740
"type-lang/phpdoc-standard-tags": "^1.0"
3841
},
@@ -42,8 +45,11 @@
4245
}
4346
},
4447
"suggest": {
45-
"type-lang/phpdoc-standard-tags": "(^1.0) Required for DocBlockDriver mapping driver support",
46-
"justinrainbow/json-schema": "(^5.3|^6.0) Required for configuration drivers validation"
48+
"ext-json": "Required for JSON mapping configuration files",
49+
"nette/neon": "(^3.0) Required for NEON mapping configuration files",
50+
"symfony/yaml": "(^5.4|^6.0|^7.0) Required for YAML mapping configuration files",
51+
"type-lang/phpdoc-standard-tags": "(^1.0) Required for PhpDoc mapping configuration",
52+
"justinrainbow/json-schema": "(^5.3|^6.0) Required for file-based configuration validation"
4753
},
4854
"extra": {
4955
"branch-alias": {

example/01.normalization/06.inherited-types-normalization.php

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

3131
$result = $mapper->normalize($value);
3232

33-
dd($result);
33+
var_dump($result);
3434
//
3535
// array:1 [
3636
// "items" => array:3 [

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ public function __construct(
3737

3838
var_dump($result);
3939
//
40-
// object{
41-
// items: array:3 [
42-
// "key1" => object{
43-
// value: "first"
44-
// }
45-
// "key2" => object{
46-
// value: "second"
47-
// }
48-
// 0 => object{
49-
// value: "third"
50-
// }
40+
// array:1 [
41+
// "items" => array:3 [
42+
// "key1" => array:1 [
43+
// "value" => "first"
44+
// ]
45+
// "key2" => array:1 [
46+
// "value" => "second"
47+
// ]
48+
// 0 => array:1 [
49+
// "value" => "third"
50+
// ]
5151
// ]
52-
// }
52+
// ]
5353
//

example/02.errors/01.custom-type-printer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,15 @@ public function __construct(
2626
]
2727
], ExampleDTO::class);
2828
} catch (RuntimeException $e) {
29-
// Before
3029
var_dump($e->getMessage());
31-
// - Type: "list<ExampleDTO>"
32-
// - Message: Passed value in "values" of {"values": 42} must be of type
33-
// list<ExampleDTO>, but 42 given at $.values[1].values
3430

3531
// Replace all "expected type" definition to PHP-supported printer
3632
// instead of PrettyPrinter
3733
$e->template->types = new \TypeLang\Printer\NativeTypePrinter();
3834

39-
// After
35+
// Before: Passed value in "values" of {"values": 42} must be of type
36+
// list<ExampleDTO>, but 42 given at $.values[1].values
37+
// After: Passed value in "values" of {"values": 42} must be of type
38+
// array, but 42 given at $.values[1].values
4039
var_dump($e->getMessage());
41-
// - Type: "array"
42-
// - Message: Passed value in "values" of {"values": 42} must be of type
43-
// array, but 42 given at $.values[1].values
4440
}

example/02.errors/02.extended-type-printer.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ public function __construct(
2828
],
2929
], ExampleDTO::class);
3030
} catch (RuntimeException $e) {
31-
// Before
3231
var_dump($e->getMessage());
33-
// - Type: "list<ExampleDTO>"
34-
// - Message: Passed value in "values" of {"values": 42} must be of type
35-
// list<ExampleDTO>, but 42 given at $.values[1].values
3632

3733
// Print all NamedTypeNode AST statements as "!!!MODIFIED!!!" string
3834
$e->template->types = new class extends PrettyPrinter {
@@ -42,9 +38,9 @@ protected function printNamedTypeNode(NamedTypeNode $node): string
4238
}
4339
};
4440

45-
// After
41+
// Before: Passed value in "values" of {"values": 42} must be of type
42+
// list<ExampleDTO>, but 42 given at $.values[1].values
43+
// After: Passed value in "values" of {"values": 42} must be of type
44+
// !!!MODIFIED!!!, but 42 given at $.values[1].values
4645
var_dump($e->getMessage());
47-
// - Type: "!!!MODIFIED!!!"
48-
// - Message: Passed value in "values" of {"values": 42} must be of type
49-
// !!!MODIFIED!!!, but 42 given at $.values[1].values
5046
}

example/02.errors/03.custom-value-printer.php

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,30 @@ public function __construct(
2828
],
2929
], ExampleDTO::class);
3030
} catch (RuntimeException $e) {
31-
// Before
3231
var_dump($e->getMessage());
33-
// - Value#1: "of {"values": 42}"
34-
// - Value#2: "but 42 given"
35-
// - Message: Passed value in "values" of {"values": 42} must be of type
36-
// list<ExampleDTO>, but 42 given at $.values[1].values
3732

3833
// Print all values using PHP-compatible types
3934
$e->template->values = new PHPValuePrinter();
4035

41-
// After#1
36+
// Before: Passed value in "values" of {"values": 42} must be of type
37+
// list<ExampleDTO>, but 42 given at $.values[1].values
38+
// After#1: Passed value in string of stdClass must be of type
39+
// list<ExampleDTO>, but int given at $.values[1].values
4240
var_dump($e->getMessage());
43-
// - Value#1: "of array"
44-
// - Value#2: "but int given"
45-
// - Message: Passed value in "values" of array must be of type
46-
// list<ExampleDTO>, but int given at $.values[1].values
4741

4842

4943
// In case of symfony/var-dumper is installed, we can use it
5044
if (\Composer\InstalledVersions::isInstalled('symfony/var-dumper')) {
5145
// Print all values using SymfonyValuePrinter
5246
$e->template->values = new SymfonyValuePrinter();
5347

54-
// After#2
48+
// Before: Passed value in "values" of {"values": 42} must be of type
49+
// list<ExampleDTO>, but 42 given at $.values[1].values
50+
// After#1: Passed value in string of stdClass must be of type
51+
// list<ExampleDTO>, but int given at $.values[1].values
52+
// After#2: Passed value in "values" of {#394
53+
// +"values": 42
54+
// } must be of type list<ExampleDTO>, but 42 given at $.values[1].values
5555
var_dump($e->getMessage());
56-
// - Value#1: "of array:1 [
57-
// "values" => 42
58-
// ]"
59-
// - Value#2: "but 42 given"
60-
// - Message: Passed value in "values" of array:1 [
61-
// "values" => 42
62-
// ] must be of type list<ExampleDTO>, but 42 given at $.values[1].values
6356
}
6457
}

example/02.errors/04.custom-path-printer.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@ public function __construct(
2828
],
2929
], ExampleDTO::class);
3030
} catch (RuntimeException $e) {
31-
// Before: "at $.values[1].values"
3231
var_dump($e->getMessage());
33-
// Passed value of field "values" must be of type list<ExampleDTO>,
34-
// but 42 given at $.values[1].values
3532

3633
// Print full path using ">" delimiter
3734
$e->template->paths = new class implements PathPrinterInterface {
@@ -41,8 +38,10 @@ public function print(PathInterface $path): string
4138
}
4239
};
4340

44-
// After: "at ExampleDTO > values > 1 > ExampleDTO > values"
41+
// Before: Passed value in "values" of {"values": 42} must be of type
42+
// list<ExampleDTO>, but 42 given at $.values[1].values
43+
// After: Passed value in "values" of {"values": 42} must be of type
44+
// list<ExampleDTO>, but 42 given at ExampleDTO > values >
45+
// 1 > ExampleDTO > values
4546
var_dump($e->getMessage());
46-
// Passed value of field "values" must be of type list<ExampleDTO>,
47-
// but 42 given at ExampleDTO > values > 1 > ExampleDTO > values
4847
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,30 @@ public function cast(mixed $value, Context $context): mixed
8787
// ]
8888
//
8989

90-
var_dump($mapper->normalize([], 'non-empty<string>'));
90+
try {
91+
var_dump($mapper->normalize([], 'non-empty<string>'));
92+
} catch (\Throwable $e) {
93+
echo $e->getMessage() . "\n";
94+
}
9195
//
9296
// InvalidValueException: Passed value [] is invalid
9397
//
9498

95-
96-
var_dump($mapper->normalize('example', 'non-empty'));
99+
try {
100+
var_dump($mapper->normalize('example', 'non-empty'));
101+
} catch (\Throwable $e) {
102+
echo $e->getMessage() . "\n";
103+
}
97104
//
98105
// MissingTemplateArgumentsException: Type "non-empty" expects at least 1
99106
// template argument(s), but 0 were passed
100107
//
101108

102-
var_dump($mapper->normalize('', 'non-empty<string>'));
109+
try {
110+
var_dump($mapper->normalize('', 'non-empty<string>'));
111+
} catch (\Throwable $e) {
112+
echo $e->getMessage() . "\n";
113+
}
103114
//
104115
// InvalidValueException: Passed value "" is invalid
105116
//

0 commit comments

Comments
 (0)