Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,34 @@ echo $workflow->get("jobs.test.runs-on");
echo $workflow->get("on.0"); // push , the first event
```

### Loading Data from JSON URL via Symfony HttpClient

If you want more control over the HTTP request (headers, authentication, timeouts, retries, etc.) or your environment restricts PHP stream functions (for example `allow_url_fopen=0`), you can use `fromHttpJsonUrl()`.
This method relies on your Symfony HttpClient implementation and allows you to pass any request options supported by the client.

```php
use HiFolks\DataType\Block;
use HiFolks\DataType\Enums\Operator;
use Symfony\Component\HttpClient\HttpClient;

$url = "https://api.github.com/repos/hi-folks/data-block/commits";
$client = HttpClient::create();

$commits = Block::fromHttpJsonUrl($url, $client, [
'headers' => [
'User-Agent' => 'my-app',
'Accept' => 'application/json',
],
]);

$myCommits = $commits->where("commit.author.name", Operator::LIKE, "Roberto");
foreach ($myCommits as $value) {
echo $value->get("commit.message") . PHP_EOL;
}
```
Don't forget to install the client's implementation `composer require symfony/http-client`


## Adding and appending elements

### Appending the elements of a Block object to another Block object
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
"require": {
"php": "^8.2|^8.3|^8.4",
"swaggest/json-schema": "^0.12.42",
"symfony/http-client-contracts": "^3.4.4",
"symfony/yaml": "^6.4|^7.1"
},
"require-dev": {
"laravel/pint": "^1.2",
"pestphp/pest": "^3.0",
"phpstan/phpstan": "^2",
"rector/rector": "^2"
"rector/rector": "^2",
"symfony/http-client": "^7.4"
},
"suggest": {
"symfony/http-client": "HTTP client implementation"
},
"autoload": {
"psr-4": {
Expand Down
15 changes: 15 additions & 0 deletions src/Traits/LoadableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace HiFolks\DataType\Traits;

use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\HttpClient\HttpClientInterface;

trait LoadableBlock
{
Expand Down Expand Up @@ -56,6 +57,20 @@ public static function fromJsonUrl(string $jsonUrl, ?array $headers = null): sel

}

/**
* @param array<string, mixed> $options Symfony client's request options
*/
public static function fromHttpJsonUrl(string $jsonUrl, HttpClientInterface $client, array $options = []): self
{
$content = $client->request('GET', $jsonUrl, $options)->getContent(false);

if ('' === $content) {
return self::make([]);
}

return self::fromJsonString($content);
}

public static function fromYamlFile(string $yamlFile): self
{
if (file_exists($yamlFile)) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Feature/UrlTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use HiFolks\DataType\Block;
use Symfony\Component\HttpClient\HttpClient;

it('remote json', function (): void {

Expand All @@ -15,6 +16,20 @@

})->group("url");

it('remote json (Symfony\Contracts\HttpClient\HttpClientInterface)', function (): void {
$url = "https://api.github.com/repos/hi-folks/data-block/commits";

$commits = Block::fromHttpJsonUrl($url, HttpClient::create());
expect($commits)->toBeInstanceOf(Block::class);
expect($commits)->toHaveCount(30);

$myCommits = $commits->where("commit.author.name", "like", "Roberto");
foreach ($myCommits as $value) {
expect($value->get("commit.message"))->toBeString();
}

})->group("url");

it('remote dummyjson post', function (): void {

$url = "https://dummyjson.com/posts";
Expand Down