Skip to content

Commit d72dd17

Browse files
authored
chore(mapper): fix MapTo naming collision (#933)
1 parent b9a89de commit d72dd17

21 files changed

+63
-70
lines changed

src/Tempest/Mapper/src/Attributes/MapTo.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Tempest/Mapper/src/Attributes/MapFrom.php renamed to src/Tempest/Mapper/src/MapFrom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Tempest\Mapper\Attributes;
5+
namespace Tempest\Mapper;
66

77
use Attribute;
88

src/Tempest/Mapper/src/MapTo.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
namespace Tempest\Mapper;
66

7-
enum MapTo
7+
use Attribute;
8+
9+
#[Attribute(Attribute::TARGET_PROPERTY)]
10+
final readonly class MapTo
811
{
9-
case JSON;
10-
case ARRAY;
12+
public function __construct(
13+
public string $name,
14+
) {
15+
}
1116
}

src/Tempest/Mapper/src/Mappers/ArrayToJsonMapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
namespace Tempest\Mapper\Mappers;
66

77
use Tempest\Mapper\Mapper;
8-
use Tempest\Mapper\MapTo;
98

109
final readonly class ArrayToJsonMapper implements Mapper
1110
{
1211
public function canMap(mixed $from, mixed $to): bool
1312
{
14-
return $to === MapTo::JSON && is_array($from);
13+
return false;
1514
}
1615

1716
public function map(mixed $from, mixed $to): string

src/Tempest/Mapper/src/Mappers/ArrayToObjectMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Tempest\Mapper\Mappers;
66

7-
use Tempest\Mapper\Attributes\MapFrom;
87
use Tempest\Mapper\Casters\ArrayCaster;
98
use Tempest\Mapper\Casters\CasterFactory;
109
use Tempest\Mapper\Exceptions\MissingValuesException;
10+
use Tempest\Mapper\MapFrom;
1111
use Tempest\Mapper\Mapper;
1212
use Tempest\Mapper\Strict;
1313
use Tempest\Mapper\UnknownValue;

src/Tempest/Mapper/src/Mappers/JsonToArrayMapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{
1212
public function canMap(mixed $from, mixed $to): bool
1313
{
14-
return $to === MapTo::ARRAY && is_string($from) && json_validate($from);
14+
return is_string($from) && json_validate($from);
1515
}
1616

1717
public function map(mixed $from, mixed $to): array

src/Tempest/Mapper/src/Mappers/JsonToObjectMapper.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ public function canMap(mixed $from, mixed $to): bool
3333

3434
public function map(mixed $from, mixed $to): object
3535
{
36-
return map(
37-
map($from)->to(MapTo::ARRAY),
38-
)->to($to);
36+
return map(map($from)->toArray())->to($to);
3937
}
4038
}

src/Tempest/Mapper/src/Mappers/ObjectToArrayMapper.php

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

77
use JsonSerializable;
88
use ReflectionException;
9-
use Tempest\Mapper\Attributes\MapTo as MapToAttribute;
109
use Tempest\Mapper\Mapper;
1110
use Tempest\Mapper\MapTo;
11+
use Tempest\Mapper\MapTo as MapToAttribute;
1212
use Tempest\Reflection\ClassReflector;
1313
use Tempest\Reflection\PropertyReflector;
1414
use function Tempest\Support\arr;
@@ -17,7 +17,7 @@
1717
{
1818
public function canMap(mixed $from, mixed $to): bool
1919
{
20-
return $to === MapTo::ARRAY && is_object($from);
20+
return false;
2121
}
2222

2323
public function map(mixed $from, mixed $to): array

src/Tempest/Mapper/src/Mappers/ObjectToJsonMapper.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212
{
1313
public function canMap(mixed $from, mixed $to): bool
1414
{
15-
return $to === MapTo::JSON && is_object($from);
15+
return false;
1616
}
1717

1818
public function map(mixed $from, mixed $to): string
1919
{
20-
return
21-
map(
22-
map($from)->to(MapTo::ARRAY),
23-
)->to(MapTo::JSON);
20+
return map(map($from)->toArray())->toJson();
2421
}
2522
}

src/Tempest/Mapper/src/ObjectFactory.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use ReflectionException;
99
use Tempest\Container\Container;
1010
use Tempest\Mapper\Exceptions\CannotMapDataException;
11+
use Tempest\Mapper\Mappers\ArrayToJsonMapper;
12+
use Tempest\Mapper\Mappers\JsonToArrayMapper;
13+
use Tempest\Mapper\Mappers\ObjectToArrayMapper;
14+
use Tempest\Mapper\Mappers\ObjectToJsonMapper;
1115
use Tempest\Reflection\FunctionReflector;
1216

1317
/** @template ClassType */
@@ -22,8 +26,7 @@ final class ObjectFactory
2226
public function __construct(
2327
private readonly MapperConfig $config,
2428
private readonly Container $container,
25-
) {
26-
}
29+
) {}
2730

2831
/**
2932
* @template T of object
@@ -68,7 +71,7 @@ public function from(mixed $data): mixed
6871

6972
/**
7073
* @template T of object
71-
* @param T|class-string<T> $to
74+
* @param T|class-string<T>|string $to
7275
* @return T|T[]|mixed
7376
*/
7477
public function to(mixed $to): mixed
@@ -80,9 +83,33 @@ public function to(mixed $to): mixed
8083
);
8184
}
8285

86+
public function toArray(): array
87+
{
88+
if (is_object($this->from)) {
89+
return $this->with(ObjectToArrayMapper::class);
90+
} elseif (is_array($this->from)) {
91+
return $this->from;
92+
} elseif (is_string($this->from) && json_validate($this->from)) {
93+
return $this->with(JsonToArrayMapper::class);
94+
} else {
95+
throw new CannotMapDataException($this->from, 'array');
96+
}
97+
}
98+
99+
public function toJson(): string
100+
{
101+
if (is_object($this->from)) {
102+
return $this->with(ObjectToJsonMapper::class);
103+
} elseif (is_array($this->from)) {
104+
return $this->with(ArrayToJsonMapper::class);
105+
} else {
106+
throw new CannotMapDataException($this->from, 'json');
107+
}
108+
}
109+
83110
/**
84111
* @template T of object
85-
* @param T|class-string<T> $to
112+
* @param T|class-string<T>|string $to
86113
* @return T|mixed
87114
*/
88115
public function map(mixed $from, mixed $to): mixed
@@ -131,7 +158,8 @@ private function mapObject(
131158
mixed $from,
132159
mixed $to,
133160
bool $isCollection,
134-
): mixed {
161+
): mixed
162+
{
135163
if ($isCollection && is_array($from)) {
136164
return array_map(
137165
fn (mixed $item) => $this->mapObject(

0 commit comments

Comments
 (0)