Skip to content

Commit 91d43db

Browse files
authored
Add support for attribute name formatting. (#20)
1 parent 0d4dce4 commit 91d43db

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ Install this package via composer:
1212
```bash
1313
composer require webfox/laravel-inertia-dataproviders
1414
```
15+
16+
Optionally publish the configuration file:
17+
18+
```bash
19+
php artisan vendor:publish --provider="Webfox\InertiaDataProviders\InertiaDataProvidersServiceProvider"
20+
````
21+
1522
We assume you've already for the Inertia adapter for Laravel installed.
1623
1724
## What Problem Does This Package Solve?
@@ -233,6 +240,22 @@ class DemoController extends Controller
233240
}
234241
```
235242
243+
## Attribute Name Formatting
244+
The attribute name format can be configured in the configuration file by setting the `attribute_name_formatter`.
245+
The package ships with three formatters under the namespace `\Webfox\InertiaDataProviders\AttributeNameFormatters` but you are free to create your own.
246+
247+
### AsWritten
248+
This is the default formatter. The output attribute name will be the same as the input name.
249+
E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `someData` and `more_data`.
250+
251+
### SnakeCase
252+
This formatter will convert the attribute name to snake_case.
253+
E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `some_data` and `more_data`.
254+
255+
### CamelCase
256+
This formatter will convert the attribute name to camelCase.
257+
E.g. a property named `$someData` and a method named `more_data()` will be available in the page as `someData` and `moreData`.
258+
236259
## Changelog
237260
238261
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

config/inertia-dataproviders.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
22

33
return [
4-
5-
];
4+
'attribute_name_formatter' => \Webfox\InertiaDataProviders\AttributeNameFormatters\AsWritten::class,
5+
];
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Webfox\InertiaDataProviders\AttributeNameFormatters;
4+
5+
class AsWritten implements AttributeNameFormatter
6+
{
7+
public function __invoke(string $name): string
8+
{
9+
return $name;
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Webfox\InertiaDataProviders\AttributeNameFormatters;
4+
5+
interface AttributeNameFormatter
6+
{
7+
public function __invoke(string $name): string;
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Webfox\InertiaDataProviders\AttributeNameFormatters;
4+
5+
use Illuminate\Support\Str;
6+
7+
class CamelCase implements AttributeNameFormatter
8+
{
9+
public function __invoke(string $name): string
10+
{
11+
return Str::of($name)->camel()->toString();
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Webfox\InertiaDataProviders\AttributeNameFormatters;
4+
5+
use Illuminate\Support\Str;
6+
7+
class SnakeCase implements AttributeNameFormatter
8+
{
9+
public function __invoke(string $name): string
10+
{
11+
return Str::of($name)->snake()->toString();
12+
}
13+
}

src/InertiaDataProvidersServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public function configurePackage(Package $package): void
1111
{
1212
$package
1313
->name('laravel-inertia-dataproviders')
14-
->hasConfigFile();
14+
->hasConfigFile('inertia-dataproviders');
1515
}
1616
}

0 commit comments

Comments
 (0)