Skip to content

Commit d283977

Browse files
committed
Add wrapping attribute and update DataProvider method to use it
1 parent c90bb44 commit d283977

File tree

3 files changed

+54
-12
lines changed

3 files changed

+54
-12
lines changed

src/DataProvider.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
namespace Webfox\InertiaDataProviders;
66

77
use Closure;
8-
use Illuminate\Contracts\Support\Arrayable;
9-
use Illuminate\Contracts\Support\Jsonable;
10-
use Inertia\DeferProp;
8+
use ReflectionClass;
119
use Inertia\LazyProp;
1210
use Inertia\Response;
13-
use ReflectionClass;
1411
use ReflectionMethod;
15-
use ReflectionNamedType;
12+
use Inertia\DeferProp;
1613
use ReflectionProperty;
14+
use ReflectionNamedType;
15+
use Illuminate\Contracts\Support\Jsonable;
1716
use Symfony\Component\VarDumper\VarDumper;
17+
use Illuminate\Contracts\Support\Arrayable;
18+
use Webfox\InertiaDataProviders\WrappingAttributes\WrappingAttribute;
1819
use Webfox\InertiaDataProviders\AttributeNameFormatters\AttributeNameFormatter;
1920

2021
abstract class DataProvider implements Arrayable, Jsonable
@@ -30,30 +31,41 @@ public static function collection(DataProvider|array ...$dataProviders): DataPro
3031

3132
public function toArray(): array
3233
{
33-
$staticData = $this->staticData instanceof Arrayable ? $this->staticData->toArray() : $this->staticData;
34+
$staticData = $this->staticData instanceof Arrayable ? $this->staticData->toArray() : $this->staticData;
3435
$reflectionClass = (new ReflectionClass($this));
3536

3637
$convertedProperties = collect($reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC))
37-
->filter(fn (ReflectionProperty $property) => ! $property->isStatic())
38-
->mapWithKeys(fn (ReflectionProperty $property) => [$property->getName() => $property->getValue($this)])
39-
->map(fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value);
38+
->filter(fn(ReflectionProperty $property) => !$property->isStatic())
39+
->mapWithKeys(fn(ReflectionProperty $property) => [$property->getName() => $property->getValue($this)])
40+
->map(fn($value) => $value instanceof Arrayable ? $value->toArray() : $value);
4041

4142
$convertedMethods = collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
42-
->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods))
43+
->filter(fn(ReflectionMethod $method) => !$method->isStatic() && !in_array($method->name, $this->excludedMethods))
4344
->mapWithKeys(function (ReflectionMethod $method) {
45+
$attributes = $method->getAttributes();
4446
$returnType = $method->getReturnType();
47+
4548
if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [DeferProp::class, LazyProp::class, Closure::class])) {
4649
return [$method->name => $method->invoke($this)];
4750
}
4851

49-
return [$method->name => fn () => app()->call([$this, $method->name])];
52+
if (count($attributes) > 0) {
53+
foreach ($attributes as $attribute) {
54+
$attributeInstance = $attribute->newInstance();
55+
if ($attributeInstance instanceof WrappingAttribute) {
56+
return [$method->name => $attributeInstance(fn() => app()->call([$this, $method->name]))];
57+
}
58+
}
59+
}
60+
61+
return [$method->name => fn() => app()->call([$this, $method->name])];
5062
});
5163

5264
return collect()
5365
->merge($staticData)
5466
->merge($convertedProperties)
5567
->merge($convertedMethods)
56-
->mapWithKeys(fn ($value, $key) => [$this->attributeNameFormatter()($key) => $value])
68+
->mapWithKeys(fn($value, $key) => [$this->attributeNameFormatter()($key) => $value])
5769
->toArray();
5870
}
5971

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Webfox\InertiaDataProviders\WrappingAttributes;
4+
5+
6+
use Attribute;
7+
use Inertia\Inertia;
8+
9+
#[Attribute(Attribute::TARGET_METHOD)]
10+
class InertiaMerge implements WrappingAttribute
11+
{
12+
public function __invoke($data)
13+
{
14+
return Inertia::Merge($data);
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Webfox\InertiaDataProviders\WrappingAttributes;
4+
5+
interface WrappingAttribute
6+
{
7+
/**
8+
* Wrap the data with the attribute.
9+
*
10+
* @param mixed $data
11+
* @return array
12+
*/
13+
public function __invoke($data);
14+
}

0 commit comments

Comments
 (0)