-
Notifications
You must be signed in to change notification settings - Fork 703
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an option to normalize between underscore and camel case in the…
… body listener
- Loading branch information
Showing
11 changed files
with
385 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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\RestBundle\Normalizer; | ||
|
||
/** | ||
* Normalizes arrays. | ||
* | ||
* @author Florian Voutzinos <florian@voutzinos.com> | ||
*/ | ||
interface ArrayNormalizerInterface | ||
{ | ||
/** | ||
* Normalizes the array. | ||
* | ||
* @param array $data The array to normalize | ||
* | ||
* @return array The normalized array | ||
* | ||
* @throws Exception\NormalizationException | ||
*/ | ||
public function normalize(array $data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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\RestBundle\Normalizer; | ||
|
||
use FOS\RestBundle\Normalizer\Exception\NormalizationException; | ||
|
||
/** | ||
* Normalizes the array by changing its keys from underscore to camel case. | ||
* | ||
* @author Florian Voutzinos <florian@voutzinos.com> | ||
*/ | ||
class CamelKeysNormalizer implements ArrayNormalizerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize(array $data) | ||
{ | ||
$this->normalizeArray($data); | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Normalizes an array. | ||
* | ||
* @param array &$data | ||
* | ||
* @throws Exception\NormalizationException | ||
*/ | ||
private function normalizeArray(array &$data) | ||
{ | ||
foreach ($data as $key => $val) { | ||
$normalizedKey = $this->normalizeString($key); | ||
|
||
if ($normalizedKey !== $key) { | ||
if (array_key_exists($normalizedKey, $data)) { | ||
throw new NormalizationException(sprintf( | ||
'The key "%s" is invalid as it will override the existing key "%s"', | ||
$key, | ||
$normalizedKey | ||
)); | ||
} | ||
|
||
unset($data[$key]); | ||
$data[$normalizedKey] = $val; | ||
$key = $normalizedKey; | ||
} | ||
|
||
if (is_array($val)) { | ||
$this->normalizeArray($data[$key]); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Normalizes a string. | ||
* | ||
* @param string $string | ||
* | ||
* @return string | ||
*/ | ||
private function normalizeString($string) | ||
{ | ||
if (false === strpos($string, '_')) { | ||
return $string; | ||
} | ||
|
||
return preg_replace_callback('/_([a-zA-Z0-9])/', function ($matches) { | ||
return strtoupper($matches[1]); | ||
}, $string); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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\RestBundle\Normalizer\Exception; | ||
|
||
/** | ||
* Exception thrown when the normalization failed. | ||
* | ||
* @author Florian Voutzinos <florian@voutzinos.com> | ||
*/ | ||
class NormalizationException extends \RuntimeException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.