Skip to content
This repository was archived by the owner on Mar 12, 2024. It is now read-only.

Commit b556619

Browse files
committed
add dumpProperties method
1 parent b792b40 commit b556619

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/Traits/LoopFunctions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,31 @@ public function arrayToProperties(array|\ArrayAccess|null $data, mixed $rescue =
6969
}
7070
});
7171
}
72+
73+
/**
74+
* Dump class properties as key-valued array.
75+
*
76+
* @param string|object|null $class
77+
* @param int|null $filter
78+
*
79+
* @return array
80+
* @throws \ReflectionException
81+
*/
82+
public function dumpProperties(string|object|null $class = null, ?int $filter = null): array
83+
{
84+
$class = match (true) {
85+
is_string($class) => app($class),
86+
is_object($class) => $class,
87+
default => $this,
88+
};
89+
90+
$properties = (new \ReflectionClass($class))
91+
->getProperties($filter);
92+
93+
return collect($properties)->mapWithKeys(
94+
fn (\ReflectionProperty $property) => [
95+
$property->getName() => $property->getValue($class),
96+
]
97+
)->all();
98+
}
7299
}

0 commit comments

Comments
 (0)