5
\$\begingroup\$

I would like to get feedback on this, which are just simple Response/Request classes.

I tried to keep it simple, so I made two interfaces and two classes:

  • RequestInterface.php
  • Request.php
  • ResponseInterface.php
  • Response.php

The use Foo\Storage\ArrayHandler as access; is just a class to access and set arrays, so I wouldn't have to use functions like array_key_exists all over the place to check for keys, set, and unset arrays.

RequestInterface.php

namespace Foo\Http;
interface RequestInterface
{
    public function get($key, $default = null);
    public function post($key, $default = null);
    public function server($key, $default = null);
    public function getMethod();
}

ResponseInterface.php

namespace Foo\Http; 
interface ResponseInterface
{
    public function setStatusCode($statusCode);
    public function addHeader($name, $value);
    public function setContentType($contentType);
    public function redirect($url);
}

Request.php

namespace Foo\Http;
use Foo\Storage\ArrayHandler as access; 
class Request implements RequestInterface
{

    protected $get; 
    protected $post;
    protected $files;
    protected $server;
    protected $cookies;


  public function __construct(
         access $get,
         access $post,
         access $files,
         access $server,
         access $cookies
    ) {
        $this->get = $get;
        $this->post = $post;
        $this->files = $files;
        $this->server = $server;
        $this->cookies = $cookies;
    }


    public function get($key, $defaultValue = null){
      return $this->get->get($key, $defaultValue);
    }

    public function post($key, $defaultValue = null){
        return $this->post->get($key, $defaultValue);
    }

    public function server($key, $defaultValue = null){
        return $this->server->get($key, $defaultValue); 
    }

    public function getMethod(){
        return $this->server->get('REQUEST_METHOD');
    }
}

Response.php

namespace Foo\Http; 

class Response implements ResponseInterface
{

    private $statusCode = 'HTTP/1.1 200 OK';
    private $headers = [];

    public function addHeader($name, $value){
        $this->headers[$name][] = $value;
    }

    public function setHeader($name, $value){
        $this->headers[$name] = [
            (string) $value,
        ];
    }

    public function redirect($url){
        $this->setHeader('Location', $url);
    }

}
\$\endgroup\$
2
  • \$\begingroup\$ There's not much to say, these files don't 'do' much. At this point the purpose of these files elude me. As far as OOP goes it seems alright. But, I think there's a bug in Response.php, which you call Request.php; Each header can have multiple values? \$\endgroup\$ Commented Dec 13, 2014 at 10:09
  • 1
    \$\begingroup\$ There is now a PSR recommended standard for the RequestInterface and ResponseInterface: composer package and original PSR \$\endgroup\$
    – rich remer
    Commented Sep 5, 2015 at 6:16

1 Answer 1

3
\$\begingroup\$

You should take a look at Symfony's HttpFoundation component.

They've written various Request/Response objects similar to what you're attempting to do and they are extremely well done. Either I'd use them directly(save your own time). Or if this is an exploratory exercise then take a look at their code base to see how you can improve on your own solution.

It's not as sexy as writing your own objects, but it'll save you a lot of time and effort in the long run.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.