Skip to content

Commit 7abae77

Browse files
committed
docs: added configure options section
1 parent 1ad9fd0 commit 7abae77

1 file changed

Lines changed: 80 additions & 1 deletion

File tree

README.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ use ProgrammatorDev\Api\Builder\LoggerBuilder;
722722
$this->getLoggerBuilder(): LoggerBuilder;
723723
```
724724

725-
As an example:
725+
As an example, if you wanted to save log into a file:
726726

727727
```php
728728
use ProgrammatorDev\Api\Api;
@@ -763,6 +763,85 @@ $api->setClientBuilder(
763763
);
764764
```
765765

766+
### Configure Options
767+
768+
It is very common for APIs to offer different options (like language, timezone, etc.).
769+
To simplify the process of configuring options, the [`OptionsResolver`](https://symfony.com/doc/current/components/options_resolver.html) is available.
770+
It allows you to create a set of default options and their constraints such as required options, default values, allowed types, etc.
771+
It then resolves the given options `array` against these default options to ensure it meets all the constraints.
772+
773+
For example, if an API has a language and timezone options:
774+
775+
```php
776+
use ProgrammatorDev\Api\Api;
777+
778+
class YourApi extends Api
779+
{
780+
private array $options = [];
781+
782+
public function __construct(array $options = [])
783+
{
784+
parent::__construct();
785+
786+
$this->configureOptions($options);
787+
$this->configureApi();
788+
}
789+
790+
private function configureOptions(array $options): void
791+
{
792+
// set defaults values, if none were provided
793+
$this->optionsResolver->setDefault('timezone', 'UTC');
794+
$this->optionsResolver->setDefault('language', 'en');
795+
796+
// set allowed types
797+
$this->optionsResolver->setAllowedTypes('timezone', 'string');
798+
$this->optionsResolver->setAllowedTypes('language', 'string');
799+
800+
// set allowed values
801+
$this->optionsResolver->setAllowedValues('timezone', \DateTimeZone::listIdentifiers());
802+
$this->optionsResolver->setAllowedValues('language', ['en', 'pt']);
803+
804+
// resolve and set to options property
805+
$this->options = $this->optionsResolver->resolve($options);
806+
}
807+
808+
private function configureApi(): void
809+
{
810+
// set required base url
811+
$this->setBaseUrl('https://api.example.com/v1');
812+
// set options as query defaults (will be included in all requests)
813+
$this->addQueryDefault('language', $this->options['language']);
814+
$this->addQueryDefault('timezone', $this->options['timezone']);
815+
}
816+
817+
public function getPosts(int $page = 1): string
818+
{
819+
// GET https://api.example.com/v1/posts?language=en&timezone=UTC&page=1
820+
return $this->request(
821+
method: 'GET',
822+
path: '/posts',
823+
query: [
824+
'page' => $page
825+
]
826+
);
827+
}
828+
}
829+
```
830+
831+
For the end user, it should look like this:
832+
833+
```php
834+
$api = new YourApi([
835+
'language' => 'pt',
836+
'timezone' => 'Europe/Lisbon'
837+
]);
838+
839+
// GET https://api.example.com/v1/posts?language=pt&timezone=Europe/Lisbon&page=1
840+
$posts = $api->getPosts();
841+
```
842+
843+
For all available methods, check the official page documentation [here](https://symfony.com/doc/current/components/options_resolver.html).
844+
766845
## Cookbook
767846

768847
TODO

0 commit comments

Comments
 (0)