Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace Guzzle\Http\Message;
4
 
5
use Guzzle\Common\Version;
6
use Guzzle\Http\Message\Header\HeaderInterface;
7
 
8
/**
9
 * Represents a header and all of the values stored by that header
10
 */
11
class Header implements HeaderInterface
12
{
13
    protected $values = array();
14
    protected $header;
15
    protected $glue;
16
 
17
    /**
18
     * @param string       $header Name of the header
19
     * @param array|string $values Values of the header as an array or a scalar
20
     * @param string       $glue   Glue used to combine multiple values into a string
21
     */
22
    public function __construct($header, $values = array(), $glue = ',')
23
    {
24
        $this->header = trim($header);
25
        $this->glue = $glue;
26
 
27
        foreach ((array) $values as $value) {
28
            foreach ((array) $value as $v) {
29
                $this->values[] = $v;
30
            }
31
        }
32
    }
33
 
34
    public function __toString()
35
    {
36
        return implode($this->glue . ' ', $this->toArray());
37
    }
38
 
39
    public function add($value)
40
    {
41
        $this->values[] = $value;
42
 
43
        return $this;
44
    }
45
 
46
    public function getName()
47
    {
48
        return $this->header;
49
    }
50
 
51
    public function setName($name)
52
    {
53
        $this->header = $name;
54
 
55
        return $this;
56
    }
57
 
58
    public function setGlue($glue)
59
    {
60
        $this->glue = $glue;
61
 
62
        return $this;
63
    }
64
 
65
    public function getGlue()
66
    {
67
        return $this->glue;
68
    }
69
 
70
    /**
71
     * Normalize the header to be a single header with an array of values.
72
     *
73
     * If any values of the header contains the glue string value (e.g. ","), then the value will be exploded into
74
     * multiple entries in the header.
75
     *
76
     * @return self
77
     */
78
    public function normalize()
79
    {
80
        $values = $this->toArray();
81
 
82
        for ($i = 0, $total = count($values); $i < $total; $i++) {
83
            if (strpos($values[$i], $this->glue) !== false) {
84
                // Explode on glue when the glue is not inside of a comma
85
                foreach (preg_split('/' . preg_quote($this->glue) . '(?=([^"]*"[^"]*")*[^"]*$)/', $values[$i]) as $v) {
86
                    $values[] = trim($v);
87
                }
88
                unset($values[$i]);
89
            }
90
        }
91
 
92
        $this->values = array_values($values);
93
 
94
        return $this;
95
    }
96
 
97
    public function hasValue($searchValue)
98
    {
99
        return in_array($searchValue, $this->toArray());
100
    }
101
 
102
    public function removeValue($searchValue)
103
    {
104
        $this->values = array_values(array_filter($this->values, function ($value) use ($searchValue) {
105
            return $value != $searchValue;
106
        }));
107
 
108
        return $this;
109
    }
110
 
111
    public function toArray()
112
    {
113
        return $this->values;
114
    }
115
 
116
    public function count()
117
    {
118
        return count($this->toArray());
119
    }
120
 
121
    public function getIterator()
122
    {
123
        return new \ArrayIterator($this->toArray());
124
    }
125
 
126
    public function parseParams()
127
    {
128
        $params = $matches = array();
129
        $callback = array($this, 'trimHeader');
130
 
131
        // Normalize the header into a single array and iterate over all values
132
        foreach ($this->normalize()->toArray() as $val) {
133
            $part = array();
134
            foreach (preg_split('/;(?=([^"]*"[^"]*")*[^"]*$)/', $val) as $kvp) {
135
                if (!preg_match_all('/<[^>]+>|[^=]+/', $kvp, $matches)) {
136
                    continue;
137
                }
138
                $pieces = array_map($callback, $matches[0]);
139
                $part[$pieces[0]] = isset($pieces[1]) ? $pieces[1] : '';
140
            }
141
            if ($part) {
142
                $params[] = $part;
143
            }
144
        }
145
 
146
        return $params;
147
    }
148
 
149
    /**
150
     * @deprecated
151
     * @codeCoverageIgnore
152
     */
153
    public function hasExactHeader($header)
154
    {
155
        Version::warn(__METHOD__ . ' is deprecated');
156
        return $this->header == $header;
157
    }
158
 
159
    /**
160
     * @deprecated
161
     * @codeCoverageIgnore
162
     */
163
    public function raw()
164
    {
165
        Version::warn(__METHOD__ . ' is deprecated. Use toArray()');
166
        return $this->toArray();
167
    }
168
 
169
    /**
170
     * Trim a header by removing excess spaces and wrapping quotes
171
     *
172
     * @param $str
173
     *
174
     * @return string
175
     */
176
    protected function trimHeader($str)
177
    {
178
        static $trimmed = "\"'  \n\t";
179
 
180
        return trim($str, $trimmed);
181
    }
182
}