Skip to content

Commit 88b4b90

Browse files
committed
Added createLazyUsing callback function
1 parent 052d2ac commit 88b4b90

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Concerns/LazyData.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function createLazy(mixed ...$args): static
3030
}
3131

3232
return $context->newLazyProxy(
33-
static function (mixed $object) use($context, $value): static {
33+
static function () use($context, $value): static {
3434
$data = DeserializePipeline::createFromArray()
3535
->sendThenReturn(new DeserializePipelinePassable(
3636
classContext: $context,
@@ -45,4 +45,23 @@ classContext: $context,
4545
throw DataCreationException::unableToCreateLazyInstance(static::class, $e);
4646
}
4747
}
48+
49+
/**
50+
* @param callable(static $data): static $callable
51+
*
52+
* @throws DataCreationException
53+
*/
54+
public static function createLazyUsing(callable $callable): static
55+
{
56+
try {
57+
/** @var ClassContext<static> $context */
58+
$context = ClassContext::getInstance(static::class);
59+
60+
return $context->newLazyProxy($callable);
61+
// @codeCoverageIgnoreStart
62+
} catch (Throwable $e) {
63+
throw DataCreationException::unableToCreateLazyInstance(static::class, $e);
64+
}
65+
// @codeCoverageIgnoreEnd
66+
}
4867
}

src/Contracts/LazyData.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Nuxtifyts\PhpDto\Contracts;
44

5+
use Nuxtifyts\PhpDto\Data;
56
use Nuxtifyts\PhpDto\Exceptions\DataCreationException;
67

78
interface LazyData
@@ -10,4 +11,11 @@ interface LazyData
1011
* @throws DataCreationException
1112
*/
1213
public static function createLazy(mixed ...$args): static;
14+
15+
/**
16+
* @param callable(static $data): static $callable
17+
*
18+
* @throws DataCreationException
19+
*/
20+
public static function createLazyUsing(callable $callable): static;
1321
}

0 commit comments

Comments
 (0)