Skip to content

Commit

Permalink
Exporting macros to another application layer
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-helldar committed Aug 22, 2024
1 parent 7fc0d70 commit 75c331e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 63 deletions.
15 changes: 15 additions & 0 deletions src/Macroses/Macros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelHttpMacros\Macroses;

use Closure;

/** @mixin \Illuminate\Http\Client\Response */
abstract class Macros
{
abstract public static function callback(): Closure;

abstract public static function name(): string;
}
52 changes: 52 additions & 0 deletions src/Macroses/ToDataCollectionMacros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelHttpMacros\Macroses;

use Closure;
use Illuminate\Support\Collection;

/**
* Get the JSON decoded body of the response as a collection.
*/
class ToDataCollectionMacros extends Macros
{
public static function callback(): Closure
{
return function (Closure|string $class, int|string|null $key = null): Collection {
if (is_null($data = $this->json($key))) {
return collect();
}

if (is_callable($class)) {
$result = $class($data);

return $result instanceof Collection ? $result : collect([$result]);
}

if (method_exists($class, 'collect')) {
$result = $class::collect($data);

if ($result instanceof Collection) {
return $result;
}

return is_array($result) ? collect($result) : collect([$result]);
}

return collect($data)->map(function (array $item) use ($class) {
if (method_exists($class, 'from')) {
return $class::from($item);
}

return new $class(...$item);
});
};
}

public static function name(): string
{
return 'toDataCollection';
}
}
37 changes: 37 additions & 0 deletions src/Macroses/ToDataMacros.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace DragonCode\LaravelHttpMacros\Macroses;

use Closure;

/**
* Get the JSON decoded body of the response as a class instance.
*/
class ToDataMacros extends Macros
{
public static function callback(): Closure
{
return function (Closure|string $class, int|string|null $key = null): mixed {
if (is_callable($class)) {
return $class($this->json($key));
}

if (is_null($data = $this->json($key))) {
return null;
}

if (method_exists($class, 'from')) {
return $class::from($data);
}

return new $class(...$data);
};
}

public static function name(): string
{
return 'toData';
}
}
77 changes: 14 additions & 63 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,29 @@

namespace DragonCode\LaravelHttpMacros;

use Closure;
use DragonCode\LaravelHttpMacros\Macroses\Macros;
use DragonCode\LaravelHttpMacros\Macroses\ToDataCollectionMacros;
use DragonCode\LaravelHttpMacros\Macroses\ToDataMacros;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Collection;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
public function boot(): void
{
$this->toData();
$this->toDataCollection();
}
/** @var array<string|Macros> */
protected array $macroses = [
ToDataMacros::class,
ToDataCollectionMacros::class,
];

/**
* Get the JSON decoded body of the response as a class instance.
*
* @return void
*/
protected function toData(): void
public function boot(): void
{
Response::macro('toData', function (Closure|string $class, int|string|null $key = null): mixed {
if (is_callable($class)) {
return $class($this->json($key));
}

if (is_null($data = $this->json($key))) {
return null;
}

if (method_exists($class, 'from')) {
return $class::from($data);
}

return new $class(...$data);
});
foreach ($this->macroses as $macros) {
$this->bootMacros($macros);
}
}

/**
* Get the JSON decoded body of the response as a collection.
*
* @return void
*/
protected function toDataCollection(): void
protected function bootMacros(Macros|string $macros): void
{
Response::macro('toDataCollection', function (Closure|string $class, int|string|null $key = null): Collection {
if (is_null($data = $this->json($key))) {
return collect();
}

if (is_callable($class)) {
$result = $class($data);

return $result instanceof Collection ? $result : collect([$result]);
}

if (method_exists($class, 'collect')) {
$result = $class::collect($data);

if ($result instanceof Collection) {
return $result;
}

return is_array($result) ? collect($result) : collect([$result]);
}

return collect($data)->map(function (array $item) use ($class) {
if (method_exists($class, 'from')) {
return $class::from($item);
}

return new $class(...$item);
});
});
Response::macro($macros::name(), $macros::callback());
}
}

0 comments on commit 75c331e

Please sign in to comment.