Skip to content

Commit 66b2b45

Browse files
committed
[Feature] Add with count validation to query stubs
1 parent 23fdb6e commit 66b2b45

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/Console/MakeQuery.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
namespace LaravelJsonApi\Laravel\Console;
2121

22+
use Illuminate\Support\Str;
23+
use LaravelJsonApi\Core\Query\Custom\ExtendedQueryParameters;
2224
use Symfony\Component\Console\Input\InputOption;
2325

2426
class MakeQuery extends GeneratorCommand
@@ -82,6 +84,36 @@ protected function getClassType(): string
8284
return 'Query';
8385
}
8486

87+
/**
88+
* @inheritDoc
89+
*/
90+
protected function buildClass($name)
91+
{
92+
$stub = parent::buildClass($name);
93+
94+
return $this->replaceAdditional($stub);
95+
}
96+
97+
/**
98+
* Replace additional placeholders.
99+
*
100+
* @param string $stub
101+
* @return string
102+
*/
103+
protected function replaceAdditional(string $stub): string
104+
{
105+
$withCount = ExtendedQueryParameters::withCount();
106+
107+
$replace = [
108+
'{{ withCount }}' => $withCount,
109+
'{{withCount}}' => $withCount,
110+
];
111+
112+
return str_replace(
113+
array_keys($replace), array_values($replace), $stub
114+
);
115+
}
116+
85117
/**
86118
* @inheritDoc
87119
*/

stubs/query-collection.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class {{ class }} extends ResourceQuery
4141
'string',
4242
JsonApiRule::sort(),
4343
],
44+
'{{ withCount }}' => [
45+
'nullable',
46+
'string',
47+
JsonApiRule::countable(),
48+
],
4449
];
4550
}
4651
}

stubs/query.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class {{ class }} extends ResourceQuery
3333
],
3434
'page' => JsonApiRule::notSupported(),
3535
'sort' => JsonApiRule::notSupported(),
36+
'{{ withCount }}' => [
37+
'nullable',
38+
'string',
39+
JsonApiRule::countable(),
40+
],
3641
];
3742
}
3843
}

tests/lib/Integration/Console/MakeQueryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ private function assertQueryCreated(): void
158158
'class PostQuery extends ResourceQuery',
159159
"'page' => JsonApiRule::notSupported(),",
160160
"'sort' => JsonApiRule::notSupported(),",
161+
"'withCount' =>",
161162
];
162163

163164
foreach ($tests as $expected) {
@@ -180,6 +181,7 @@ private function assertQueryCollectionCreated(): void
180181
'class PostCollectionQuery extends ResourceQuery',
181182
'JsonApiRule::page(),',
182183
'JsonApiRule::sort(),',
184+
"'withCount' =>",
183185
];
184186

185187
foreach ($tests as $expected) {

0 commit comments

Comments
 (0)