feat: DTO에 Ignore 추가
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace sammo\DTO\Attr;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class Ignore
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,13 @@ abstract class DTO
|
|||||||
|
|
||||||
$param = $params[$name] ?? null;
|
$param = $params[$name] ?? null;
|
||||||
|
|
||||||
|
if(key_exists(Attr\Ignore::class, $attrs)){
|
||||||
|
if($param !== null){
|
||||||
|
throw new \Exception("Property {$name} is ignored but has a constructor parameter");
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (key_exists(Attr\DefaultValueGenerator::class, $attrs)){
|
if (key_exists(Attr\DefaultValueGenerator::class, $attrs)){
|
||||||
/** @var Attr\DefaultValueGenerator */
|
/** @var Attr\DefaultValueGenerator */
|
||||||
$defaultValueSetter = $attrs[Attr\DefaultValueGenerator::class]->newInstance();
|
$defaultValueSetter = $attrs[Attr\DefaultValueGenerator::class]->newInstance();
|
||||||
@@ -113,6 +120,10 @@ abstract class DTO
|
|||||||
$attrs = Util\Util::getAttrs($property);
|
$attrs = Util\Util::getAttrs($property);
|
||||||
$name = $property->getName();
|
$name = $property->getName();
|
||||||
|
|
||||||
|
if(key_exists(Attr\Ignore::class, $attrs)){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (key_exists(Attr\RawName::class, $attrs)) {
|
if (key_exists(Attr\RawName::class, $attrs)) {
|
||||||
$rawAttr = $attrs[Attr\RawName::class];
|
$rawAttr = $attrs[Attr\RawName::class];
|
||||||
$attr = new Attr\RawName(...$rawAttr->getArguments());
|
$attr = new Attr\RawName(...$rawAttr->getArguments());
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
use sammo\DTO\Attr\Convert;
|
use sammo\DTO\Attr\Convert;
|
||||||
use sammo\DTO\Attr\DefaultValue;
|
use sammo\DTO\Attr\DefaultValue;
|
||||||
use sammo\DTO\Attr\DefaultValueGenerator;
|
use sammo\DTO\Attr\DefaultValueGenerator;
|
||||||
|
use sammo\DTO\Attr\Ignore;
|
||||||
use sammo\DTO\DTO;
|
use sammo\DTO\DTO;
|
||||||
use sammo\DTO\Attr\JsonString;
|
use sammo\DTO\Attr\JsonString;
|
||||||
use sammo\DTO\Attr\NullIsUndefined;
|
use sammo\DTO\Attr\NullIsUndefined;
|
||||||
@@ -196,6 +197,23 @@ class TypeDefaultValue extends DTO
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TypeIgnore extends DTO
|
||||||
|
{
|
||||||
|
#[Ignore]
|
||||||
|
public int $c = 100;
|
||||||
|
#[Ignore]
|
||||||
|
public $d;
|
||||||
|
#[Ignore]
|
||||||
|
public $e;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public int $a,
|
||||||
|
public int $b,
|
||||||
|
) {
|
||||||
|
$this->d = $a * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class DTOTest extends PHPUnit\Framework\TestCase
|
class DTOTest extends PHPUnit\Framework\TestCase
|
||||||
{
|
{
|
||||||
public function testBasic()
|
public function testBasic()
|
||||||
@@ -436,4 +454,15 @@ class DTOTest extends PHPUnit\Framework\TestCase
|
|||||||
|
|
||||||
$this->assertEquals($testValue, $testType);
|
$this->assertEquals($testValue, $testType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testIgnore(){
|
||||||
|
$rawType = [
|
||||||
|
'a' => 1,
|
||||||
|
'b' => 2,
|
||||||
|
];
|
||||||
|
$obj = TypeIgnore::fromArray($rawType);
|
||||||
|
$testType = $obj->toArray();
|
||||||
|
|
||||||
|
$this->assertEquals($rawType, $testType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user