Skip to content

Commit

Permalink
Add support for attribute name formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
hailwood committed Jun 21, 2024
1 parent 0d4dce4 commit 82892cc
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ Install this package via composer:
```bash
composer require webfox/laravel-inertia-dataproviders
```

Optionally publish the configuration file:

```bash
php artisan vendor:publish --provider="Webfox\InertiaDataProviders\InertiaDataProvidersServiceProvider" --tag="config"
````

We assume you've already for the Inertia adapter for Laravel installed.
## What Problem Does This Package Solve?
Expand Down Expand Up @@ -233,6 +240,22 @@ class DemoController extends Controller
}
```
## Attribute Name Formatting
The attribute name format can be configured in the configuration file by setting the `attribute_name_formatter`.
The package ships with three formatters under the namespace `\Webfox\InertiaDataProviders\AttributeNameFormatters` but you are free to create your own.
### AsWritten
This is the default formatter. The output attribute name will be the same as the input name.
E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `someData` and `more_data`.
### SnakeCase
This formatter will convert the attribute name to snake_case.
E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `some_data` and `more_data`.
### CamelCase
This formatter will convert the attribute name to camelCase.
E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `someData` and `moreData`.
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
4 changes: 2 additions & 2 deletions config/inertia-dataproviders.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return [

];
'attribute_name_formatter' => \Webfox\InertiaDataProviders\AttributeNameFormatters\AsWritten::class,
];
13 changes: 13 additions & 0 deletions src/AttributeNameFormatters/AsWritten.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Webfox\InertiaDataProviders\AttributeNameFormatters;

use Illuminate\Support\Str;

class AsWritten implements AttributeNameFormatter
{
public function __invoke(string $name): string
{
return $name;
}
}
8 changes: 8 additions & 0 deletions src/AttributeNameFormatters/AttributeNameFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Webfox\InertiaDataProviders\AttributeNameFormatters;

interface AttributeNameFormatter
{
public function __invoke(string $name): string;
}
13 changes: 13 additions & 0 deletions src/AttributeNameFormatters/CamelCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Webfox\InertiaDataProviders\AttributeNameFormatters;

use Illuminate\Support\Str;

class CamelCase implements AttributeNameFormatter
{
public function __invoke(string $name): string
{
return Str::of($name)->camel()->toString();
}
}
13 changes: 13 additions & 0 deletions src/AttributeNameFormatters/SnakeCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Webfox\InertiaDataProviders\AttributeNameFormatters;

use Illuminate\Support\Str;

class SnakeCase implements AttributeNameFormatter
{
public function __invoke(string $name): string
{
return Str::of($name)->snake()->toString();
}
}
2 changes: 1 addition & 1 deletion src/InertiaDataProvidersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public function configurePackage(Package $package): void
{
$package
->name('laravel-inertia-dataproviders')
->hasConfigFile();
->hasConfigFile('inertia-dataproviders');
}
}

0 comments on commit 82892cc

Please sign in to comment.