forked from devsam/core
feat: DTO에 DefaultValue, DefaultValueGenerator 추가
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace sammo\DTO\Attr;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class DefaultValue
|
||||
{
|
||||
public function __construct(public readonly null|bool|int|float|string|array $defaultValue)
|
||||
{
|
||||
}
|
||||
|
||||
public function getDefaultValue(): mixed
|
||||
{
|
||||
return $this->defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace sammo\DTO\Attr;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
class DefaultValueGenerator
|
||||
{
|
||||
public function __construct(public readonly string $generator)
|
||||
{
|
||||
if (!is_callable($generator)) {
|
||||
throw new \Exception("$generator is not a callable");
|
||||
}
|
||||
}
|
||||
|
||||
public function getDefaultValue(): mixed
|
||||
{
|
||||
$generator = $this->generator;
|
||||
return $generator();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user