diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 431f3be..b744b39 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: run: echo "::set-output name=dir::$(composer config cache-files-dir)" - name: Load Composer cache - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: ${{ steps.composer-cache.outputs.dir }} key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.deps }}-composer diff --git a/CHANGELOG.md b/CHANGELOG.md index ce38567..fbcd4cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +## Unreleased +### BREAKING +- Add QueryArrayNormalizer middleware to ClientBuilder + ## [v0.9.9] - 2024.06.06 ### Added - Add support to API key authorization diff --git a/composer.json b/composer.json index 3877141..202d8a9 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ }, "scripts": { "test": [ - "php-cs-fixer fix --dry-run -v", + "php-cs-fixer fix -v", "phpmd src text phpmd.xml.dist", "phpspec run --format=dot" ] diff --git a/src/Client/ClientBuilder.php b/src/Client/ClientBuilder.php index 4194b21..bf98a2b 100644 --- a/src/Client/ClientBuilder.php +++ b/src/Client/ClientBuilder.php @@ -7,6 +7,7 @@ use GuzzleHttp\HandlerStack; use Omikron\FactFinder\Communication\Client\Middleware\Authenticator; use Omikron\FactFinder\Communication\Client\Middleware\HttpErrors; +use Omikron\FactFinder\Communication\Client\Middleware\QueryArrayNormalizer; use Omikron\FactFinder\Communication\Credentials; use Omikron\FactFinder\Communication\ServerUrl; use Omikron\FactFinder\Communication\Version; @@ -57,6 +58,7 @@ public function build(): ClientInterface { $handler = HandlerStack::create(); $handler->push(new HttpErrors()); + $handler->push(new QueryArrayNormalizer()); $config = [ 'base_uri' => $this->serverUrl, diff --git a/src/Client/Middleware/QueryArrayNormalizer.php b/src/Client/Middleware/QueryArrayNormalizer.php new file mode 100644 index 0000000..48e2b84 --- /dev/null +++ b/src/Client/Middleware/QueryArrayNormalizer.php @@ -0,0 +1,47 @@ +getUri(); + $query = $uri->getQuery(); + + if (!$query) { + return $handler($request, $options); + } + + parse_str($query, $params); + + $normalized = []; + + foreach ($params as $key => $value) { + if (is_array($value)) { + foreach ($value as $v) { + $normalized[] = rawurlencode($key) . '=' . rawurlencode((string) $v); + } + + continue; + } + + $normalized[] = rawurlencode($key) . '=' . rawurlencode((string) $value); + } + + $newQuery = implode('&', $normalized); + + $request = $request->withUri( + $uri->withQuery($newQuery) + ); + + return $handler($request, $options); + }; + } +}