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
79 changes: 0 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,85 +817,6 @@ class YourApi extends Api
}
```

### Configure Options

It is very common for APIs to offer different options (like language, timezone, etc.).
To simplify the process of configuring options, the [`OptionsResolver`](https://symfony.com/doc/current/components/options_resolver.html) is available.
It allows you to create a set of default options and their constraints such as required options, default values, allowed types, etc.
It then resolves the given options `array` against these default options to ensure it meets all the constraints.

For example, if an API has a language and timezone options:

```php
use ProgrammatorDev\Api\Api;

class YourApi extends Api
{
private array $options = [];

public function __construct(array $options = [])
{
parent::__construct();

$this->options = $this->configureOptions($options);
$this->configureApi();
}

private function configureOptions(array $options): array
{
// set defaults values if none were provided
$this->optionsResolver->setDefault('timezone', 'UTC');
$this->optionsResolver->setDefault('language', 'en');

// set allowed types
$this->optionsResolver->setAllowedTypes('timezone', 'string');
$this->optionsResolver->setAllowedTypes('language', 'string');

// set allowed values
$this->optionsResolver->setAllowedValues('timezone', \DateTimeZone::listIdentifiers());
$this->optionsResolver->setAllowedValues('language', ['en', 'pt']);

// return resolved options
return $this->optionsResolver->resolve($options);
}

private function configureApi(): void
{
// set the base url
$this->setBaseUrl('https://api.example.com/v1');

// set options as query defaults (will be included in all requests)
$this->addQueryDefault('language', $this->options['language']);
$this->addQueryDefault('timezone', $this->options['timezone']);
}

public function getPosts(int $page = 1): string
{
// GET https://api.example.com/v1/posts?language=en&timezone=UTC&page=1
return $this->request(
method: 'GET',
path: '/posts',
query: [
'page' => $page
]
);
}
}
```

When using the API, it should look like this:

```php
$api = new YourApi([
'language' => 'pt'
]);

// GET https://api.example.com/v1/posts?language=pt&timezone=UTC&page=1
$posts = $api->getPosts();
```

For all available methods, check the official page [documentation](https://symfony.com/doc/current/components/options_resolver.html).

## Libraries using PHP API SDK

- [programmatordev/openweathermap-php-api](https://github.com/programmatordev/openweathermap-php-api)
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
"psr/http-factory": "^1.1",
"psr/http-factory-implementation": "*",
"psr/log": "^2.0|^3.0",
"symfony/event-dispatcher": "^6.4|^7.3",
"symfony/options-resolver": "^6.4|^7.3"
"symfony/event-dispatcher": "^6.4|^7.3"
},
"require-dev": {
"monolog/monolog": "^3.9",
Expand Down
4 changes: 0 additions & 4 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\OptionsResolver\OptionsResolver;

class Api
{
Expand All @@ -40,13 +39,10 @@ class Api

private EventDispatcher $eventDispatcher;

protected OptionsResolver $optionsResolver;

public function __construct()
{
$this->clientBuilder ??= new ClientBuilder();
$this->eventDispatcher = new EventDispatcher();
$this->optionsResolver = new OptionsResolver();
}

/**
Expand Down