Skip to content

Upgrade http-accept library #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "tobyz/json-api-server",
"description": "A fully automated JSON:API server implementation in PHP.",
"require": {
"php": ">=7.1",
"php": ">=7.3",
"ext-json": "*",
"asispts/http-accept": "^1.0",
"doctrine/inflector": "^1.4 || ^2.0",
"json-api-php/json-api": "^2.2",
"nyholm/psr7": "^1.3",
"psr/http-message": "^1.0",
"psr/http-server-handler": "^1.0",
"hnet/http-accept": "^0.1"
"psr/http-server-handler": "^1.0"
},
"license": "MIT",
"authors": [
Expand Down
28 changes: 0 additions & 28 deletions phpunit.xml

This file was deleted.

30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
cacheResultFile=".phpunit.result.cache"
backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
failOnRisky="true"
failOnWarning="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="JSON:API Server Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
33 changes: 13 additions & 20 deletions src/JsonApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Tobyz\JsonApiServer;

use HttpAccept\AcceptParser;
use HttpAccept\ContentTypeParser;
use InvalidArgumentException;
use JsonApiPhp\JsonApi\ErrorDocument;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
Expand Down Expand Up @@ -224,26 +226,21 @@ private function getContentTypeExtensionUris(Request $request): array
return [];
}

$mediaList = (new AcceptParser())->parse($contentType);

if ($mediaList->count() > 1) {
try {
$type = (new ContentTypeParser())->parse($contentType);
} catch(InvalidArgumentException $exc){
throw new UnsupportedMediaTypeException();
}

$mediaType = $mediaList->preferredMedia(0);

if ($mediaType->mimetype() !== JsonApi::MEDIA_TYPE) {
if ($type->name() !== JsonApi::MEDIA_TYPE) {
throw new UnsupportedMediaTypeException();
}

$parameters = $mediaType->parameter();

if (! empty(array_diff(array_keys($parameters->all()), ['ext', 'profile']))) {
if (! empty(array_diff(array_keys($type->parameters()), ['ext', 'profile']))) {
throw new UnsupportedMediaTypeException();
}

$extensionUris = $parameters->has('ext') ? explode(' ', $parameters->get('ext')) : [];

$extensionUris = $type->hasParamater('ext') ? explode(' ', $type->getParameter('ext')) : [];
if (! empty(array_diff($extensionUris, array_keys($this->extensions)))) {
throw new UnsupportedMediaTypeException();
}
Expand All @@ -257,21 +254,17 @@ private function getAcceptableExtensionUris(Request $request): array
return [];
}

$mediaList = (new AcceptParser())->parse($accept);

foreach ($mediaList->all() as $mediaType) {
if (! in_array($mediaType->mimetype(), [JsonApi::MEDIA_TYPE, '*/*'])) {
$list = (new AcceptParser())->parse($accept);
foreach($list as $mediaType) {
if (!in_array($mediaType->name(), [JsonApi::MEDIA_TYPE, '*/*'])) {
continue;
}

$parameters = $mediaType->parameter();

if (! empty(array_diff(array_keys($parameters->all()), ['ext', 'profile']))) {
if (!empty(array_diff(array_keys($mediaType->parameters()), ['ext', 'profile']))) {
continue;
}

$extensionUris = $parameters->has('ext') ? explode(' ', $parameters->get('ext')) : [];

$extensionUris = $mediaType->hasParamater('ext') ? explode(' ', $mediaType->getParameter('ext')) : [];
if (! empty(array_diff($extensionUris, array_keys($this->extensions)))) {
continue;
}
Expand Down