From 7464629597a188f150d984f9ea3468a9ebb86562 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Sat, 28 May 2022 16:12:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20DTO=EC=97=90=20Ignore=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sammo/DTO/Attr/Ignore.php | 11 +++++++++++ src/sammo/DTO/DTO.php | 11 +++++++++++ tests/DTOTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 src/sammo/DTO/Attr/Ignore.php diff --git a/src/sammo/DTO/Attr/Ignore.php b/src/sammo/DTO/Attr/Ignore.php new file mode 100644 index 00000000..18eef283 --- /dev/null +++ b/src/sammo/DTO/Attr/Ignore.php @@ -0,0 +1,11 @@ +newInstance(); @@ -113,6 +120,10 @@ abstract class DTO $attrs = Util\Util::getAttrs($property); $name = $property->getName(); + if(key_exists(Attr\Ignore::class, $attrs)){ + continue; + } + if (key_exists(Attr\RawName::class, $attrs)) { $rawAttr = $attrs[Attr\RawName::class]; $attr = new Attr\RawName(...$rawAttr->getArguments()); diff --git a/tests/DTOTest.php b/tests/DTOTest.php index 3574ff59..1b66512b 100644 --- a/tests/DTOTest.php +++ b/tests/DTOTest.php @@ -3,6 +3,7 @@ use sammo\DTO\Attr\Convert; use sammo\DTO\Attr\DefaultValue; use sammo\DTO\Attr\DefaultValueGenerator; +use sammo\DTO\Attr\Ignore; use sammo\DTO\DTO; use sammo\DTO\Attr\JsonString; 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 { public function testBasic() @@ -436,4 +454,15 @@ class DTOTest extends PHPUnit\Framework\TestCase $this->assertEquals($testValue, $testType); } + + public function testIgnore(){ + $rawType = [ + 'a' => 1, + 'b' => 2, + ]; + $obj = TypeIgnore::fromArray($rawType); + $testType = $obj->toArray(); + + $this->assertEquals($rawType, $testType); + } }