File tree Expand file tree Collapse file tree 3 files changed +70
-1
lines changed
Expand file tree Collapse file tree 3 files changed +70
-1
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1313namespace App \Providers ;
1414
1515use App \ConfigManager ;
16+ use App \Macros \StringableMacro ;
17+ use App \Macros \StrMacro ;
1618use Illuminate \Support \ServiceProvider ;
19+ use Illuminate \Support \Str ;
20+ use Illuminate \Support \Stringable ;
1721
1822class 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}
You can’t perform that action at this time.
0 commit comments