Skip to content

Commit 4db08a6

Browse files
authored
Don't explicitly set default value (#16)
1 parent 7d229ff commit 4db08a6

File tree

2 files changed

+5
-15
lines changed

2 files changed

+5
-15
lines changed

readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ class UserDto
133133
function jmapper_8cf8f45dc33c7f58ab728699ac3ebec3(Jerodev\DataMapper\Mapper $mapper, array $data)
134134
{
135135
$x = new Jerodev\DataMapper\Tests\_Mocks\UserDto((string) $data['name']);
136-
$x->name = (string) $data['name'];
137136
$x->friends = (\array_key_exists('friends', $data) ? \array_map(static fn ($x6462755ab00b1) => $mapper->map('Jerodev\DataMapper\Tests\_Mocks\UserDto', $x6462755ab00b1), $data['friends']) : []);
138137
$x->favoriteSuit = (\array_key_exists('favoriteSuit', $data) ? Jerodev\DataMapper\Tests\_Mocks\SuitEnum::from($data['favoriteSuit']) : NULL);
139138

src/Objects/ObjectMapper.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,22 +209,13 @@ public function __destruct()
209209
/** @param array{type: DataTypeCollection, default?: mixed} $property */
210210
private function buildPropertyForeachMapping(string $propertyName, array $property, ClassBluePrint $blueprint): string
211211
{
212-
$canHaveDefault = \array_key_exists('default', $property);
213-
214-
$foreach = \PHP_EOL . \str_repeat(' ', $canHaveDefault ? 3 : 2) . '$x->' . $propertyName . ' = [];';
215-
$foreach .= \PHP_EOL . \str_repeat(' ', $canHaveDefault ? 3 : 2) . 'foreach ($data[\'' . $propertyName . '\'] as $key => $value) {';
216-
$foreach .= \PHP_EOL . \str_repeat(' ', $canHaveDefault ? 4 : 3) . '$x->' . $propertyName . '[' . $this->castInMapperFunction('$key', $property['type']->types[0]->genericTypes[0], $blueprint) . '] = ';
212+
$foreach = \PHP_EOL . \str_repeat(' ', 2) . '$x->' . $propertyName . ' = [];';
213+
$foreach .= \PHP_EOL . \str_repeat(' ', 2) . 'foreach ($data[\'' . $propertyName . '\'] as $key => $value) {';
214+
$foreach .= \PHP_EOL . \str_repeat(' ', 3) . '$x->' . $propertyName . '[' . $this->castInMapperFunction('$key', $property['type']->types[0]->genericTypes[0], $blueprint) . '] = ';
217215
$foreach .= $this->castInMapperFunction('$value', $property['type']->types[0]->genericTypes[1], $blueprint) . ';';
218-
$foreach .= \PHP_EOL . \str_repeat(' ', $canHaveDefault ? 3 : 2) . '}';
219-
220-
if ($canHaveDefault) {
221-
$foreach = \PHP_EOL . \str_repeat(' ', 2) . 'if (\\array_key_exists(\'' . $propertyName . '\', $data)) {' . $foreach;
222-
$foreach .= \PHP_EOL . \str_repeat(' ', 2) . '} else {';
223-
$foreach .= \PHP_EOL . \str_repeat(' ', 3) . '$x->' . $propertyName . ' = ' . \var_export($property['default'], true) . ';';
224-
$foreach .= \PHP_EOL . \str_repeat(' ', 2) . '}';
225-
}
216+
$foreach .= \PHP_EOL . \str_repeat(' ', 2) . '}';
226217

227-
if ($this->mapper->config->allowUninitializedFields && ! \array_key_exists('default', $property)) {
218+
if (\array_key_exists('default', $property) || $this->mapper->config->allowUninitializedFields) {
228219
$foreach = $this->wrapArrayKeyExists($foreach, $propertyName);
229220
}
230221

0 commit comments

Comments
 (0)