Skip to content

Commit 69d6acf

Browse files
committed
re-integrate friendsofsymfony/rest
1 parent 71e0c24 commit 69d6acf

31 files changed

+372
-31
lines changed

Controller/ExceptionController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\Component\HttpFoundation\Request;
2121
use Symfony\Component\HttpFoundation\Response;
2222

23-
use FOS\Rest\Util\Codes;
23+
use FOS\RestBundle\Util\Codes;
2424
use FOS\RestBundle\View\ViewHandler;
2525
use FOS\RestBundle\View\View;
2626
use FOS\RestBundle\Util\ExceptionWrapper;

Controller/FOSRestController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use FOS\RestBundle\View\View;
1818
use FOS\RestBundle\View\RedirectView;
1919
use FOS\RestBundle\View\RouteRedirectView;
20-
use FOS\Rest\Util\Codes;
20+
use FOS\RestBundle\Util\Codes;
2121

2222
/**
2323
* Base Controller for Controllers using the View functionality of FOSRestBundle.

Decoder/ContainerDecoderProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Component\DependencyInjection\ContainerAware;
1515

16-
use FOS\Rest\Decoder\DecoderProviderInterface;
16+
use FOS\RestBundle\Decoder\DecoderProviderInterface;
1717

1818
/**
1919
* Provides encoders through the Symfony2 DIC
@@ -51,7 +51,7 @@ public function supports($format)
5151
* @param string $format format
5252
*
5353
* @throws \InvalidArgumentException
54-
* @return FOS\Rest\Decoder\DecoderInterface
54+
* @return FOS\RestBundle\Decoder\DecoderInterface
5555
*/
5656
public function getDecoder($format)
5757
{

Decoder/DecoderInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRest package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Decoder;
13+
14+
/**
15+
* Defines the interface of decoders
16+
*
17+
* @author Jordi Boggiano <[email protected]>
18+
*/
19+
interface DecoderInterface
20+
{
21+
/**
22+
* Decodes a string into PHP data
23+
*
24+
* @param string $data data to decode
25+
* @return array|Boolean false in case the content could not be decoded, else an array
26+
*/
27+
function decode($data);
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRest package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Decoder;
13+
14+
/**
15+
* Defines the interface of decoder providers
16+
*
17+
* @author Igor Wiedler <[email protected]>
18+
*/
19+
interface DecoderProviderInterface
20+
{
21+
/**
22+
* Check if a certain format is supported.
23+
*
24+
* @param string $format Format for the requested decoder.
25+
* @return Boolean
26+
*/
27+
function supports($format);
28+
29+
/**
30+
* Provides decoders, possibly lazily.
31+
*
32+
* @param string $format Format for the requested decoder.
33+
* @return FOS\RestBundle\Decoder\DecoderInterface
34+
*/
35+
function getDecoder($format);
36+
}

Decoder/JsonDecoder.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRest package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Decoder;
13+
14+
/**
15+
* Decodes JSON data
16+
*
17+
* @author Jordi Boggiano <[email protected]>
18+
*/
19+
class JsonDecoder implements DecoderInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function decode($data)
25+
{
26+
return @json_decode($data, true);
27+
}
28+
}

Decoder/JsonToFormDecoder.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRest package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Decoder;
13+
14+
use FOS\RestBundle\Decoder\DecoderInterface;
15+
16+
/**
17+
* Decodes JSON data and make it compliant with application/x-www-form-encoded style
18+
*
19+
* @author Kévin Dunglas <[email protected]>
20+
*/
21+
class JsonToFormDecoder implements DecoderInterface
22+
{
23+
24+
/**
25+
* Makes data decoded from JSON application/x-www-form-encoded compliant
26+
*
27+
* @param array $data
28+
*/
29+
private function xWwwFormEncodedLike(&$data)
30+
{
31+
foreach ($data as $key => &$value) {
32+
if (is_array($value)) {
33+
// Encode recursively
34+
$this->xWwwFormEncodedLike($value);
35+
} elseif (false === $value) {
36+
// Checkbox-like behavior: remove false data
37+
unset($data[$key]);
38+
} elseif (!is_string($value)) {
39+
// Convert everyting to string
40+
// true values will be converted to '1', this is the default checkbox behavior
41+
$value = strval($value);
42+
}
43+
}
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function decode($data)
50+
{
51+
$decodedData = @json_decode($data, true);
52+
if ($decodedData) {
53+
$this->xWwwFormEncodedLike($decodedData);
54+
}
55+
56+
return $decodedData;
57+
}
58+
59+
}

Decoder/XmlDecoder.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRest package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Decoder;
13+
14+
/**
15+
* Decodes XML data
16+
*
17+
* @author Jordi Boggiano <[email protected]>
18+
* @author John Wards <[email protected]>
19+
* @author Fabian Vogler <[email protected]>
20+
*/
21+
class XmlDecoder implements DecoderInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function decode($data)
27+
{
28+
$xml = @simplexml_load_string($data);
29+
if (!$xml) {
30+
return;
31+
}
32+
33+
if (!$xml->count()) {
34+
if (!$xml->attributes()) {
35+
return (string) $xml;
36+
}
37+
$data = array();
38+
foreach ($xml->attributes() as $attrkey => $attr) {
39+
$data['@'.$attrkey] = (string) $attr;
40+
}
41+
$data['#'] = (string) $xml;
42+
43+
return $data;
44+
}
45+
46+
return $this->parseXml($xml);
47+
}
48+
49+
/**
50+
* Parse the input SimpleXmlElement into an array
51+
*
52+
* @param \SimpleXmlElement $node xml to parse
53+
* @return array
54+
*/
55+
private function parseXml(\SimpleXmlElement $node)
56+
{
57+
$data = array();
58+
if ($node->attributes()) {
59+
foreach ($node->attributes() as $attrkey => $attr) {
60+
$data['@'.$attrkey] = (string) $attr;
61+
}
62+
}
63+
foreach ($node->children() as $key => $subnode) {
64+
if ($subnode->count()) {
65+
$value = $this->parseXml($subnode);
66+
} elseif ($subnode->attributes()) {
67+
$value = array();
68+
foreach ($subnode->attributes() as $attrkey => $attr) {
69+
$value['@'.$attrkey] = (string) $attr;
70+
}
71+
$value['#'] = (string) $subnode;
72+
} else {
73+
$value = (string) $subnode;
74+
}
75+
76+
if ($key === 'item') {
77+
if (isset($value['@key'])) {
78+
$data[(string)$value['@key']] = $value['#'];
79+
} elseif (isset($data['item'])) {
80+
$tmp = $data['item'];
81+
unset($data['item']);
82+
$data[] = $tmp;
83+
$data[] = $value;
84+
}
85+
} elseif (array_key_exists($key, $data)) {
86+
if ((false === is_array($data[$key])) || (false === isset($data[$key][0]))) {
87+
$data[$key] = array($data[$key]);
88+
}
89+
$data[$key][] = $value;
90+
} else {
91+
$data[$key] = $value;
92+
}
93+
}
94+
95+
return $data;
96+
}
97+
}

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1616
use Symfony\Component\Config\Definition\ConfigurationInterface;
1717

18-
use FOS\Rest\Util\Codes;
18+
use FOS\RestBundle\Util\Codes;
1919

2020
/**
2121
* This class contains the configuration information for the bundle

DependencyInjection/FOSRestExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use Symfony\Component\DependencyInjection\Reference;
2121
use Symfony\Component\HttpKernel\Kernel;
2222

23-
use FOS\Rest\Util\Codes;
23+
use FOS\RestBundle\Util\Codes;
2424

2525
use FOS\RestBundle\FOSRestBundle;
2626

@@ -83,12 +83,12 @@ public function load(array $configs, ContainerBuilder $container)
8383
$container->setParameter($this->getAlias().'.force_redirects', $config['view']['force_redirects']);
8484

8585
if (!is_numeric($config['view']['failed_validation'])) {
86-
$config['view']['failed_validation'] = constant('\FOS\Rest\Util\Codes::'.$config['view']['failed_validation']);
86+
$config['view']['failed_validation'] = constant('\FOS\RestBundle\Util\Codes::'.$config['view']['failed_validation']);
8787
}
8888
$container->setParameter($this->getAlias().'.failed_validation', $config['view']['failed_validation']);
8989

9090
if (!is_numeric($config['view']['empty_content'])) {
91-
$config['view']['empty_content'] = constant('\FOS\Rest\Util\Codes::'.$config['view']['empty_content']);
91+
$config['view']['empty_content'] = constant('\FOS\RestBundle\Util\Codes::'.$config['view']['empty_content']);
9292
}
9393
$container->setParameter($this->getAlias().'.empty_content', $config['view']['empty_content']);
9494

@@ -104,7 +104,7 @@ public function load(array $configs, ContainerBuilder $container)
104104

105105
foreach ($config['exception']['codes'] as $exception => $code) {
106106
if (!is_numeric($code)) {
107-
$config['exception']['codes'][$exception] = constant("\FOS\Rest\Util\Codes::$code");
107+
$config['exception']['codes'][$exception] = constant("\FOS\RestBundle\Util\Codes::$code");
108108
}
109109
$this->testExceptionExists($exception);
110110
}

0 commit comments

Comments
 (0)