-
Notifications
You must be signed in to change notification settings - Fork 1
/
MovieMapper.php
188 lines (157 loc) · 5.5 KB
/
MovieMapper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php declare (strict_types=1);
namespace ShipMonkTests\InputMapper\Compiler\Mapper\Object\Data;
use ShipMonk\InputMapper\Compiler\Mapper\Object\MapObject;
use ShipMonk\InputMapper\Runtime\Exception\MappingFailedException;
use ShipMonk\InputMapper\Runtime\Mapper;
use ShipMonk\InputMapper\Runtime\MapperProvider;
use ShipMonk\InputMapper\Runtime\Optional;
use ShipMonk\InputMapper\Runtime\OptionalSome;
use function array_diff_key;
use function array_is_list;
use function array_key_exists;
use function array_keys;
use function count;
use function is_array;
use function is_int;
use function is_string;
/**
* Generated mapper by {@see MapObject}. Do not edit directly.
*
* @implements Mapper<MovieInput>
*/
class MovieMapper implements Mapper
{
public function __construct(private readonly MapperProvider $provider)
{
}
/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
public function map(mixed $data, array $path = []): MovieInput
{
if (!is_array($data)) {
throw MappingFailedException::incorrectType($data, $path, 'array');
}
if (!array_key_exists('id', $data)) {
throw MappingFailedException::missingKey($path, 'id');
}
if (!array_key_exists('title', $data)) {
throw MappingFailedException::missingKey($path, 'title');
}
if (!array_key_exists('year', $data)) {
throw MappingFailedException::missingKey($path, 'year');
}
if (!array_key_exists('genres', $data)) {
throw MappingFailedException::missingKey($path, 'genres');
}
if (!array_key_exists('director', $data)) {
throw MappingFailedException::missingKey($path, 'director');
}
if (!array_key_exists('actors', $data)) {
throw MappingFailedException::missingKey($path, 'actors');
}
$knownKeys = ['id' => true, 'title' => true, 'description' => true, 'year' => true, 'genres' => true, 'director' => true, 'actors' => true];
$extraKeys = array_diff_key($data, $knownKeys);
if (count($extraKeys) > 0) {
throw MappingFailedException::extraKeys($path, array_keys($extraKeys));
}
return new MovieInput(
$this->mapId($data['id'], [...$path, 'id']),
$this->mapTitle($data['title'], [...$path, 'title']),
array_key_exists('description', $data) ? $this->mapDescription($data['description'], [...$path, 'description']) : Optional::none($path, 'description'),
$this->mapYear($data['year'], [...$path, 'year']),
$this->mapGenres($data['genres'], [...$path, 'genres']),
$this->mapDirector($data['director'], [...$path, 'director']),
$this->mapActors($data['actors'], [...$path, 'actors']),
);
}
/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
private function mapId(mixed $data, array $path = []): int
{
if (!is_int($data)) {
throw MappingFailedException::incorrectType($data, $path, 'int');
}
return $data;
}
/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
private function mapTitle(mixed $data, array $path = []): string
{
if (!is_string($data)) {
throw MappingFailedException::incorrectType($data, $path, 'string');
}
return $data;
}
/**
* @param list<string|int> $path
* @return OptionalSome<string>
* @throws MappingFailedException
*/
private function mapDescription(mixed $data, array $path = []): OptionalSome
{
if (!is_string($data)) {
throw MappingFailedException::incorrectType($data, $path, 'string');
}
return Optional::of($data);
}
/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
private function mapYear(mixed $data, array $path = []): int
{
if (!is_int($data)) {
throw MappingFailedException::incorrectType($data, $path, 'int');
}
return $data;
}
/**
* @param list<string|int> $path
* @return list<string>
* @throws MappingFailedException
*/
private function mapGenres(mixed $data, array $path = []): array
{
if (!is_array($data) || !array_is_list($data)) {
throw MappingFailedException::incorrectType($data, $path, 'list');
}
$mapped = [];
foreach ($data as $index => $item) {
if (!is_string($item)) {
throw MappingFailedException::incorrectType($item, [...$path, $index], 'string');
}
$mapped[] = $item;
}
return $mapped;
}
/**
* @param list<string|int> $path
* @throws MappingFailedException
*/
private function mapDirector(mixed $data, array $path = []): PersonInput
{
return $this->provider->get(PersonInput::class)->map($data, $path);
}
/**
* @param list<string|int> $path
* @return list<PersonInput>
* @throws MappingFailedException
*/
private function mapActors(mixed $data, array $path = []): array
{
if (!is_array($data) || !array_is_list($data)) {
throw MappingFailedException::incorrectType($data, $path, 'list');
}
$mapped = [];
foreach ($data as $index => $item) {
$mapped[] = $this->provider->get(PersonInput::class)->map($item, [...$path, $index]);
}
return $mapped;
}
}