Skip to content

Commit ebfff4f

Browse files
committed
feat(helpers): Improve make function to support more keys
- Enhance the `make` function to recognize additional keys for abstraction - Update key checks from 3 to 8, improving flexibility in input arrays - Add reference link for clarity on the implementation - Modify tests to accommodate new key options for making API responses
1 parent acbccd8 commit ebfff4f

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

baselines/loader.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ includes:
22
- function.alreadyNarrowedType.neon
33
- method.nonObject.neon
44
- method.notFound.neon
5+
- nullCoalesce.variable.neon
56
- trait.unused.neon
67
- typeCoverage.paramTypeCoverage.neon

baselines/nullCoalesce.variable.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# total 1 error
2+
3+
parameters:
4+
ignoreErrors:
5+
-
6+
message: '#^Variable \$keys on left side of \?\?\= is never defined\.$#'
7+
count: 1
8+
path: ../src/Support/helpers.php

src/Support/helpers.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
if (!\function_exists('Guanguans\LaravelApiResponse\Support\make')) {
2020
/**
21+
* @see https://github.com/yiisoft/yii2/blob/master/framework/BaseYii.php
22+
*
2123
* @param array<string, mixed>|string $abstract
2224
* @param array<string, mixed> $parameters
2325
*
@@ -29,18 +31,27 @@ function make(array|string $abstract, array $parameters = []): mixed
2931
return resolve($abstract, $parameters);
3032
}
3133

32-
foreach ($classes = ['__class', '_class', 'class'] as $class) {
33-
if (isset($abstract[$class])) {
34-
return make(
35-
$abstract[$class],
36-
$parameters + Arr::except($abstract, $class),
37-
);
34+
foreach (
35+
$keys ??= [
36+
'__abstract',
37+
'__class',
38+
'__name',
39+
'_abstract',
40+
'_class',
41+
'_name',
42+
'abstract',
43+
'class',
44+
'name',
45+
] as $key
46+
) {
47+
if (isset($abstract[$key])) {
48+
return make($abstract[$key], $parameters + Arr::except($abstract, $key));
3849
}
3950
}
4051

4152
throw new InvalidArgumentException(\sprintf(
4253
'The argument of abstract must be an array containing a `%s` element.',
43-
implode('` or `', $classes)
54+
implode('` or `', $keys)
4455
));
4556
}
4657
}

tests/Support/HeplersTest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @see https://github.com/guanguans/laravel-api-response
1818
*/
1919

20-
use Guanguans\LaravelApiResponse\Contracts\ApiResponseContract;
20+
use Guanguans\LaravelApiResponse\ApiResponse;
2121
use Pest\Expectation;
2222
use function Guanguans\LaravelApiResponse\Support\env_explode;
2323
use function Guanguans\LaravelApiResponse\Support\make;
@@ -34,9 +34,20 @@
3434
make([]);
3535
})->group(__DIR__, __FILE__)->throws(InvalidArgumentException::class);
3636

37-
it('can make api response', function (): void {
38-
expect(make(['class' => ApiResponseContract::class]))->toBeInstanceOf(ApiResponseContract::class);
39-
})->group(__DIR__, __FILE__);
37+
it('can make api response', function (array|string $abstract): void {
38+
expect(make($abstract))->toBeInstanceOf(ApiResponse::class);
39+
})->group(__DIR__, __FILE__)->with([
40+
['abstract' => ApiResponse::class],
41+
['abstract' => ['__abstract' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
42+
['abstract' => ['__class' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
43+
['abstract' => ['__name' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
44+
['abstract' => ['_abstract' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
45+
['abstract' => ['_class' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
46+
['abstract' => ['_name' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
47+
['abstract' => ['abstract' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
48+
['abstract' => ['class' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
49+
['abstract' => ['name' => ApiResponse::class, 'pipes' => collect(), 'exceptionPipes' => collect()]],
50+
]);
4051

4152
it('can explode env', function (): void {
4253
expect([

0 commit comments

Comments
 (0)