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
10namespace Zend\Http;
11
12use Zend\Stdlib\Message;
13
14/**
15 * HTTP standard message (Request/Response)
16 *
17 * @link      http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4
18 */
19abstract class AbstractMessage extends Message
20{
21    /**#@+
22     * @const string Version constant numbers
23     */
24    const VERSION_10 = '1.0';
25    const VERSION_11 = '1.1';
26    /**#@-*/
27
28    /**
29     * @var string
30     */
31    protected $version = self::VERSION_11;
32
33    /**
34     * @var Headers|null
35     */
36    protected $headers = null;
37
38    /**
39     * Set the HTTP version for this object, one of 1.0 or 1.1
40     * (AbstractMessage::VERSION_10, AbstractMessage::VERSION_11)
41     *
42     * @param  string $version (Must be 1.0 or 1.1)
43     * @return AbstractMessage
44     * @throws Exception\InvalidArgumentException
45     */
46    public function setVersion($version)
47    {
48        if ($version != self::VERSION_10 && $version != self::VERSION_11) {
49            throw new Exception\InvalidArgumentException(
50                'Not valid or not supported HTTP version: ' . $version
51            );
52        }
53        $this->version = $version;
54        return $this;
55    }
56
57    /**
58     * Return the HTTP version for this request
59     *
60     * @return string
61     */
62    public function getVersion()
63    {
64        return $this->version;
65    }
66
67    /**
68     * Provide an alternate Parameter Container implementation for headers in this object,
69     * (this is NOT the primary API for value setting, for that see getHeaders())
70     *
71     * @see    getHeaders()
72     * @param  Headers $headers
73     * @return AbstractMessage
74     */
75    public function setHeaders(Headers $headers)
76    {
77        $this->headers = $headers;
78        return $this;
79    }
80
81    /**
82     * Return the header container responsible for headers
83     *
84     * @return Headers
85     */
86    public function getHeaders()
87    {
88        if ($this->headers === null || is_string($this->headers)) {
89            // this is only here for fromString lazy loading
90            $this->headers = (is_string($this->headers)) ? Headers::fromString($this->headers) : new Headers();
91        }
92
93        return $this->headers;
94    }
95
96    /**
97     * Allow PHP casting of this object
98     *
99     * @return string
100     */
101    public function __toString()
102    {
103        return $this->toString();
104    }
105}
106