Skip to content

Commit 8d98e85

Browse files
committed
Add dd and dump methods to DataProvider
1 parent 3cd0fb2 commit 8d98e85

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/DataProvider.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Webfox\InertiaDataProviders;
65

7-
use Illuminate\Contracts\Support\Arrayable;
8-
use Inertia\LazyProp;
96
use ReflectionClass;
7+
use Inertia\LazyProp;
8+
use Inertia\Response;
109
use ReflectionMethod;
11-
use ReflectionNamedType;
1210
use ReflectionProperty;
11+
use ReflectionNamedType;
12+
use Symfony\Component\VarDumper\VarDumper;
13+
use Illuminate\Contracts\Support\Arrayable;
1314

1415
abstract class DataProvider implements Arrayable
1516
{
1617
protected array|Arrayable $staticData = [];
1718

19+
protected array $excludedMethods = ['__construct', 'toArray', 'dd', 'dump',];
20+
1821
public static function collection(DataProvider|array ...$dataProviders): DataProviderCollection
1922
{
2023
return new DataProviderCollection(...$dataProviders);
@@ -32,7 +35,7 @@ public function toArray(): array
3235

3336
$convertedMethods = collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
3437
->filter(fn (ReflectionMethod $method) => ! $method->isStatic())
35-
->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, ['toArray', '__construct']))
38+
->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods))
3639
->mapWithKeys(function (ReflectionMethod $method) {
3740
$returnType = $method->getReturnType();
3841
// @phpstan-ignore-next-line
@@ -45,4 +48,19 @@ public function toArray(): array
4548

4649
return collect()->merge($staticData)->merge($convertedProperties)->merge($convertedMethods)->toArray();
4750
}
51+
52+
public function dump(): static
53+
{
54+
$response = new Response('', []);
55+
$props = $response->resolvePropertyInstances($this->toArray(), request());
56+
VarDumper::dump($props);
57+
return $this;
58+
}
59+
60+
#[\JetBrains\PhpStorm\NoReturn]
61+
public function dd()
62+
{
63+
$this->dump();
64+
exit(1);
65+
}
4866
}

0 commit comments

Comments
 (0)