Skip to content

Add wrapping attribute and update DataProvider method to use it #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ReflectionProperty;
use Symfony\Component\VarDumper\VarDumper;
use Webfox\InertiaDataProviders\AttributeNameFormatters\AttributeNameFormatter;
use Webfox\InertiaDataProviders\WrappingAttributes\WrappingAttribute;

abstract class DataProvider implements Arrayable, Jsonable
{
Expand All @@ -41,11 +42,22 @@ public function toArray(): array
$convertedMethods = collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC))
->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods))
->mapWithKeys(function (ReflectionMethod $method) {
$attributes = $method->getAttributes();
$returnType = $method->getReturnType();

if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [DeferProp::class, LazyProp::class, Closure::class])) {
return [$method->name => $method->invoke($this)];
}

if (count($attributes) > 0) {
foreach ($attributes as $attribute) {
$attributeInstance = $attribute->newInstance();
if ($attributeInstance instanceof WrappingAttribute) {
return [$method->name => $attributeInstance(fn () => app()->call([$this, $method->name]))];
}
}
Comment on lines +52 to +58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve efficiency, consider breaking out of the foreach loop after finding and applying the first WrappingAttribute. This prevents unnecessary iterations if multiple attributes are present but only one is intended for wrapping.

                if (count($attributes) > 0) {
                    foreach ($attributes as $attribute) {
                        $attributeInstance = $attribute->newInstance();
                        if ($attributeInstance instanceof WrappingAttribute) {
                            return [$method->name => $attributeInstance(fn () => app()->call([$this, $method->name]))];
                        
                        }
                    }
                }

}

return [$method->name => fn () => app()->call([$this, $method->name])];
});

Expand Down
15 changes: 15 additions & 0 deletions src/WrappingAttributes/InertiaMerge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Webfox\InertiaDataProviders\WrappingAttributes;

use Attribute;
use Inertia\Inertia;

#[Attribute(Attribute::TARGET_METHOD)]
class InertiaMerge implements WrappingAttribute
{
public function __invoke($data)
{
return Inertia::Merge($data);
}
}
14 changes: 14 additions & 0 deletions src/WrappingAttributes/WrappingAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Webfox\InertiaDataProviders\WrappingAttributes;

interface WrappingAttribute
{
/**
* Wrap the data with the attribute.
*
* @param mixed $data
* @return array
*/
public function __invoke($data);
}