Skip to content

Commit

Permalink
Merge pull request #9 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Added the ability to specify macros for a request object
  • Loading branch information
andrey-helldar authored Aug 23, 2024
2 parents b4fc952 + 8e4eeb4 commit 4e328ff
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 16 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,17 @@ Http::get()->toData(...); // will be method not found exception
## Usage

### As Class
### Available Methods


#### Response

- [toData](#todata)
- [toDataCollection](#todatacollection)

### Method Listing

#### toData()

The class instance will be returned.

Expand Down Expand Up @@ -117,7 +127,7 @@ class SomeData
return Http::get()->toData(SomeData::class);
```

### As Collection
#### toDataCollection()

The `Illuminate\Support\Collection` object or an object inherited from it will be returned.

Expand Down
9 changes: 7 additions & 2 deletions config/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

declare(strict_types=1);

use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro;
use DragonCode\LaravelHttpMacros\Macros\ToDataMacro;
use DragonCode\LaravelHttpMacros\Macros\Responses\ToDataCollectionMacro;
use DragonCode\LaravelHttpMacros\Macros\Responses\ToDataMacro;

return [
'macros' => [
'request' => [
// CustomMacro::class,
// 'toFoo' => CustomMacro::class,
],

'response' => [
ToDataMacro::class,
ToDataCollectionMacro::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

namespace DragonCode\LaravelHttpMacros\Macros;
namespace DragonCode\LaravelHttpMacros\Macros\Responses;

use Closure;
use DragonCode\LaravelHttpMacros\Macros\Macro;
use Illuminate\Support\Collection;

use function collect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

declare(strict_types=1);

namespace DragonCode\LaravelHttpMacros\Macros;
namespace DragonCode\LaravelHttpMacros\Macros\Responses;

use Closure;
use DragonCode\LaravelHttpMacros\Macros\Macro;

use function is_callable;
use function is_null;
Expand Down
40 changes: 32 additions & 8 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace DragonCode\LaravelHttpMacros;

use DragonCode\LaravelHttpMacros\Commands\GenerateHelperCommand;
use DragonCode\LaravelHttpMacros\Macros\Macro;
use Illuminate\Http\Client\Request;
use Illuminate\Http\Client\Response;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

Expand All @@ -22,8 +24,10 @@ public function register(): void
public function boot(): void
{
$this->publishConfig();
$this->bootMacros();
$this->bootCommands();

$this->bootRequestMacros();
$this->bootResponseMacros();
}

protected function bootCommands(): void
Expand All @@ -35,17 +39,37 @@ protected function bootCommands(): void
}
}

protected function bootMacros(): void
protected function bootRequestMacros(): void
{
foreach ($this->requestMacros() as $name => $macro) {
Request::macro($this->resolveName($name, $macro), $macro::callback());
}
}

protected function bootResponseMacros(): void
{
foreach ($this->macros() as $name => $macro) {
Response::macro(
name : is_string($name) ? $name : $macro::name(),
macro: $macro::callback()
);
foreach ($this->responseMacros() as $name => $macro) {
Response::macro($this->resolveName($name, $macro), $macro::callback());
}
}

protected function macros(): array
protected function resolveName(int|string $name, Macro|string $macro): string
{
return is_string($name) ? $name : $macro::name();
}

/**
* @return array<class-string|Macro>
*/
protected function requestMacros(): array
{
return config('http.macros.request', []);
}

/**
* @return array<class-string|Macro>
*/
protected function responseMacros(): array
{
return config('http.macros.response', []);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Tests;

use DragonCode\LaravelHttpMacros\Macros\ToDataCollectionMacro;
use DragonCode\LaravelHttpMacros\Macros\ToDataMacro;
use DragonCode\LaravelHttpMacros\Macros\Responses\ToDataCollectionMacro;
use DragonCode\LaravelHttpMacros\Macros\Responses\ToDataMacro;
use DragonCode\LaravelHttpMacros\ServiceProvider;
use Illuminate\Config\Repository;
use Orchestra\Testbench\TestCase as BaseTestCase;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 4e328ff

Please sign in to comment.