Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit e55a4a2

Browse files
committed
Merge branch 'hotfix/43' into develop
Forward port #43
2 parents 873493e + 7e2fd0a commit e55a4a2

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ All notable changes to this project will be documented in this file, in reverse
4545
- [#93](https://github.com/zendframework/zend-mvc/pull/93) ensures that the
4646
Console `Catchall` route factory will not fail when the `defaults` `$options`
4747
array key is missing.
48+
- [#43](https://github.com/zendframework/zend-mvc/pull/43) updates the
49+
`AbstractRestfulController` to ensure it can accept textual (e.g., XML, YAML)
50+
data.
4851

4952
## 2.7.1 - 2016-03-02
5053

src/Controller/AbstractRestfulController.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ protected function getIdentifier($routeMatch, $request)
560560
*
561561
* If the content-type indicates a JSON payload, the payload is immediately
562562
* decoded and the data returned. Otherwise, the data is passed to
563-
* parse_str(). If that function returns a single-member array with a key
564-
* of "0", the method assumes that we have non-urlencoded content and
563+
* parse_str(). If that function returns a single-member array with a empty
564+
* value, the method assumes that we have non-urlencoded content and
565565
* returns the raw content; otherwise, the array created is returned.
566566
*
567567
* @param mixed $request
@@ -578,10 +578,9 @@ protected function processBodyContent($request)
578578

579579
parse_str($content, $parsedParams);
580580

581-
// If parse_str fails to decode, or we have a single element with key
582-
// 0, return the raw content.
583-
if (!is_array($parsedParams)
584-
|| (1 == count($parsedParams) && isset($parsedParams[0]))
581+
// If parse_str fails to decode, or we have a single element with empty value
582+
if (!is_array($parsedParams) || empty($parsedParams)
583+
|| (1 == count($parsedParams) && '' === reset($parsedParams))
585584
) {
586585
return $content;
587586
}

test/Controller/RestfulControllerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Zend\Http\Response;
1919
use Zend\Mvc\MvcEvent;
2020
use Zend\Mvc\Router\RouteMatch;
21+
use ZendTest\Mvc\Controller\TestAsset\RestfulContentTypeTestController;
2122

2223
class RestfulControllerTest extends TestCase
2324
{
@@ -101,6 +102,22 @@ public function testDispatchInvokesCreateMethodWhenNoActionPresentAndPostInvoked
101102
$this->assertEquals('create', $this->routeMatch->getParam('action'));
102103
}
103104

105+
public function testCanReceiveStringAsRequestContent()
106+
{
107+
$string = "any content";
108+
$this->request->setMethod('PUT');
109+
$this->request->setContent($string);
110+
$this->routeMatch->setParam('id', $id = 1);
111+
112+
$controller = new RestfulContentTypeTestController();
113+
$controller->setEvent($this->event);
114+
$result = $controller->dispatch($this->request, $this->response);
115+
116+
$this->assertEquals($id, $result['id']);
117+
$this->assertEquals($string, $result['data']);
118+
$this->assertEquals('update', $this->routeMatch->getParam('action'));
119+
}
120+
104121
public function testDispatchInvokesUpdateMethodWhenNoActionPresentAndPutInvokedWithIdentifier()
105122
{
106123
$entity = ['name' => __FUNCTION__];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Mvc\Controller\TestAsset;
11+
12+
use Zend\Mvc\Controller\AbstractRestfulController;
13+
14+
class RestfulContentTypeTestController extends AbstractRestfulController
15+
{
16+
/**
17+
* Update an existing resource
18+
*
19+
* @param mixed $id
20+
* @param mixed $data
21+
* @return array
22+
*/
23+
public function update($id, $data)
24+
{
25+
return [
26+
'id' => $id,
27+
'data' => $data,
28+
];
29+
}
30+
}

0 commit comments

Comments
 (0)