-
Notifications
You must be signed in to change notification settings - Fork 64
Handle wildcard "+" segments #92
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,23 +21,62 @@ protected function match(AcceptHeader $accept, AcceptHeader $priority, $index) | |
| return null; | ||
| } | ||
|
|
||
| $ab = $accept->getBasePart(); | ||
| $pb = $priority->getBasePart(); | ||
| $acceptBase = $accept->getBasePart(); | ||
| $priorityBase = $priority->getBasePart(); | ||
|
|
||
| $as = $accept->getSubPart(); | ||
| $ps = $priority->getSubPart(); | ||
| $acceptSub = $accept->getSubPart(); | ||
| $prioritySub = $priority->getSubPart(); | ||
|
|
||
| $intersection = array_intersect_assoc($accept->getParameters(), $priority->getParameters()); | ||
|
|
||
| $baseEqual = !strcasecmp($ab, $pb); | ||
| $subEqual = !strcasecmp($as, $ps); | ||
| $baseEqual = !strcasecmp($acceptBase, $priorityBase); | ||
| $subEqual = !strcasecmp($acceptSub, $prioritySub); | ||
|
|
||
| if (($ab === '*' || $baseEqual) && ($as === '*' || $subEqual) && count($intersection) === count($accept->getParameters())) { | ||
| if (($acceptBase === '*' || $baseEqual) | ||
| && ($acceptSub === '*' || $subEqual) | ||
| && count($intersection) === count($accept->getParameters()) | ||
| ) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I split this condition across multiple lines to better understand the precedence of operations.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 |
||
| $score = 100 * $baseEqual + 10 * $subEqual + count($intersection); | ||
|
|
||
| return new Match($accept->getQuality() * $priority->getQuality(), $score, $index); | ||
| } | ||
|
|
||
| if (!strstr($acceptSub, '+') || !strstr($prioritySub, '+')) { | ||
| return null; | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If either the accept header or the priority do not have
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👌 |
||
|
|
||
| // Handle "+" segment wildcards | ||
| list($acceptSub, $acceptPlus) = $this->splitSubPart($acceptSub); | ||
| list($prioritySub, $priorityPlus) = $this->splitSubPart($prioritySub); | ||
|
|
||
| $subEqual = !strcasecmp($acceptSub, $prioritySub); | ||
| $plusEqual = !strcasecmp($acceptPlus, $priorityPlus); | ||
|
|
||
| if (($acceptBase === '*' || $baseEqual) | ||
| && ($acceptSub === '*' || $subEqual || $acceptPlus === '*' || $plusEqual) | ||
| && count($intersection) === count($accept->getParameters()) | ||
| ) { | ||
| $score = 100 * $baseEqual + 10 * $subEqual + $plusEqual + count($intersection); | ||
|
|
||
| return new Match($accept->getQuality() * $priority->getQuality(), $score, $index); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Split a subpart into the subpart and "plus" part. | ||
| * | ||
| * For media-types of the form "application/vnd.example+json", matching | ||
| * should allow wildcards for either the portion before the "+" or | ||
| * after. This method splits the subpart to allow such matching. | ||
| */ | ||
| protected function splitSubPart($subPart) | ||
| { | ||
| if (!strstr($subPart, '+')) { | ||
| return [$subPart, '']; | ||
| } | ||
|
|
||
| return explode('+', $subPart, 2); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed these variable names as I was confused by the two-character names when attempting to make my changes. I can revert these if desired, but I think they make the intent far clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep, perfect