Skip to content

Commit 8594b21

Browse files
author
Kirill Nesmeyanov
committed
Split MapProperty attribute to MapType + MapName and add SkipWhen attribute + metadata support
1 parent b1b6cb3 commit 8594b21

16 files changed

+161
-65
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,12 @@ Normalization: Transformation from object to raw payload (array).
102102
## Quick Start
103103

104104
```php
105-
use TypeLang\Mapper\Mapping\MapProperty;
105+
use TypeLang\Mapper\Mapping\MapType;
106106

107107
class ExampleObject
108108
{
109109
public function __construct(
110-
#[MapProperty('list<non-empty-string>')]
110+
#[MapType('list<non-empty-string>')]
111111
public readonly array $names,
112112
) {}
113113
}

example/01.normalization/05.typed-object-normalization.php

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

55
use TypeLang\Mapper\Mapper;
6-
use TypeLang\Mapper\Mapping\MapProperty;
6+
use TypeLang\Mapper\Mapping\MapType;
77

88
require __DIR__ . '/../../vendor/autoload.php';
99

@@ -22,7 +22,7 @@ public function __construct(
2222
class ExampleDTO
2323
{
2424
public function __construct(
25-
#[MapProperty('list<ChildDTO>')]
25+
#[MapType('list<ChildDTO>')]
2626
public readonly array $children = [],
2727
) {}
2828
}

example/02.errors/01.errors-purification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use TypeLang\Mapper\Exception\Mapping\MappingExceptionInterface;
66
use TypeLang\Mapper\Mapper;
7-
use TypeLang\Mapper\Mapping\MapProperty;
7+
use TypeLang\Mapper\Mapping\MapType;
88
use TypeLang\Parser\Node\Name;
99

1010
require __DIR__ . '/../../vendor/autoload.php';
1111

1212
class ExampleDTO
1313
{
1414
public function __construct(
15-
#[MapProperty(type: 'list<ExampleDTO>')]
15+
#[MapType(type: 'list<ExampleDTO>')]
1616
public readonly array $values = [],
1717
) {}
1818
}

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

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

55
use TypeLang\Mapper\Mapper;
6-
use TypeLang\Mapper\Mapping\MapProperty;
6+
use TypeLang\Mapper\Mapping\MapType;
77
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
88

99
require __DIR__ . '/../../vendor/autoload.php';
1010

1111
class ExampleDTO
1212
{
1313
public function __construct(
14-
#[MapProperty(type: 'list<ExampleDTO>')]
14+
#[MapType(type: 'list<ExampleDTO>')]
1515
public readonly array $values = [],
1616
) {}
1717
}

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

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

55
use TypeLang\Mapper\Mapper;
6-
use TypeLang\Mapper\Mapping\MapProperty;
6+
use TypeLang\Mapper\Mapping\MapType;
77
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
88
use TypeLang\Parser\Node\Stmt\NamedTypeNode;
99
use TypeLang\Printer\PrettyPrinter;
@@ -13,7 +13,7 @@
1313
class ExampleDTO
1414
{
1515
public function __construct(
16-
#[MapProperty(type: 'list<ExampleDTO>')]
16+
#[MapType(type: 'list<ExampleDTO>')]
1717
public readonly array $values = [],
1818
) {}
1919
}

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

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

55
use TypeLang\Mapper\Mapper;
6-
use TypeLang\Mapper\Mapping\MapProperty;
6+
use TypeLang\Mapper\Mapping\MapType;
77
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
88
use TypeLang\Mapper\Runtime\Value\SimpleValuePrinter;
99

@@ -12,7 +12,7 @@
1212
class ExampleDTO
1313
{
1414
public function __construct(
15-
#[MapProperty(type: 'list<ExampleDTO>')]
15+
#[MapType(type: 'list<ExampleDTO>')]
1616
public readonly array $values = [],
1717
) {}
1818
}

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

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

55
use TypeLang\Mapper\Mapper;
6-
use TypeLang\Mapper\Mapping\MapProperty;
6+
use TypeLang\Mapper\Mapping\MapType;
77
use TypeLang\Mapper\Exception\Mapping\RuntimeException;
88
use TypeLang\Mapper\Runtime\Path\PathInterface;
99
use TypeLang\Mapper\Runtime\Path\PathPrinterInterface;
@@ -13,7 +13,7 @@
1313
class ExampleDTO
1414
{
1515
public function __construct(
16-
#[MapProperty(type: 'list<ExampleDTO>')]
16+
#[MapType(type: 'list<ExampleDTO>')]
1717
public readonly array $values = [],
1818
) {}
1919
}

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

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

55
use TypeLang\Mapper\Mapper;
6-
use TypeLang\Mapper\Mapping\MapProperty;
6+
use TypeLang\Mapper\Mapping\MapType;
77

88
require __DIR__ . '/../../vendor/autoload.php';
99

1010
class ExampleDTO
1111
{
1212
public function __construct(
1313
public readonly int $value1 = 0,
14-
#[MapProperty('int<min, 0>')]
14+
#[MapType('int<min, 0>')]
1515
public readonly int $value2 = 0,
1616
) {}
1717
}

example/04.mapping/03.driver-inheritance-mapping.php

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

55
use TypeLang\Mapper\Mapper;
6-
use TypeLang\Mapper\Mapping\MapProperty;
6+
use TypeLang\Mapper\Mapping\MapType;
77

88
require __DIR__ . '/../../vendor/autoload.php';
99

1010
class ExampleDTO
1111
{
1212
public function __construct(
1313
public readonly int $value1 = 0,
14-
#[MapProperty('int<min, 0>')]
14+
#[MapType('int<min, 0>')]
1515
public readonly int $value2 = 0,
1616
) {}
1717
}

src/Mapping/Driver/AttributeDriver.php

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use TypeLang\Mapper\Exception\Definition\PropertyTypeNotFoundException;
88
use TypeLang\Mapper\Exception\Definition\TypeNotFoundException;
9-
use TypeLang\Mapper\Mapping\MapProperty;
9+
use TypeLang\Mapper\Mapping\MapName;
10+
use TypeLang\Mapper\Mapping\MapType;
1011
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
1112
use TypeLang\Mapper\Mapping\Metadata\TypeMetadata;
13+
use TypeLang\Mapper\Mapping\SkipWhen;
1214
use TypeLang\Mapper\Type\Repository\RepositoryInterface;
1315

1416
final class AttributeDriver extends LoadableDriver
@@ -17,43 +19,68 @@ final class AttributeDriver extends LoadableDriver
1719
protected function load(\ReflectionClass $reflection, ClassMetadata $class, RepositoryInterface $types): void
1820
{
1921
foreach ($reflection->getProperties() as $property) {
20-
$attribute = $this->findPropertyAttribute(
21-
property: $property,
22-
class: MapProperty::class,
23-
);
22+
$metadata = $class->getPropertyOrCreate($property->getName());
23+
24+
// -----------------------------------------------------------------
25+
// Apply property type
26+
// -----------------------------------------------------------------
27+
28+
$attribute = $this->findPropertyAttribute($property, MapType::class);
29+
30+
if ($attribute !== null) {
31+
$type = $this->createType($attribute->type, $property, $types);
2432

25-
if ($attribute === null) {
26-
continue;
33+
$metadata->setTypeInfo($type);
2734
}
2835

29-
$metadata = $class->getPropertyOrCreate($property->getName());
36+
// -----------------------------------------------------------------
37+
// Apply property name
38+
// -----------------------------------------------------------------
39+
40+
$attribute = $this->findPropertyAttribute($property, MapName::class);
3041

31-
if ($attribute->name !== null) {
42+
if ($attribute !== null) {
3243
$metadata->setExportName($attribute->name);
3344
}
3445

35-
if ($attribute->type !== null) {
36-
$statement = $types->parse($attribute->type);
37-
38-
try {
39-
$type = $types->getByStatement($statement, $reflection);
40-
} catch (TypeNotFoundException $e) {
41-
throw PropertyTypeNotFoundException::becauseTypeOfPropertyNotDefined(
42-
class: $class->getName(),
43-
property: $property->getName(),
44-
type: $e->getType(),
45-
previous: $e,
46-
);
47-
}
48-
49-
$metadata->setTypeInfo(new TypeMetadata(
50-
type: $type,
51-
statement: $statement,
52-
));
46+
// -----------------------------------------------------------------
47+
// Apply skip condition
48+
// -----------------------------------------------------------------
49+
50+
$attribute = $this->findPropertyAttribute($property, SkipWhen::class);
51+
52+
if ($attribute !== null) {
53+
$type = $this->createType($attribute->type, $property, $types);
54+
55+
$metadata->setSkipCondition($type);
5356
}
5457
}
5558
}
5659

60+
/**
61+
* @param non-empty-string $type
62+
* @throws PropertyTypeNotFoundException
63+
*/
64+
private function createType(string $type, \ReflectionProperty $property, RepositoryInterface $types): TypeMetadata
65+
{
66+
$statement = $types->parse($type);
67+
68+
$class = $property->getDeclaringClass();
69+
70+
try {
71+
$instance = $types->getByStatement($statement, $class);
72+
} catch (TypeNotFoundException $e) {
73+
throw PropertyTypeNotFoundException::becauseTypeOfPropertyNotDefined(
74+
class: $class->getName(),
75+
property: $property->getName(),
76+
type: $e->getType(),
77+
previous: $e,
78+
);
79+
}
80+
81+
return new TypeMetadata($instance, $statement);
82+
}
83+
5784
/**
5885
* @template TAttribute of object
5986
*

0 commit comments

Comments
 (0)