Skip to content

Commit 75c331e

Browse files
Exporting macros to another application layer
1 parent 7fc0d70 commit 75c331e

File tree

4 files changed

+118
-63
lines changed

4 files changed

+118
-63
lines changed

src/Macroses/Macros.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelHttpMacros\Macroses;
6+
7+
use Closure;
8+
9+
/** @mixin \Illuminate\Http\Client\Response */
10+
abstract class Macros
11+
{
12+
abstract public static function callback(): Closure;
13+
14+
abstract public static function name(): string;
15+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelHttpMacros\Macroses;
6+
7+
use Closure;
8+
use Illuminate\Support\Collection;
9+
10+
/**
11+
* Get the JSON decoded body of the response as a collection.
12+
*/
13+
class ToDataCollectionMacros extends Macros
14+
{
15+
public static function callback(): Closure
16+
{
17+
return function (Closure|string $class, int|string|null $key = null): Collection {
18+
if (is_null($data = $this->json($key))) {
19+
return collect();
20+
}
21+
22+
if (is_callable($class)) {
23+
$result = $class($data);
24+
25+
return $result instanceof Collection ? $result : collect([$result]);
26+
}
27+
28+
if (method_exists($class, 'collect')) {
29+
$result = $class::collect($data);
30+
31+
if ($result instanceof Collection) {
32+
return $result;
33+
}
34+
35+
return is_array($result) ? collect($result) : collect([$result]);
36+
}
37+
38+
return collect($data)->map(function (array $item) use ($class) {
39+
if (method_exists($class, 'from')) {
40+
return $class::from($item);
41+
}
42+
43+
return new $class(...$item);
44+
});
45+
};
46+
}
47+
48+
public static function name(): string
49+
{
50+
return 'toDataCollection';
51+
}
52+
}

src/Macroses/ToDataMacros.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\LaravelHttpMacros\Macroses;
6+
7+
use Closure;
8+
9+
/**
10+
* Get the JSON decoded body of the response as a class instance.
11+
*/
12+
class ToDataMacros extends Macros
13+
{
14+
public static function callback(): Closure
15+
{
16+
return function (Closure|string $class, int|string|null $key = null): mixed {
17+
if (is_callable($class)) {
18+
return $class($this->json($key));
19+
}
20+
21+
if (is_null($data = $this->json($key))) {
22+
return null;
23+
}
24+
25+
if (method_exists($class, 'from')) {
26+
return $class::from($data);
27+
}
28+
29+
return new $class(...$data);
30+
};
31+
}
32+
33+
public static function name(): string
34+
{
35+
return 'toData';
36+
}
37+
}

src/ServiceProvider.php

Lines changed: 14 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,29 @@
44

55
namespace DragonCode\LaravelHttpMacros;
66

7-
use Closure;
7+
use DragonCode\LaravelHttpMacros\Macroses\Macros;
8+
use DragonCode\LaravelHttpMacros\Macroses\ToDataCollectionMacros;
9+
use DragonCode\LaravelHttpMacros\Macroses\ToDataMacros;
810
use Illuminate\Http\Client\Response;
9-
use Illuminate\Support\Collection;
1011
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
1112

1213
class ServiceProvider extends BaseServiceProvider
1314
{
14-
public function boot(): void
15-
{
16-
$this->toData();
17-
$this->toDataCollection();
18-
}
15+
/** @var array<string|Macros> */
16+
protected array $macroses = [
17+
ToDataMacros::class,
18+
ToDataCollectionMacros::class,
19+
];
1920

20-
/**
21-
* Get the JSON decoded body of the response as a class instance.
22-
*
23-
* @return void
24-
*/
25-
protected function toData(): void
21+
public function boot(): void
2622
{
27-
Response::macro('toData', function (Closure|string $class, int|string|null $key = null): mixed {
28-
if (is_callable($class)) {
29-
return $class($this->json($key));
30-
}
31-
32-
if (is_null($data = $this->json($key))) {
33-
return null;
34-
}
35-
36-
if (method_exists($class, 'from')) {
37-
return $class::from($data);
38-
}
39-
40-
return new $class(...$data);
41-
});
23+
foreach ($this->macroses as $macros) {
24+
$this->bootMacros($macros);
25+
}
4226
}
4327

44-
/**
45-
* Get the JSON decoded body of the response as a collection.
46-
*
47-
* @return void
48-
*/
49-
protected function toDataCollection(): void
28+
protected function bootMacros(Macros|string $macros): void
5029
{
51-
Response::macro('toDataCollection', function (Closure|string $class, int|string|null $key = null): Collection {
52-
if (is_null($data = $this->json($key))) {
53-
return collect();
54-
}
55-
56-
if (is_callable($class)) {
57-
$result = $class($data);
58-
59-
return $result instanceof Collection ? $result : collect([$result]);
60-
}
61-
62-
if (method_exists($class, 'collect')) {
63-
$result = $class::collect($data);
64-
65-
if ($result instanceof Collection) {
66-
return $result;
67-
}
68-
69-
return is_array($result) ? collect($result) : collect([$result]);
70-
}
71-
72-
return collect($data)->map(function (array $item) use ($class) {
73-
if (method_exists($class, 'from')) {
74-
return $class::from($item);
75-
}
76-
77-
return new $class(...$item);
78-
});
79-
});
30+
Response::macro($macros::name(), $macros::callback());
8031
}
8132
}

0 commit comments

Comments
 (0)