From 52554048ae95053a58998ad6568545b3ac78f641 Mon Sep 17 00:00:00 2001 From: Hide_D Date: Tue, 7 Jun 2022 00:28:37 +0900 Subject: [PATCH] =?UTF-8?q?refac:=20toArrayExcept=EB=A5=BC=20toArray?= =?UTF-8?q?=EB=A1=9C=20=ED=86=B5=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sammo/DTO/DTO.php | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/sammo/DTO/DTO.php b/src/sammo/DTO/DTO.php index 45cae1ca..3a9542f9 100644 --- a/src/sammo/DTO/DTO.php +++ b/src/sammo/DTO/DTO.php @@ -2,6 +2,7 @@ namespace sammo\DTO; +use Ds\Set; use sammo\DTO\Attr\Convert; use sammo\DTO\Converter\DefaultConverter; @@ -110,10 +111,13 @@ abstract class DTO return $object; } - public function toArray(): array + public function toArray(string ...$exceptKeys): array { $reflection = new \ReflectionClass($this::class); $result = []; + + $exceptKeySet = new Set($exceptKeys); + foreach ($reflection->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { $value = $property->getValue($this); $attrs = Util\DTOUtil::getAttrs($property); @@ -123,6 +127,10 @@ abstract class DTO continue; } + if($exceptKeySet->contains($name)){ + continue; + } + if (key_exists(Attr\RawName::class, $attrs)) { $rawAttr = $attrs[Attr\RawName::class]; $attr = new Attr\RawName(...$rawAttr->getArguments()); @@ -153,18 +161,4 @@ abstract class DTO } return $result; } - - public function toArrayExcept(string ...$keys): array{ - $reflection = new \ReflectionClass($this::class); - $values = $this->toArray(); - foreach($keys as $key){ - if(!$reflection->hasProperty($key)){ - throw new \Exception("Key {$key} does not exist"); - } - if(key_exists($key, $values)){ - unset($values[$key]); - } - } - return $values; - } }