Skip to content

Commit

Permalink
use trait instead of macro
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Aug 28, 2024
1 parent fcfe25d commit 102a553
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 49 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ composer require leuverink/livewire-property-group

```php
use Leuverink\PropertyAttribute\Group;
use Leuverink\PropertyAttribute\WithGroups;

class Form extends Component
{
use WithGroups;

#[Group('a')]
public $foo = 1;
#[Group('a')]
Expand All @@ -36,6 +39,8 @@ class Form extends Component

### Accessing Group Properties

Use the `WithGroups` trait within your Component or Form object to get access to the `group` method.

```php
// Get all properties in a group
$this->group('a'); // ['foo' => 1, 'bar' => 2]
Expand Down Expand Up @@ -99,16 +104,12 @@ $validated = $this->group('a')
### Conflicting `group` method signature

I realize that `group` is a very generic method name that you might well use inside your own components.
You may change the macro's signature by publishing & updating the package config.

For example, if you change `property-group.macro` from `group` to `fooBarBaz` you can retrieve property groups by calling `$this->fooBarBaz('group-name')` in your component.

You may also import and use the function directly;
You may change the macro's signature by providing an alias.

```php
use function Leuverink\PropertyAttribute\group;

$properties = group(component: $this, groups: 'group-name');
use WithGroups {
group as fooBar;
}
```

## Development
Expand Down
5 changes: 0 additions & 5 deletions config/property-group.php

This file was deleted.

30 changes: 10 additions & 20 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,20 @@ class ServiceProvider extends BaseServiceProvider
{
public function boot()
{
// Only when using locally
if (! $this->app->environment(['local', 'testing'])) {

$this->publishes([
__DIR__ . '/../config/property-group.php' => config_path('property-group.php'),
], 'livewire-property-group');
}

$this->registerGroupMacro();
// $this->registerGroupMacro();
}

public function register()
{
$this->mergeConfigFrom(__DIR__ . '/../config/property-group.php', 'property-group');
//
}

protected function registerGroupMacro()
{
$macro = config('property-group.macro');
$macro = str($macro)->camel()->toString();

Component::macro($macro, function (string|array|null $groups = null): PropertyCollection {
/** @var Component $this */
return group($this, $groups);
});
}
// Sadly Form objects are not macroable, going to use a trait instead
// protected function registerGroupMacro()
// {
// Component::macro($macro, function (string|array|null $groups = null): PropertyCollection {
// /** @var Component $this */
// return group($this, $groups);
// });
// }
}
3 changes: 3 additions & 0 deletions tests/TestComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Tests;

use Livewire\Component;
use Leuverink\PropertyAttribute\WithGroups;

class TestComponent extends Component
{
use WithGroups;

public function render()
{
return '<div></div>';
Expand Down
16 changes: 0 additions & 16 deletions tests/Unit/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Tests\TestComponent;
use Leuverink\PropertyAttribute\Group;
use Leuverink\PropertyAttribute\ServiceProvider;
use Leuverink\PropertyAttribute\PropertyCollection;

use function Leuverink\PropertyAttribute\group;
Expand Down Expand Up @@ -135,18 +134,3 @@
->expect(PropertyCollection::class)
->toHaveMethod('dump')
->toHaveMethod('dd');

it('can register the `group` macro under a different name', function () {

$this->app->config->set([
'property-group.macro' => 'fooBarBaz',
]);

// Hacky way to reregister the macro (Provider already booted before updating config)
(new ServiceProvider($this->app))->boot();

$component = new class extends TestComponent {};

$component->fooBarBaz('a'); // Will throw method not found exception if not rebound
expect(true)->toBe(true);
});

0 comments on commit 102a553

Please sign in to comment.