Skip to content

Commit 1258a99

Browse files
author
Nathan Esayeas
authored
feat: generate resource toArray for non-hidden columns (#117)
1 parent 5dc34da commit 1258a99

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

src/Generators/Statements/ResourceGenerator.php

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
use Blueprint\Contracts\Generator;
66
use Blueprint\Models\Controller;
7+
use Blueprint\Models\Model;
78
use Blueprint\Models\Statements\ResourceStatement;
9+
use Illuminate\Support\Str;
810

911
class ResourceGenerator implements Generator
1012
{
13+
const INDENT = ' ';
1114
/**
1215
* @var \Illuminate\Contracts\Filesystem\Filesystem
1316
*/
@@ -26,11 +29,13 @@ public function output(array $tree): array
2629

2730
$stub = $this->files->stub('resource.stub');
2831

32+
$this->registerModels($tree);
33+
2934
/** @var \Blueprint\Models\Controller $controller */
3035
foreach ($tree['controllers'] as $controller) {
3136
foreach ($controller->methods() as $method => $statements) {
3237
foreach ($statements as $statement) {
33-
if (!$statement instanceof ResourceStatement) {
38+
if (! $statement instanceof ResourceStatement) {
3439
continue;
3540
}
3641

@@ -40,7 +45,7 @@ public function output(array $tree): array
4045
continue;
4146
}
4247

43-
if (!$this->files->exists(dirname($path))) {
48+
if (! $this->files->exists(dirname($path))) {
4449
$this->files->makeDirectory(dirname($path), 0755, true);
4550
}
4651

@@ -56,12 +61,12 @@ public function output(array $tree): array
5661

5762
protected function getPath(string $name)
5863
{
59-
return config('blueprint.app_path') . '/Http/Resources/' . $name . '.php';
64+
return config('blueprint.app_path').'/Http/Resources/'.$name.'.php';
6065
}
6166

6267
protected function populateStub(string $stub, ResourceStatement $resource)
6368
{
64-
$stub = str_replace('DummyNamespace', config('blueprint.namespace') . '\\Http\\Resources', $stub);
69+
$stub = str_replace('DummyNamespace', config('blueprint.namespace').'\\Http\\Resources', $stub);
6570
$stub = str_replace('DummyImport', $resource->collection() ? 'Illuminate\\Http\\Resources\\Json\\ResourceCollection' : 'Illuminate\\Http\\Resources\\Json\\JsonResource', $stub);
6671
$stub = str_replace('DummyParent', $resource->collection() ? 'ResourceCollection' : 'JsonResource', $stub);
6772
$stub = str_replace('DummyClass', $resource->name(), $stub);
@@ -74,14 +79,54 @@ protected function populateStub(string $stub, ResourceStatement $resource)
7479

7580
private function buildData(ResourceStatement $resource)
7681
{
82+
$context = Str::singular($resource->reference());
83+
84+
/** @var \Blueprint\Models\Model $model */
85+
$model = $this->modelForContext($context);
86+
87+
$data = [];
7788
if ($resource->collection()) {
78-
return 'return [
79-
\'data\' => $this->collection,
80-
];';
89+
$data[] = 'return [';
90+
$data[] = self::INDENT.'\'data\' => $this->collection,';
91+
$data[] = ' ];';
92+
93+
return implode(PHP_EOL, $data);
94+
}
95+
96+
$data[] = 'return [';
97+
foreach ($this->visibleColumns($model) as $column) {
98+
$data[] = self::INDENT.'\''.$column.'\' => $this->'.$column.',';
8199
}
100+
$data[] = ' ];';
101+
102+
return implode(PHP_EOL, $data);
103+
}
82104

83-
return 'return [
84-
\'id\' => $this->id,
85-
];';
105+
private function visibleColumns(Model $model)
106+
{
107+
return array_diff(array_keys($model->columns()), [
108+
'password',
109+
'remember_token',
110+
]);
111+
}
112+
113+
private function modelForContext(string $context)
114+
{
115+
if (isset($this->models[Str::studly($context)])) {
116+
return $this->models[Str::studly($context)];
117+
}
118+
119+
$matches = array_filter(array_keys($this->models), function ($key) use ($context) {
120+
return Str::endsWith($key, '/'.Str::studly($context));
121+
});
122+
123+
if (count($matches) === 1) {
124+
return $this->models[$matches[0]];
125+
}
126+
}
127+
128+
private function registerModels(array $tree)
129+
{
130+
$this->models = array_merge($tree['cache'] ?? [], $tree['models'] ?? []);
86131
}
87132
}

tests/Feature/Generator/Statements/ResourceGeneratorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ protected function setUp(): void
2727
$this->subject = new ResourceGenerator($this->files);
2828

2929
$this->blueprint = new Blueprint();
30+
$this->blueprint->registerLexer(new \Blueprint\Lexers\ModelLexer());
3031
$this->blueprint->registerLexer(new \Blueprint\Lexers\ControllerLexer(new StatementLexer()));
3132
$this->blueprint->registerGenerator($this->subject);
3233
}

tests/fixtures/definitions/resource-statements.bp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
models:
2+
User:
3+
name: string
4+
email: string
5+
password: string
6+
remember_token: remembertoken
7+
18
controllers:
29
User:
310
index:

tests/fixtures/resources/user.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public function toArray($request)
1616
{
1717
return [
1818
'id' => $this->id,
19+
'name' => $this->name,
20+
'email' => $this->email,
1921
];
2022
}
2123
}

0 commit comments

Comments
 (0)