willdurand / Negotiation

Content Negotiation tools for PHP.
https://williamdurand.fr/Negotiation/
MIT License
1.41k stars 62 forks source link

Handle wildcard "+" segments #92

Closed weierophinney closed 7 years ago

weierophinney commented 7 years ago

A relatively common way to allow negotation to multiple serialization structures for a single media type is to use "+" notation within the mediatype:

In particular, this is useful for determining what general serialization is used for an incoming request in order to select an appropriate deserializer.

With the current 2.X branch, I expected the following to work:

use Negotiation\Negotiator;

$mediaType = (new Negotiator)->getBest($accept, [
    'application/json',
    'application/*+json',
    'application/xml',
    'application/*+xml',
]);

However, it failed on each of the cases listed above.

This patch provides additional negotiation routines within Negotiation\Negotiator::match in order to allow such negotiation to work as expected.

willdurand commented 7 years ago

Hi @weierophinney, thanks for this great patch!

I will fix the Appveyor setup and trigger the build again for this PR but looks good to me :)

willdurand commented 7 years ago

Thanks!

willdurand commented 7 years ago

Released 2.3.0 with this feature!

meyerbaptiste commented 7 years ago

Hi @willdurand, @weierophinney! This PR and the new release (2.3) breaks our api-platform/core test suite. See https://travis-ci.org/api-platform/core/jobs/228775063 for example.

I have this accept header: application/hal+json. My priorities are:

[0] => "application/ld+json"
[1] => "application/hal+json"
[2] => "application/xml"
[3] => "text/xml"
[4] => "application/json"
[5] => "text/html"

Then, the AbstractNegotiator::getBest() method now returns application/ld+json instead of application/hal+json before.

It looks like something is not working properly here, nop? Thanks.

(Ping @dunglas)

willdurand commented 7 years ago

ah :(