浏览代码

Implement parsed body methods.

Add more of the PSR7 interface in.
Mark Story 9 年之前
父节点
当前提交
f79582299a
共有 2 个文件被更改,包括 61 次插入0 次删除
  1. 30 0
      src/Network/Request.php
  2. 31 0
      tests/TestCase/Network/RequestTest.php

+ 30 - 0
src/Network/Request.php

@@ -1404,6 +1404,36 @@ class Request implements ArrayAccess
     }
 
     /**
+     * Get the parsed request body data.
+     *
+     * If the request Content-Type is either application/x-www-form-urlencoded
+     * or multipart/form-data, nd the request method is POST, this will be the
+     * post data. For other content types, it may be the deserialized request
+     * body.
+     *
+     * @return null|array|object The deserialized body parameters, if any.
+     *     These will typically be an array or object.
+     */
+    public function getParsedBody()
+    {
+        return $this->data;
+    }
+
+    /**
+     * Update the parsed body and get a new instance.
+     *
+     * @param null|array|object $data The deserialized body data. This will
+     *     typically be in an array or object.
+     * @return static
+     */
+    public function withParsedBody($data)
+    {
+        $new = clone $this;
+        $new->data = $data;
+        return $new;
+    }
+
+    /**
      * Get/Set value from the request's environment data.
      * Fallback to using env() if key not set in $environment property.
      *

+ 31 - 0
tests/TestCase/Network/RequestTest.php

@@ -2705,6 +2705,37 @@ XML;
     }
 
     /**
+     * Test getting the parsed body parameters.
+     *
+     * @return void
+     */
+    public function testGetParsedBody()
+    {
+        $data = ['title' => 'First', 'body' => 'Best Article!'];
+        $request = new Request(['post' => $data]);
+        $this->assertSame($data, $request->getParsedBody());
+
+        $request = new Request();
+        $this->assertSame([], $request->getParsedBody());
+    }
+
+    /**
+     * Test replacing the parsed body parameters.
+     *
+     * @return void
+     */
+    public function testWithParsedBody()
+    {
+        $data = ['title' => 'First', 'body' => 'Best Article!'];
+        $request = new Request([]);
+        $new = $request->withParsedBody($data);
+
+        $this->assertNotSame($request, $new);
+        $this->assertSame([], $request->getParsedBody());
+        $this->assertSame($data, $new->getParsedBody());
+    }
+
+    /**
      * Test updating POST data in a psr7 fashion.
      *
      * @return void