Skip to content

Commit 137ce13

Browse files
committed
feat(StrMacro): Add StrMacro class
- Declare strict types - Add StrMacro class - Add isJson() method
1 parent 1697b14 commit 137ce13

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

app/Macros/StrMacro.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/ai-commit.
7+
*
8+
* (c) guanguans <ityaozm@gmail.com>
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\Str
17+
*/
18+
class StrMacro
19+
{
20+
public static function isJson(): callable
21+
{
22+
return function ($value) {
23+
if (! is_string($value)) {
24+
return false;
25+
}
26+
27+
json_decode($value);
28+
29+
return JSON_ERROR_NONE === json_last_error();
30+
};
31+
}
32+
}

app/Macros/StringableMacro.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/ai-commit.
7+
*
8+
* (c) guanguans <ityaozm@gmail.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled.
11+
*/
12+
13+
namespace App\Macros;
14+
15+
use Illuminate\Support\Str;
16+
17+
/**
18+
* @mixin \Illuminate\Support\Stringable
19+
*/
20+
class StringableMacro
21+
{
22+
public function isJson(): callable
23+
{
24+
return function () {
25+
return Str::isJson($this->value);
26+
};
27+
}
28+
}

app/Providers/AppServiceProvider.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
namespace App\Providers;
1414

1515
use App\ConfigManager;
16+
use App\Macros\StringableMacro;
17+
use App\Macros\StrMacro;
1618
use Illuminate\Support\ServiceProvider;
19+
use Illuminate\Support\Str;
20+
use Illuminate\Support\Stringable;
1721

1822
class AppServiceProvider extends ServiceProvider
1923
{
@@ -22,7 +26,10 @@ class AppServiceProvider extends ServiceProvider
2226
*
2327
* @var array<array-key, string>
2428
*/
25-
public $singletons = [];
29+
public $singletons = [
30+
StringableMacro::class => StringableMacro::class,
31+
StrMacro::class => StrMacro::class,
32+
];
2633

2734
/**
2835
* Bootstrap any application services.
@@ -41,5 +48,7 @@ public function boot()
4148
*/
4249
public function register()
4350
{
51+
Stringable::mixin($this->app->make(StringableMacro::class));
52+
Str::mixin($this->app->make(StrMacro::class));
4453
}
4554
}

0 commit comments

Comments
 (0)