Skip to content

Commit ecb6bec

Browse files
committed
feat(command): Prompt user for the case of returned parameters
1 parent 92edd28 commit ecb6bec

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This package will help you to generate API resources for your Laravel project.
1111
You can install the package via composer:
1212

1313
```bash
14-
composer require alibori/laravel-api-resource-generator
14+
composer require alibori/laravel-api-resource-generator --dev
1515
```
1616

1717
## Usage
@@ -22,7 +22,11 @@ All you need to do is to run the following command:
2222
php artisan api-resource:generate <model-name>
2323
```
2424

25-
This command will generate a new resource for the given model name with the properties defined in the model. For example, for the model named `User` you will get the following resource:
25+
This command will generate a new resource for the given model name with the properties defined in the model.
26+
27+
If you want to set the array keys of the resource as *camelCase*, you can use the `--camel-case`, you will be prompted to do it.
28+
29+
For example, for the model named `User` you will get the following resource:
2630

2731
``` php
2832
<?php
@@ -35,7 +39,7 @@ use Illuminate\Http\Resources\Json\JsonResource;
3539
use JsonSerializable;
3640

3741
/**
38-
*
42+
* Resource generated by alibori/laravel-api-resource-generator
3943
*
4044
* @property integer $id
4145
* @property string $name

build/report.junit.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<testsuites>
3-
<testsuite name="" tests="2" assertions="2" errors="0" warnings="0" failures="0" skipped="0" time="0.203402">
4-
<testsuite name="P\Tests\Feature\Test" file="C:\laragon\www\custom-packages\laravel-api-resource-generator\vendor\pestphp\pest\src\Factories\TestCaseFactory.php(223) : eval()'d code" tests="2" assertions="2" errors="0" warnings="0" failures="0" skipped="0" time="0.203402">
5-
<testcase name="it database users table has been created" assertions="1" time="0.103819"/>
6-
<testcase name="it can generate a resource" assertions="1" time="0.099583"/>
3+
<testsuite name="" tests="2" assertions="2" errors="0" warnings="0" failures="0" skipped="0" time="0.207782">
4+
<testsuite name="P\Tests\Feature\Test" file="C:\laragon\www\custom-packages\laravel-api-resource-generator\vendor\pestphp\pest\src\Factories\TestCaseFactory.php(223) : eval()'d code" tests="2" assertions="2" errors="0" warnings="0" failures="0" skipped="0" time="0.207782">
5+
<testcase name="it database users table has been created" assertions="1" time="0.103244"/>
6+
<testcase name="it can generate a resource" assertions="1" time="0.104538"/>
77
</testsuite>
88
</testsuite>
99
</testsuites>

src/Console/GenerateApiResourceCommand.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ class GenerateApiResourceCommand extends Command
5858
*/
5959
protected string $stub = __DIR__.'/stubs/api-resource.php.stub';
6060

61+
/**
62+
* @var string
63+
*/
64+
protected string $return_case = 'snake_case';
65+
6166
public function __construct(Filesystem $files)
6267
{
6368
parent::__construct();
@@ -72,6 +77,8 @@ public function __construct(Filesystem $files)
7277
*/
7378
public function handle(): void
7479
{
80+
$this->return_case = $this->choice('Which case do you want to use for the returned parameters?', ['snake_case', 'camelCase'], 0);
81+
7582
$this->dir = $this->defaultResourcesDir();
7683
$this->namespace = config('apiresourcegenerator.resources.namespace');
7784

@@ -189,12 +196,18 @@ protected function buildResource(string $name): string
189196
$properties_length = count($properties);
190197
$count = 0;
191198
foreach ($properties as $property) {
192-
if (0 === $count) {
193-
$fields .= "'{$property}' => \$this->{$property},\n";
199+
$array_key = $property;
200+
201+
if ($this->return_case === 'camelCase') {
202+
$array_key = Str::camel($property);
203+
}
204+
205+
if ($count < 1) {
206+
$fields .= "'{$array_key}' => \$this->{$property},\n";
194207
} elseif ($count < $properties_length - 1) {
195-
$fields .= "\t\t\t'{$property}' => \$this->{$property},\n";
208+
$fields .= "\t\t\t'{$array_key}' => \$this->{$property},\n";
196209
} else {
197-
$fields .= "\t\t\t'{$property}' => \$this->{$property}";
210+
$fields .= "\t\t\t'{$array_key}' => \$this->{$property}";
198211
}
199212

200213
$count++;
@@ -210,7 +223,7 @@ protected function buildResource(string $name): string
210223

211224
protected function generatePHPDocs(): string
212225
{
213-
$phpdoc = new DocBlock('');
226+
$phpdoc = new DocBlock('Resource generated by alibori/laravel-api-resource-generator');
214227

215228
foreach ($this->php_docs_properties as $name => $property) {
216229
$type = explode(' ', $property);

0 commit comments

Comments
 (0)