Skip to content

Commit 842480a

Browse files
authored
Merge pull request #12 from programmatordev/PAS-9-remove-optionsresolver
Remove OptionsResolver
2 parents 9dd6ce8 + d2834a1 commit 842480a

File tree

3 files changed

+1
-85
lines changed

3 files changed

+1
-85
lines changed

README.md

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -817,85 +817,6 @@ class YourApi extends Api
817817
}
818818
```
819819

820-
### Configure Options
821-
822-
It is very common for APIs to offer different options (like language, timezone, etc.).
823-
To simplify the process of configuring options, the [`OptionsResolver`](https://symfony.com/doc/current/components/options_resolver.html) is available.
824-
It allows you to create a set of default options and their constraints such as required options, default values, allowed types, etc.
825-
It then resolves the given options `array` against these default options to ensure it meets all the constraints.
826-
827-
For example, if an API has a language and timezone options:
828-
829-
```php
830-
use ProgrammatorDev\Api\Api;
831-
832-
class YourApi extends Api
833-
{
834-
private array $options = [];
835-
836-
public function __construct(array $options = [])
837-
{
838-
parent::__construct();
839-
840-
$this->options = $this->configureOptions($options);
841-
$this->configureApi();
842-
}
843-
844-
private function configureOptions(array $options): array
845-
{
846-
// set defaults values if none were provided
847-
$this->optionsResolver->setDefault('timezone', 'UTC');
848-
$this->optionsResolver->setDefault('language', 'en');
849-
850-
// set allowed types
851-
$this->optionsResolver->setAllowedTypes('timezone', 'string');
852-
$this->optionsResolver->setAllowedTypes('language', 'string');
853-
854-
// set allowed values
855-
$this->optionsResolver->setAllowedValues('timezone', \DateTimeZone::listIdentifiers());
856-
$this->optionsResolver->setAllowedValues('language', ['en', 'pt']);
857-
858-
// return resolved options
859-
return $this->optionsResolver->resolve($options);
860-
}
861-
862-
private function configureApi(): void
863-
{
864-
// set the base url
865-
$this->setBaseUrl('https://api.example.com/v1');
866-
867-
// set options as query defaults (will be included in all requests)
868-
$this->addQueryDefault('language', $this->options['language']);
869-
$this->addQueryDefault('timezone', $this->options['timezone']);
870-
}
871-
872-
public function getPosts(int $page = 1): string
873-
{
874-
// GET https://api.example.com/v1/posts?language=en&timezone=UTC&page=1
875-
return $this->request(
876-
method: 'GET',
877-
path: '/posts',
878-
query: [
879-
'page' => $page
880-
]
881-
);
882-
}
883-
}
884-
```
885-
886-
When using the API, it should look like this:
887-
888-
```php
889-
$api = new YourApi([
890-
'language' => 'pt'
891-
]);
892-
893-
// GET https://api.example.com/v1/posts?language=pt&timezone=UTC&page=1
894-
$posts = $api->getPosts();
895-
```
896-
897-
For all available methods, check the official page [documentation](https://symfony.com/doc/current/components/options_resolver.html).
898-
899820
## Libraries using PHP API SDK
900821

901822
- [programmatordev/openweathermap-php-api](https://github.com/programmatordev/openweathermap-php-api)

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"psr/http-factory": "^1.1",
2525
"psr/http-factory-implementation": "*",
2626
"psr/log": "^2.0|^3.0",
27-
"symfony/event-dispatcher": "^6.4|^7.3",
28-
"symfony/options-resolver": "^6.4|^7.3"
27+
"symfony/event-dispatcher": "^6.4|^7.3"
2928
},
3029
"require-dev": {
3130
"monolog/monolog": "^3.9",

src/Api.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Psr\Http\Message\RequestInterface;
2121
use Psr\Http\Message\StreamInterface;
2222
use Symfony\Component\EventDispatcher\EventDispatcher;
23-
use Symfony\Component\OptionsResolver\OptionsResolver;
2423

2524
class Api
2625
{
@@ -40,13 +39,10 @@ class Api
4039

4140
private EventDispatcher $eventDispatcher;
4241

43-
protected OptionsResolver $optionsResolver;
44-
4542
public function __construct()
4643
{
4744
$this->clientBuilder ??= new ClientBuilder();
4845
$this->eventDispatcher = new EventDispatcher();
49-
$this->optionsResolver = new OptionsResolver();
5046
}
5147

5248
/**

0 commit comments

Comments
 (0)