Skip to content

Commit 46dde21

Browse files
feat(macros): Add JSON macro to Collection class
- Introduce a new `CollectionMacMini` that provides a `json()` method - Update `CommitCommand` to utilize the new Collection method for JSON decoding - Enhance `_ide_helper.php` by correcting namespace references for `Str` and `Collection` - Improve code organization and maintainability with added macros
1 parent 974bd06 commit 46dde21

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

_ide_helper.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212

1313
namespace Illuminate\Support {
1414
/**
15-
* @method static bool jsonValidate(string $json, int $depth = 512, int $flags = 0)
15+
* @method static \Illuminate\Support\Collection json(string $json, int $depth = 512, int $options = 0)
1616
*
17-
* @mixin \Illuminate\Support\Str
17+
* @mixin \Illuminate\Support\Collection
1818
*/
19-
final class Str
19+
final class Collection
2020
{
2121
}
2222

@@ -28,6 +28,15 @@ final class Str
2828
final class Stringable
2929
{
3030
}
31+
32+
/**
33+
* @method static bool jsonValidate(string $json, int $depth = 512, int $flags = 0)
34+
*
35+
* @mixin \Illuminate\Support\Str
36+
*/
37+
final class Str
38+
{
39+
}
3140
}
3241

3342
namespace App\Support {

app/Commands/CommitCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function ($attempts) use ($cachedDiff, $type): string {
107107
);
108108
})
109109
->tap(function () use (&$message): void {
110-
$message = collect(json_decode($message, true, 512, JSON_THROW_ON_ERROR | JSON_PARTIAL_OUTPUT_ON_ERROR))
110+
$message = Collection::json($message)
111111
->map(static function ($content) {
112112
if (\is_array($content)) {
113113
return collect($content)

app/Macros/CollectionMacro.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/ai-commit.
7+
*
8+
* (c) guanguans <[email protected]>
9+
*
10+
* This source file is subject to the MIT license that is bundled.
11+
*/
12+
13+
namespace App\Macros;
14+
15+
/**
16+
* @mixin \Illuminate\Support\Collection
17+
*/
18+
class CollectionMacro
19+
{
20+
/**
21+
* @noinspection JsonEncodingApiUsageInspection
22+
* @noinspection PhpMethodParametersCountMismatchInspection
23+
*/
24+
public static function json(): callable
25+
{
26+
return static function (string $json, int $depth = 512, int $options = 0): self {
27+
return new static(json_decode(
28+
$json,
29+
true,
30+
$depth,
31+
$options
32+
));
33+
};
34+
}
35+
}

app/Providers/AppServiceProvider.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
use App\ConfigManager;
1616
use App\GeneratorManager;
17+
use App\Macros\CollectionMacro;
1718
use App\Macros\StringableMacro;
1819
use App\Macros\StrMacro;
1920
use Illuminate\Console\OutputStyle;
2021
use Illuminate\Contracts\Container\BindingResolutionException;
22+
use Illuminate\Support\Collection;
2123
use Illuminate\Support\ServiceProvider;
2224
use Illuminate\Support\Str;
2325
use Illuminate\Support\Stringable;
@@ -33,9 +35,10 @@ final class AppServiceProvider extends ServiceProvider
3335
* @var array<array-key, string>
3436
*/
3537
public $singletons = [
38+
CollectionMacro::class => CollectionMacro::class,
39+
GeneratorManager::class => GeneratorManager::class,
3640
StringableMacro::class => StringableMacro::class,
3741
StrMacro::class => StrMacro::class,
38-
GeneratorManager::class => GeneratorManager::class,
3942
];
4043

4144
/**
@@ -50,8 +53,9 @@ public function register(): void
5053
return new OutputStyle(new ArgvInput(), new ConsoleOutput());
5154
});
5255

53-
Stringable::mixin($this->app->make(StringableMacro::class));
56+
Collection::mixin($this->app->make(CollectionMacro::class));
5457
Str::mixin($this->app->make(StrMacro::class));
58+
Stringable::mixin($this->app->make(StringableMacro::class));
5559
}
5660

5761
/**

0 commit comments

Comments
 (0)