Skip to content

Commit

Permalink
Merge branch 1.8
Browse files Browse the repository at this point in the history
Conflicts:
	Negotiation/FormatNegotiator.php
	Resources/config/body_listener.xml
	Resources/config/util.xml
	Resources/doc/3-listener-support.rst
	Resources/doc/format_listener.rst
	Tests/EventListener/FormatListenerTest.php
	Tests/EventListener/MimeTypeListenerTest.php
	Tests/Negotiation/FormatNegotiatorTest.php
	Tests/Routing/Loader/LoaderTest.php
	Tests/Validator/ViolationFormatterTest.php
	Util/FormatNegotiator.php
	Util/MediaTypeNegotiatorInterface.php
	Validator/ViolationFormatter.php
	Version/VersionResolverInterface.php
  • Loading branch information
GuilhemN committed Dec 20, 2015
2 parents 6ebbd6d + 2666f11 commit dc44ee0
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 3 deletions.
13 changes: 11 additions & 2 deletions Normalizer/CamelKeysNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,23 @@ private function normalizeArray(array &$data)
*
* @return string
*/
private function normalizeString($string)
protected function normalizeString($string)
{
if (false === strpos($string, '_')) {
return $string;
}

return preg_replace_callback('/_([a-zA-Z0-9])/', function ($matches) {
if (preg_match('/^(_+)(.*)/', $string, $matches)) {
$underscorePrefix = $matches[1];
$string = $matches[2];
} else {
$underscorePrefix = '';
}

$string = preg_replace_callback('/_([a-zA-Z0-9])/', function ($matches) {
return strtoupper($matches[1]);
}, $string);

return $underscorePrefix.$string;
}
}
45 changes: 45 additions & 0 deletions Normalizer/CamelKeysNormalizerWithLeadingUnderscore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the FOSRestBundle 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\RestBundle\Normalizer;

/**
* Normalizes the array by changing its keys from underscore to camel case, while
* leaving leading underscores unchanged.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class CamelKeysNormalizerWithLeadingUnderscore extends CamelKeysNormalizer
{
/**
* Normalizes a string while leaving leading underscores unchanged.
*
* @param string $string
*
* @return string
*/
protected function normalizeString($string)
{
if (false === strpos($string, '_')) {
return $string;
}

$offset = strspn($string, '_');
if ($offset) {
$underscorePrefix = substr($string, 0, $offset);
$string = substr($string, $offset);
} else {
$underscorePrefix = '';
}

return $underscorePrefix.parent::normalizeString($string);
}
}
2 changes: 2 additions & 0 deletions Resources/config/body_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<service id="fos_rest.normalizer.camel_keys" class="FOS\RestBundle\Normalizer\CamelKeysNormalizer" />

<service id="fos_rest.normalizer.camel_keys_with_leading_underscore" class="FOS\RestBundle\Normalizer\CamelKeysNormalizerWithLeadingUnderscore" />

<service id="fos_rest.decoder.json" class="FOS\RestBundle\Decoder\JsonDecoder" />

<service id="fos_rest.decoder.jsontoform" class="FOS\RestBundle\Decoder\JsonToFormDecoder" />
Expand Down
5 changes: 4 additions & 1 deletion Resources/doc/3-listener-support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ to enable a few additional listeners:
fos_rest:
param_fetcher_listener: true
body_listener: true
format_listener: true
format_listener:
enabled: true
rules:
- { path: '^/', priorities: ['json', 'xml'], fallback_format: 'html' }
versioning: true
view:
view_response_listener: 'force'
Expand Down
5 changes: 5 additions & 0 deletions Resources/doc/body_listener.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ to camel cased ones, you can use the ``camel_keys`` array normalizer:
body_listener:
array_normalizer: fos_rest.normalizer.camel_keys
.. note::

If you want to ignore leading underscores, for example in ``_username`` you can
instead use the ``fos_rest.normalizer.camel_keys_with_leading_underscore`` service.

Sometimes an array contains a key, which once normalized, will override an
existing array key. For example ``foo_bar`` and ``foo_Bar`` will both lead to
``fooBar``. If the normalizer receives this data, the listener will throw a
Expand Down
2 changes: 2 additions & 0 deletions Resources/doc/format_listener.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Setting ``priorities`` to a non-empty array enables Accept header negotiations.
# app/config/config.yml
fos_rest:
format_listener:
enabled: true
rules:
# setting fallback_format to json means that instead of considering the next rule in case of a priority mismatch, json will be used
- { path: '^/', host: 'api.%domain%', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
Expand Down Expand Up @@ -102,6 +103,7 @@ format will remain unchanged.
# app/config/config.yml
fos_rest:
format_listener:
enabled: true
rules:
- { path: '^/api', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: false }
- { path: '^/', stop: true } # Available for version >= 1.5
18 changes: 18 additions & 0 deletions Tests/Normalizer/CamelKeysNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace FOS\RestBundle\Tests\Normalizer;

use FOS\RestBundle\Normalizer\CamelKeysNormalizer;
use FOS\RestBundle\Normalizer\CamelKeysNormalizerWithLeadingUnderscore;

class CamelKeysNormalizerTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -52,4 +53,21 @@ public function normalizeProvider()
],
];
}

/**
* @dataProvider normalizeProvider
*/
public function testNormalizeLeadingUnderscore(array $array, array $expected)
{
$normalizer = new CamelKeysNormalizerWithLeadingUnderscore();
$this->assertEquals($expected, $normalizer->normalize($array));
}

public function normalizeProviderLeadingUnderscore()
{
$array = $this->normalizeProvider();
$array[] = array(array('__username' => 'foo', '_password' => 'bar', '_foo_bar' => 'foobar'), array('__username' => 'foo', '_password' => 'bar', '_fooBar' => 'foobar'));

return $array;
}
}

0 comments on commit dc44ee0

Please sign in to comment.