-
-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modernize generated code for Hydrators #2665
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,8 +167,8 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
$code .= sprintf( | ||
<<<EOF | ||
|
||
/** @AlsoLoad("$name") */ | ||
if (!array_key_exists('%1\$s', \$data) && array_key_exists('$name', \$data)) { | ||
// AlsoLoad("$name") | ||
if (! array_key_exists('%1\$s', \$data) && array_key_exists('$name', \$data)) { | ||
\$data['%1\$s'] = \$data['$name']; | ||
} | ||
|
||
|
@@ -183,7 +183,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
$code .= sprintf( | ||
<<<'EOF' | ||
|
||
/** @Field(type="date") */ | ||
// Field(type: "date") | ||
if (isset($data['%1$s'])) { | ||
$value = $data['%1$s']; | ||
%3$s | ||
|
@@ -201,7 +201,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
$code .= sprintf( | ||
<<<EOF | ||
|
||
/** @Field(type="{$mapping['type']}") */ | ||
// Field(type: "{$mapping['type']}") | ||
if (isset(\$data['%1\$s']) || (! empty(\$this->class->fieldMappings['%2\$s']['nullable']) && array_key_exists('%1\$s', \$data))) { | ||
\$value = \$data['%1\$s']; | ||
if (\$value !== null) { | ||
|
@@ -224,7 +224,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
$code .= sprintf( | ||
<<<'EOF' | ||
|
||
/** @ReferenceOne */ | ||
// ReferenceOne | ||
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) { | ||
$return = $data['%1$s']; | ||
if ($return !== null) { | ||
|
@@ -275,11 +275,11 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
$mappedByMapping = $targetClass->fieldMappings[$mapping['mappedBy']]; | ||
$mappedByFieldName = ClassMetadata::getReferenceFieldName($mappedByMapping['storeAs'], $mapping['mappedBy']); | ||
$criteria = array_merge( | ||
array($mappedByFieldName => $data['_id']), | ||
isset($this->class->fieldMappings['%2$s']['criteria']) ? $this->class->fieldMappings['%2$s']['criteria'] : array() | ||
[$mappedByFieldName => $data['_id']], | ||
$this->class->fieldMappings['%2$s']['criteria'] ?? [] | ||
); | ||
$sort = isset($this->class->fieldMappings['%2$s']['sort']) ? $this->class->fieldMappings['%2$s']['sort'] : array(); | ||
$return = $this->dm->getUnitOfWork()->getDocumentPersister($className)->load($criteria, null, array(), 0, $sort); | ||
$sort = $this->class->fieldMappings['%2$s']['sort'] ?? []; | ||
$return = $this->dm->getUnitOfWork()->getDocumentPersister($className)->load($criteria, null, [], 0, $sort); | ||
$this->class->reflFields['%2$s']->setValue($document, $return); | ||
$hydratedData['%2$s'] = $return; | ||
|
||
|
@@ -293,8 +293,8 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
$code .= sprintf( | ||
<<<'EOF' | ||
|
||
/** @Many */ | ||
$mongoData = isset($data['%1$s']) ? $data['%1$s'] : null; | ||
// ReferenceMany & EmbedMany | ||
$mongoData = $data['%1$s'] ?? null; | ||
|
||
if ($mongoData !== null && ! is_array($mongoData)) { | ||
throw HydratorException::associationTypeMismatch('%3$s', '%1$s', 'array', gettype($mongoData)); | ||
|
@@ -320,7 +320,7 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
$code .= sprintf( | ||
<<<'EOF' | ||
|
||
/** @EmbedOne */ | ||
// EmbedOne | ||
if (isset($data['%1$s']) || (! empty($this->class->fieldMappings['%2$s']['nullable']) && array_key_exists('%1$s', $data))) { | ||
$return = $data['%1$s']; | ||
if ($return !== null) { | ||
|
@@ -370,23 +370,20 @@ private function generateHydratorClass(ClassMetadata $class, string $hydratorCla | |
use Doctrine\ODM\MongoDB\Query\Query; | ||
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; | ||
|
||
use function array_key_exists; | ||
use function gettype; | ||
use function is_array; | ||
Comment on lines
+373
to
+375
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This functions are compiler-optimized. |
||
|
||
/** | ||
* THIS CLASS WAS GENERATED BY THE DOCTRINE ODM. DO NOT EDIT THIS FILE. | ||
*/ | ||
class $hydratorClassName implements HydratorInterface | ||
{ | ||
private \$dm; | ||
private \$class; | ||
|
||
public function __construct(DocumentManager \$dm, ClassMetadata \$class) | ||
{ | ||
\$this->dm = \$dm; | ||
\$this->class = \$class; | ||
} | ||
public function __construct(private DocumentManager \$dm, private ClassMetadata \$class) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding the property type. |
||
|
||
public function hydrate(object \$document, array \$data, array \$hints = array()): array | ||
public function hydrate(object \$document, array \$data, array \$hints = []): array | ||
{ | ||
\$hydratedData = array(); | ||
\$hydratedData = []; | ||
Comment on lines
+384
to
+386
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Short-array syntax. |
||
%s return \$hydratedData; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reference to doctrine annotations is outdated.