diff --git a/README.md b/README.md index 5bb596c..006705c 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/composer.json b/composer.json index fa1fde6..126c4d9 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Api.php b/src/Api.php index f477949..7a680a2 100644 --- a/src/Api.php +++ b/src/Api.php @@ -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 { @@ -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(); } /**