Skip to content

Commit 8710027

Browse files
committed
feat(MapperOptions): Add context object to provide mapping options.
1 parent 17327e3 commit 8710027

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* refactor(`Context`): Returns null instead of throwing exception if the
66
member is not found.
77
* refactor(`Context`): Context is now `Traversable`.
8+
* feat(`MapperOptions`): Add context object to provide mapping options.
89

910
## 0.7.3
1011

src/Context/MapperOptions.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of rekalogika/mapper package.
7+
*
8+
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
9+
*
10+
* For the full copyright and license information, please view the LICENSE file
11+
* that was distributed with this source code.
12+
*/
13+
14+
namespace Rekalogika\Mapper\Context;
15+
16+
/**
17+
* @immutable
18+
*/
19+
final readonly class MapperOptions
20+
{
21+
public int $manualGcInterval;
22+
23+
/**
24+
* @param boolean $enableLazyLoading Enable or disable lazy loading.
25+
* @param boolean $enableTargetValueReading If disabled, values on the target side will not be read, and assumed to be null.
26+
* @param integer|null $manualGcInterval Performs garbage collection manually every n `MainTransformer` invocation. If not provided, and lazy loading is active, it is automatically set to 1000 to fix memory leak.
27+
*/
28+
public function __construct(
29+
public bool $enableLazyLoading = true,
30+
public bool $enableTargetValueReading = true,
31+
?int $manualGcInterval = null,
32+
) {
33+
if ($manualGcInterval === null) {
34+
if ($enableLazyLoading) {
35+
$this->manualGcInterval = 1000;
36+
} else {
37+
$this->manualGcInterval = 0;
38+
}
39+
} else {
40+
$this->manualGcInterval = $manualGcInterval;
41+
}
42+
}
43+
}

src/MainTransformer/MainTransformer.php

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace Rekalogika\Mapper\MainTransformer;
1515

1616
use Rekalogika\Mapper\Context\Context;
17+
use Rekalogika\Mapper\Context\MapperOptions;
1718
use Rekalogika\Mapper\MainTransformer\Exception\CannotFindTransformerException;
1819
use Rekalogika\Mapper\MainTransformer\Exception\CircularReferenceException;
1920
use Rekalogika\Mapper\MainTransformer\Exception\TransformerReturnsUnexpectedValueException;
@@ -79,6 +80,13 @@ public function transform(
7980
Context $context,
8081
string $path = null,
8182
): mixed {
83+
// if MapperOptions is not provided, use the default options
84+
85+
if (!($mapperOptions = $context(MapperOptions::class))) {
86+
$mapperOptions = new MapperOptions();
87+
$context = $context->with($mapperOptions);
88+
}
89+
8290
// if target is provided, guess the type from it.
8391
// if target is not provided, use the provided target type. if it is
8492
// also not provided, then the target type is mixed.

0 commit comments

Comments
 (0)