From 25063f6b1ff82fe0d91b92ce3c4d3537c15d2d90 Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 18 Aug 2024 16:56:17 +0000 Subject: [PATCH] Fixed MediaType::getQuality always returning 1.0. --- VERSION | 2 +- src/MediaType.php | 10 ++++++---- tests/MediaTypeTest.php | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 tests/MediaTypeTest.php diff --git a/VERSION b/VERSION index 09e8a76..65d4f31 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2408.181640 +0.2408.181655 diff --git a/src/MediaType.php b/src/MediaType.php index cd5a170..dcff15e 100644 --- a/src/MediaType.php +++ b/src/MediaType.php @@ -1,7 +1,7 @@ getParam('q', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); - if(!is_float($quality) && !is_int($quality)) - return 1; + if(is_string($quality) || is_int($quality)) + $quality = (float)$quality; + if(!is_float($quality)) + return 1.0; - return max(min(round($quality, 2), 1), 0); + return max(min(round($quality, 2), 1.0), 0.0); } /** diff --git a/tests/MediaTypeTest.php b/tests/MediaTypeTest.php new file mode 100644 index 0000000..b6230e4 --- /dev/null +++ b/tests/MediaTypeTest.php @@ -0,0 +1,32 @@ + 1.0, + 'application/xhtml+xml' => 1.0, + 'application/xml;q=0.9' => 0.9, + 'image/webp' => 1.0, + '*/*;q=0.8' => 0.8, + ]; + + foreach($types as $typeStr => $expectQuality) { + $type = MediaType::parse($typeStr); + $this->assertEquals($typeStr, (string)$type); + $this->assertEquals($expectQuality, $type->getQuality()); + } + } +}