20 Commits

Author SHA1 Message Date
Lukas Kahwe Smith
02efd1e9f2 note about the code being deprecated 2013-10-12 22:34:05 +02:00
Lukas Kahwe Smith
ec9b1d9a88 preparing a stable release 2013-10-10 20:08:23 +02:00
Lukas Kahwe Smith
b773e2e270 added note about https://github.com/symfony/symfony/pull/8820 2013-08-23 17:34:55 +02:00
Lukas Kahwe Smith
9375f6cc8d go to 0.9.x dev 2013-07-28 17:11:31 +02:00
Lukas Kahwe Smith
e571ccbfc2 Merge pull request #17 from FriendsOfSymfony/remove_content_negotiation
Remove content negotiation
2013-07-28 01:10:50 -07:00
Lukas Kahwe Smith
1d610cc01b removed content negotiation in favor of willdurand/negotiation 2013-07-26 16:30:31 +02:00
Lukas Kahwe Smith
1d0a0d5b3c updated test setup 2013-07-26 16:00:12 +02:00
Lukas Kahwe Smith
b836af42da Merge pull request #15 from teohhanhui/patch-1
Do not return highest priority format immediately when catch-all is dete...
2013-07-24 11:23:56 -07:00
Teoh Han Hui
cf699ffe65 Update FormatNegotiator.php
Fixed undefined variable issue.
2013-07-16 19:09:48 +08:00
Teoh Han Hui
89306dd972 Do not return highest priority format immediately when catch-all is detected
Related to issue 372

From the HTTP/1.1 spec (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html):

Media ranges can be overridden by more specific media ranges or specific media types. If more than one media range applies to a given type, the most specific reference has precedence. For example,

       Accept: text/*, text/html, text/html;level=1, */*
have the following precedence:

       1) text/html;level=1
       2) text/html
       3) text/*
       4) */*
2013-07-09 18:14:32 +08:00
Christophe Coevoet
9a84df8220 Changed the composer constraint to allow Sf 2.3+
The HttpFoundation component is stable
2013-03-02 21:35:05 +01:00
Lukas Kahwe Smith
0a10de75a2 Merge pull request #13 from dunglas/json_form
Add JSON to form format decoder
2013-03-01 21:23:59 -08:00
Kévin Dunglas
304fa4fbfb Add JSON to form format decoder 2013-02-09 12:04:26 +01:00
Lukas Kahwe Smith
33c35fa3f8 Merge pull request #12 from seegno/fix-deprecated-call
Fixed Request::splitHttpAcceptHeader deprecated call
2013-01-18 02:39:23 -08:00
Rui Silva
8ab683db49 Fixed Request::splitHttpAcceptHeader deprecated call
wip
2013-01-18 10:07:40 +00:00
Lukas Kahwe Smith
b48857315c copy paste fix 2012-09-08 23:12:46 +03:00
lsmith77
c85f7d157a tweak test setup 2012-09-08 19:08:01 +02:00
lsmith77
b6348f7089 tweak travis setup 2012-09-08 19:06:16 +02:00
lsmith77
37a7e5591a tweaked travis setup 2012-09-08 19:01:18 +02:00
lsmith77
88c8d64bcd begin work on 0.8.0 2012-09-08 18:57:29 +02:00
11 changed files with 131 additions and 177 deletions

4
.gitignore vendored
View File

@@ -1,3 +1,5 @@
/phpunit.xml
composer.phar
composer.phar
/composer.lock
/vendor/

View File

@@ -3,10 +3,17 @@ language: php
php:
- 5.3
- 5.4
- 5.5
env:
- SYMFONY_VERSION=2.0.*
- SYMFONY_VERSION=2.1.*
- SYMFONY_VERSION=2.2.*
- SYMFONY_VERSION=2.3.*
- SYMFONY_VERSION=dev-master
before_script:
- wget -nc http://getcomposer.org/composer.phar
- php composer.phar install
- composer require symfony/http-foundation:${SYMFONY_VERSION}
script: phpunit --coverage-text

View File

@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Decoder;
use FOS\Rest\Decoder\DecoderInterface;
/**
* Decodes JSON data and make it compliant with application/x-www-form-encoded style
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class JsonToFormDecoder implements DecoderInterface
{
/**
* Makes data decoded from JSON application/x-www-form-encoded compliant
*
* @param array $data
*/
private function xWwwFormEncodedLike(&$data)
{
foreach ($data as $key => &$value) {
if (is_array($value)) {
// Encode recursively
$this->xWwwFormEncodedLike($value);
} elseif (false === $value) {
// Checkbox-like behavior: remove false data
unset($data[$key]);
} elseif (!is_string($value)) {
// Convert everyting to string
// true values will be converted to '1', this is the default checkbox behavior
$value = strval($value);
}
}
}
/**
* {@inheritdoc}
*/
public function decode($data)
{
$decodedData = @json_decode($data, true);
if ($decodedData) {
$this->xWwwFormEncodedLike($decodedData);
}
return $decodedData;
}
}

View File

@@ -8,3 +8,5 @@ The main goals for now are to provide ways for decoding request bodies
and request format negotiation (inspired by Apache mod_negotiation)
[![Build Status](https://secure.travis-ci.org/FriendsOfSymfony/FOSRest.png)](http://travis-ci.org/FriendsOfSymfony/FOSRest)
Deprecated in favor of https://github.com/willdurand/Negotiation and https://github.com/FriendsOfSymfony/FOSRestBundle

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Tests\Decoder;
use FOS\Rest\Decoder\JsonToFormDecoder;
/**
* Tests the form like encoder
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class JsonToFormDecoderTest extends \PHPUnit_Framework_TestCase
{
public function testDecode()
{
$data = array(
'arrayKey' => array(
'falseKey' => false,
'stringKey' => 'foo'
),
'falseKey' => false,
'trueKey' => true,
'intKey' => 69,
'floatKey' => 3.14,
'stringKey' => 'bar',
);
$decoder = new JsonToFormDecoder();
$decoded = $decoder->decode(json_encode($data));
$this->assertTrue(is_array($decoded));
$this->assertTrue(is_array($decoded['arrayKey']));
$this->assertArrayNotHasKey('falseKey', $decoded['arrayKey']);
$this->assertEquals('foo', $decoded['arrayKey']['stringKey']);
$this->assertArrayNotHasKey('falseKey', $decoded);
$this->assertEquals('1', $decoded['trueKey']);
$this->assertEquals('69', $decoded['intKey']);
$this->assertEquals('3.14', $decoded['floatKey']);
$this->assertEquals('bar', $decoded['stringKey']);
}
}

View File

@@ -1,49 +0,0 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Tests\Util;
use FOS\Rest\Util\FormatNegotiator;
use Symfony\Component\HttpFoundation\Request;
class FormatNegotiatorTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getData
*/
public function testGetBestFormat($acceptHeader, $format, $priorities, $preferExtension, $expected)
{
$request = new Request();
$request->headers->set('Accept', $acceptHeader);
$request->attributes->set('_format', $format);
$formatNegotiator = new FormatNegotiator();
$this->assertEquals($expected, $formatNegotiator->getBestFormat($request, $priorities, $preferExtension));
}
public function getData()
{
return array(
array(null, null, array('html', 'json', '*/*'), false, null),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', null, array('html', 'json', '*/*'), false, 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('html', 'json', '*/*'), false, 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('html', 'json', '*/*'), true, 'json'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('rss', '*/*'), false, 'html'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('xml'), false, 'xml'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('json', 'xml'), false, 'xml'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'json', array('json'), false, 'json'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*', 'json', array('json'), false, 'json'),
array('text/html,application/xhtml+xml,application/xml;q=0.9,*/*', null, array('json'), false, 'json'),
array('text/html,application/xhtml+xml,application/xml', null, array('json'), false, null),
);
}}

View File

@@ -9,21 +9,9 @@
* file that was distributed with this source code.
*/
$file = __DIR__.'/../vendor/.composer/autoload.php';
$file = __DIR__.'/../vendor/autoload.php';
if (!file_exists($file)) {
throw new RuntimeException('Install dependencies to run test suite.');
}
require_once $file;
spl_autoload_register(function($class)
{
if (0 === strpos($class, 'FOS\\Rest\\')) {
$path = __DIR__ . '/../' . implode('/', array_slice(explode('\\', $class), 2)) . '.php';
if (!stream_resolve_include_path($path)) {
return false;
}
require_once $path;
return true;
}
});
$autoload = require_once $file;

View File

@@ -19,6 +19,9 @@ namespace FOS\Rest\Util;
* (last updated 2012-02-13).
*
* Unless otherwise noted, the status code is defined in RFC2616.
*
* Note: Symfony 2.4 will have the same constants in the Response class:
* https://github.com/symfony/symfony/pull/8820
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Markus Lanthaler <markus.lanthaler@gmx.net>

View File

@@ -1,89 +0,0 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Util;
use Symfony\Component\HttpFoundation\Request;
class FormatNegotiator implements FormatNegotiatorInterface
{
/**
* Detect the request format based on the priorities and the Accept header
*
* Note: Request "_format" parameter is considered the preferred Accept header
*
* @param Request $request The request
* @param array $priorities Ordered array of formats (highest priority first)
* @param Boolean $preferExtension If to consider the extension last or first
*
* @return void|string The format string
*/
public function getBestFormat(Request $request, array $priorities, $preferExtension = false)
{
$mimetypes = $request->splitHttpAcceptHeader($request->headers->get('Accept'));
$extension = $request->get('_format');
if (null !== $extension && $request->getMimeType($extension)) {
$mimetypes[$request->getMimeType($extension)] = $preferExtension
? reset($mimetypes)+1
: end($mimetypes)-1;
arsort($mimetypes);
}
if (empty($mimetypes)) {
return null;
}
$catchAllEnabled = in_array('*/*', $priorities);
return $this->getFormatByPriorities($request, $mimetypes, $priorities, $catchAllEnabled);
}
/**
* Get the format applying the supplied priorities to the mime types
*
* @param Request $request The request
* @param array $mimetypes Ordered array of mimetypes as keys with priroties s values
* @param array $priorities Ordered array of formats (highest priority first)
* @param Boolean $catchAllEnabled If there is a catch all priority
*
* @return void|string The format string
*/
protected function getFormatByPriorities($request, $mimetypes, $priorities, $catchAllEnabled = false)
{
$max = reset($mimetypes);
$keys = array_keys($mimetypes, $max);
$formats = array();
foreach ($keys as $mimetype) {
unset($mimetypes[$mimetype]);
if ($mimetype === '*/*') {
return reset($priorities);
}
$format = $request->getFormat($mimetype);
if ($format) {
$priority = array_search($format, $priorities);
if (false !== $priority) {
$formats[$format] = $priority;
} elseif ($catchAllEnabled) {
$formats[$format] = count($priorities);
}
}
}
if (empty($formats) && !empty($mimetypes)) {
return $this->getFormatByPriorities($request, $mimetypes, $priorities);
}
asort($formats);
return key($formats);
}
}

View File

@@ -1,19 +0,0 @@
<?php
/*
* This file is part of the FOSRest package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\Rest\Util;
use Symfony\Component\HttpFoundation\Request;
interface FormatNegotiatorInterface
{
function getBestFormat(Request $request, array $availableTypes);
}

View File

@@ -16,8 +16,7 @@
}
],
"require": {
"php": ">=5.3.2",
"symfony/http-foundation": ">=2.0,<2.2-dev"
"php": ">=5.3.3"
},
"autoload": {
"psr-0": { "FOS\\Rest": "" }
@@ -25,7 +24,7 @@
"target-dir": "FOS/Rest",
"extra": {
"branch-alias": {
"dev-master": "0.7.x-dev"
"dev-master": "1.0-dev"
}
}
}