Skip to content

Commit c55992c

Browse files
committed
feat(lazy): checking lazy should be optional
1 parent bfa38b0 commit c55992c

3 files changed

Lines changed: 61 additions & 36 deletions

File tree

src/Generator/MapMethodStatementsGenerator.php

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -241,28 +241,49 @@ private function isSourceIsLazyProxy(GeneratorMetadata $metadata): array
241241
]),
242242
];
243243

244-
if (!interface_exists(LazyObjectInterface::class)) {
245-
return $statements;
244+
if (interface_exists(LazyObjectInterface::class)) {
245+
/**
246+
* ```php
247+
* if ($source instanceof LazyObjectInterface) {
248+
* $source->initializeLazyObject();
249+
* } else {
250+
* ...
251+
* }
252+
* ```.
253+
*/
254+
$statements = [
255+
new Stmt\If_(new Expr\Instanceof_($metadata->variableRegistry->getSourceInput(), new Name\FullyQualified(LazyObjectInterface::class)), [
256+
'stmts' => [
257+
new Stmt\Expression(
258+
new Expr\MethodCall($metadata->variableRegistry->getSourceInput(), 'initializeLazyObject')
259+
),
260+
],
261+
'else' => new Stmt\Else_($statements),
262+
]),
263+
];
246264
}
247265

248266
/**
249-
* ```php
250-
* if ($source instanceof LazyObjectInterface) {
251-
* $source->initializeLazyObject();
252-
* } else {
253-
* ...
254-
* }
255-
* ```.
267+
* ```php
268+
* if ($context[MapperContext::INITIALIZE_LAZY_OBJECT] ?? false) {
269+
* ...
270+
* }
271+
* ```.
256272
*/
273+
257274
return [
258-
new Stmt\If_(new Expr\Instanceof_($metadata->variableRegistry->getSourceInput(), new Name\FullyQualified(LazyObjectInterface::class)), [
259-
'stmts' => [
260-
new Stmt\Expression(
261-
new Expr\MethodCall($metadata->variableRegistry->getSourceInput(), 'initializeLazyObject')
275+
new Stmt\If_(
276+
new Expr\BinaryOp\Coalesce(
277+
new Expr\ArrayDimFetch(
278+
$metadata->variableRegistry->getContext(),
279+
new Scalar\String_(MapperContext::INITIALIZE_LAZY_OBJECT)
262280
),
263-
],
264-
'else' => new Stmt\Else_($statements),
265-
]),
281+
new Expr\ConstFetch(new Name('false'))
282+
),
283+
[
284+
'stmts' => $statements,
285+
]
286+
),
266287
];
267288
}
268289

src/MapperContext.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,33 @@
3333
* "datetime_format"?: string,
3434
* "datetime_force_timezone"?: string,
3535
* "map_to_accessor_parameter"?: array<string, string>,
36-
* "normalizer_format"?: string
36+
* "normalizer_format"?: string,
37+
* "initialize_lazy_object"?: bool,
38+
* "lazy_mapping"?: bool,
3739
* }
3840
*/
3941
class MapperContext
4042
{
41-
public const GROUPS = 'groups';
42-
public const ALLOWED_ATTRIBUTES = 'allowed_attributes';
43-
public const IGNORED_ATTRIBUTES = 'ignored_attributes';
44-
public const CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
45-
public const CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler';
46-
public const CIRCULAR_REFERENCE_REGISTRY = 'circular_reference_registry';
47-
public const CIRCULAR_COUNT_REFERENCE_REGISTRY = 'circular_count_reference_registry';
48-
public const DEPTH = 'depth';
49-
public const TARGET_TO_POPULATE = 'target_to_populate';
50-
public const DEEP_TARGET_TO_POPULATE = 'deep_target_to_populate';
51-
public const CONSTRUCTOR_ARGUMENTS = 'constructor_arguments';
52-
public const SKIP_NULL_VALUES = 'skip_null_values';
53-
public const SKIP_UNINITIALIZED_VALUES = 'skip_uninitialized_values';
54-
public const ALLOW_READONLY_TARGET_TO_POPULATE = 'allow_readonly_target_to_populate';
55-
public const DATETIME_FORMAT = 'datetime_format';
56-
public const DATETIME_FORCE_TIMEZONE = 'datetime_force_timezone';
57-
public const MAP_TO_ACCESSOR_PARAMETER = 'map_to_accessor_parameter';
58-
public const NORMALIZER_FORMAT = 'normalizer_format';
59-
public const LAZY_MAPPING = 'lazy_mapping';
43+
public const string GROUPS = 'groups';
44+
public const string ALLOWED_ATTRIBUTES = 'allowed_attributes';
45+
public const string IGNORED_ATTRIBUTES = 'ignored_attributes';
46+
public const string CIRCULAR_REFERENCE_LIMIT = 'circular_reference_limit';
47+
public const string CIRCULAR_REFERENCE_HANDLER = 'circular_reference_handler';
48+
public const string CIRCULAR_REFERENCE_REGISTRY = 'circular_reference_registry';
49+
public const string CIRCULAR_COUNT_REFERENCE_REGISTRY = 'circular_count_reference_registry';
50+
public const string DEPTH = 'depth';
51+
public const string TARGET_TO_POPULATE = 'target_to_populate';
52+
public const string DEEP_TARGET_TO_POPULATE = 'deep_target_to_populate';
53+
public const string CONSTRUCTOR_ARGUMENTS = 'constructor_arguments';
54+
public const string SKIP_NULL_VALUES = 'skip_null_values';
55+
public const string SKIP_UNINITIALIZED_VALUES = 'skip_uninitialized_values';
56+
public const string ALLOW_READONLY_TARGET_TO_POPULATE = 'allow_readonly_target_to_populate';
57+
public const string DATETIME_FORMAT = 'datetime_format';
58+
public const string DATETIME_FORCE_TIMEZONE = 'datetime_force_timezone';
59+
public const string MAP_TO_ACCESSOR_PARAMETER = 'map_to_accessor_parameter';
60+
public const string NORMALIZER_FORMAT = 'normalizer_format';
61+
public const string INITIALIZE_LAZY_OBJECT = 'initialize_lazy_object';
62+
public const string LAZY_MAPPING = 'lazy_mapping';
6063

6164
/** @var MapperContextArray */
6265
private array $context = [

src/ObjectMapper/ObjectMapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function map(object $source, object|string|null $target = null): object
6868

6969
return $this->autoMapper->map($source, $target, [
7070
MapperContext::SKIP_UNINITIALIZED_VALUES => true,
71+
MapperContext::INITIALIZE_LAZY_OBJECT => true,
7172
]);
7273
}
7374

0 commit comments

Comments
 (0)