Skip to content

Instantly share code, notes, and snippets.

@schmengler
Created June 20, 2015 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmengler/a6b073a57804480a02f2 to your computer and use it in GitHub Desktop.
Save schmengler/a6b073a57804480a02f2 to your computer and use it in GitHub Desktop.
Example: Use the Builder pattern to build immutable objects (usage see below in example.php)
use Example\Http\Request;
use Phly\Http\Stream;
use Phly\Http\Uri;
$requestBuilder = Request::builder(new Uri('http://example.com/'), new Stream(fopen('php://memory')));
$requestBuilder->setMethod('POST')->setTarget('/ping')
$requestBuilder->getHeaders()->add([
'Accept' => 'text/html',
'User-Agent' => 'Example-Robot'
]);
// parameters of $requestBuilder may be changed until request is built ...
$request = new Request($requestBuilder);
// $request is now an immutable Request instance that can be passed to a middleware
<?php
namespace Example\Http;
use Psr\Http\Message\UriInterface;
use Psr\Http\Message\StreamInterface;
use Symfony\Component\HttpFoundation\HeaderBag;
/**
* Immutable request object
*/
final class Request
{
/**
* @var StreamInterface
*/
private $body;
/**
* @var UriInterface
*/
private $uri;
/**
* @var string[]
*/
private $headers;
/**
* @var string
*/
private $method;
/**
* @var string
*/
private $target;
public static function builder(UriInterface $uri, StreamInterface $body)
{
return new RequestBuilder($uri, $body);
}
public function __construct(RequestBuilder $builder)
{
$this->body = $builder->getBody();
$this->uri = $builder->getUri();
$this->headers = $builder->getHeaders()->all();
$this->method = $builder->getMethod();
$this->target = $builder->getTarget();
}
/**
* @return StreamInterface
*/
public function getBody()
{
return $this->body;
}
/**
* @return UriInterface
*/
public function getUri()
{
return $this->uri;
}
/**
* @return \string[]
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* @return string
*/
public function getTarget()
{
return $this->target;
}
}
class RequestBuilder
{
/**
* @var StreamInterface
*/
private $body;
/**
* @var UriInterface
*/
private $uri;
/**
* @var HeaderBag
*/
private $headers;
/**
* @var string
*/
private $method;
/**
* @var string
*/
private $target;
public function __construct(UriInterface $uri, StreamInterface $body)
{
// required values:
$this->uri = $uri;
$this->body = $body;
// default values:
$this->headers= new HeaderBag();
$this->method = 'GET';
$this->target = '/';
}
/**
* @return StreamInterface
*/
public function getBody()
{
return $this->body;
}
/**
* @param StreamInterface $body
*/
public function setBody($body)
{
$this->body = $body;
}
/**
* @return UriInterface
*/
public function getUri()
{
return $this->uri;
}
/**
* @param UriInterface $uri
*/
public function setUri($uri)
{
$this->uri = $uri;
}
/**
* @return string[]
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @return string
*/
public function getMethod()
{
return $this->method;
}
/**
* @param string $method
*/
public function setMethod($method)
{
$this->method = $method;
}
/**
* @return string
*/
public function getTarget()
{
return $this->target;
}
/**
* @param string $target
*/
public function setTarget($target)
{
$this->target = $target;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment