PHP code example of dragon-code / laravel-http-macros
1. Go to this page and download the library: Download dragon-code/laravel-http-macros library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dragon-code / laravel-http-macros example snippets
use Illuminate\Support\Facades\Http;
// Returns a SomeData object
return Http::get()->toData(SomeData::class);
// Will return a SomeData object generated from the JSON path
return Http::get()->toData(SomeData::class, 'data.item');
// Returns the result of the callback execution
return Http::get()->toData(
fn (array $data) => new SomeData(
$data['data']['item']['id'],
$data['data']['item']['title']
)
);
// Returns the result of the callback execution from a custom JSON path
return Http::get()->toData(
fn (array $data) => new SomeData($data['id'], $data['title']),
'data.item'
);
class SomeData
{
public function __construct(
public int $id,
public string $title
) {}
public static function from(array $data): static
{
return new static(...$data);
}
}
return Http::get()->toData(SomeData::class);
use Illuminate\Support\Facades\Http;
// Returns a collection of SomeData objects
return Http::get()->toDataCollection(SomeData::class);
// Returns a collection of SomeData objects formed from the JSON path
return Http::get()->toDataCollection(SomeData::class, 'data.item');
// Returns the result of the callback execution
return Http::get()->toDataCollection(
fn (array $data) => collect([
new SomeData(
$data['data']['item']['id'],
$data['data']['item']['title']
),
])
);
// Returns the result of the callback execution from a custom JSON path
return Http::get()->toDataCollection(
fn (array $data) => collect([
new SomeData(...$data),
]),
'data.item'
);
use Illuminate\Support\Collection;
class SomeData
{
public function __construct(
public int $id,
public string $title
) {}
public static function collect(array $items): Collection
{
return collect($items)->map(
fn (array $item) => new static(...$item)
);
}
}
return Http::get()->toDataCollection(SomeData::class);