refac: toArrayExcept를 toArray로 통합
This commit is contained in:
+9
-15
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user