-
Notifications
You must be signed in to change notification settings - Fork 586
/
ExtractProperties.php
42 lines (37 loc) · 1.3 KB
/
ExtractProperties.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Laravel\Telescope;
use Illuminate\Database\Eloquent\Model;
use ReflectionClass;
class ExtractProperties
{
/**
* Extract the properties for the given object in array form.
*
* The given array is ready for storage.
*
* @param mixed $target
* @return array
*/
public static function from($target)
{
return collect((new ReflectionClass($target))->getProperties())
->mapWithKeys(function ($property) use ($target) {
$property->setAccessible(true);
if (PHP_VERSION_ID >= 70400 && ! $property->isInitialized($target)) {
return [];
}
if (($value = $property->getValue($target)) instanceof Model) {
return [$property->getName() => FormatModel::given($value)];
} elseif (is_object($value)) {
return [
$property->getName() => [
'class' => get_class($value),
'properties' => json_decode(json_encode($value), true),
],
];
} else {
return [$property->getName() => json_decode(json_encode($value), true)];
}
})->toArray();
}
}