Ergo\Http\ResponseBuilder::build PHP Method

build() public method

public build ( ) : Response
return Response
    public function build()
    {
        if (isset($this->_view)) {
            $this->setBody($this->_view->output());
        }
        if (!$this->_status) {
            $this->setStatusCode(200);
        }
        if (isset($this->_location)) {
            $this->addHeader('Location', $this->_location);
        }
        if (isset($this->_cacheControl)) {
            $this->addHeader('Cache-Control', implode(', ', $this->_cacheControl));
        }
        $this->addHeader('Content-Length', strlen($this->_body));
        return new Response($this->_status, $this->_headerFields, $this->_body);
    }

Usage Example

コード例 #1
0
 public function testBasicUsage()
 {
     $builder = new Http\ResponseBuilder();
     $builder->addHeader('Content-Type', 'text/plain')->setBody('test')->addHeader('X-Blarg', 'meh')->setStatusCode(418);
     $response = $builder->build();
     $this->assertInstanceOf('\\Ergo\\Http\\Response', $response);
     $this->assertEquals($response->getStatus()->getCode(), 418);
     $this->assertTrue($response->hasBody());
     $this->assertEquals($response->getBody(), 'test');
     $headers = $response->getHeaders()->toArray();
     $this->assertEquals(count($headers), 3, 'should be 3 headers: %s');
     $this->assertEquals($headers[0], "Content-Type: text/plain\r\n");
     $this->assertEquals($headers[1], "X-Blarg: meh\r\n");
     $this->assertEquals($headers[2], "Content-Length: 4\r\n");
 }