mirror of
https://github.com/FriendsOfSymfony/FOSRest.git
synced 2026-03-24 08:42:15 +01:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02efd1e9f2 | ||
|
|
ec9b1d9a88 | ||
|
|
b773e2e270 | ||
|
|
9375f6cc8d | ||
|
|
e571ccbfc2 | ||
|
|
1d610cc01b | ||
|
|
1d0a0d5b3c | ||
|
|
b836af42da | ||
|
|
cf699ffe65 | ||
|
|
89306dd972 | ||
|
|
9a84df8220 | ||
|
|
0a10de75a2 | ||
|
|
304fa4fbfb | ||
|
|
33c35fa3f8 | ||
|
|
8ab683db49 | ||
|
|
b48857315c | ||
|
|
c85f7d157a | ||
|
|
b6348f7089 | ||
|
|
37a7e5591a | ||
|
|
88c8d64bcd |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
/phpunit.xml
|
||||
|
||||
composer.phar
|
||||
composer.phar
|
||||
/composer.lock
|
||||
/vendor/
|
||||
11
.travis.yml
11
.travis.yml
@@ -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
|
||||
|
||||
|
||||
59
Decoder/JsonToFormDecoder.php
Normal file
59
Decoder/JsonToFormDecoder.php
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
[](http://travis-ci.org/FriendsOfSymfony/FOSRest)
|
||||
|
||||
Deprecated in favor of https://github.com/willdurand/Negotiation and https://github.com/FriendsOfSymfony/FOSRestBundle
|
||||
|
||||
51
Tests/Decoder/JsonToFormDecoderTest.php
Normal file
51
Tests/Decoder/JsonToFormDecoderTest.php
Normal 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']);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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),
|
||||
);
|
||||
}}
|
||||
@@ -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;
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user