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\Header;
4
 
5
use Guzzle\Common\ToArrayInterface;
6
 
7
/**
8
 * Provides a case-insensitive collection of headers
9
 */
10
class HeaderCollection implements \IteratorAggregate, \Countable, \ArrayAccess, ToArrayInterface
11
{
12
    /** @var array */
13
    protected $headers;
14
 
15
    public function __construct($headers = array())
16
    {
17
        $this->headers = $headers;
18
    }
19
 
20
    public function __clone()
21
    {
22
        foreach ($this->headers as &$header) {
23
            $header = clone $header;
24
        }
25
    }
26
 
27
    /**
28
     * Clears the header collection
29
     */
30
    public function clear()
31
    {
32
        $this->headers = array();
33
    }
34
 
35
    /**
36
     * Set a header on the collection
37
     *
38
     * @param HeaderInterface $header Header to add
39
     *
40
     * @return self
41
     */
42
    public function add(HeaderInterface $header)
43
    {
44
        $this->headers[strtolower($header->getName())] = $header;
45
 
46
        return $this;
47
    }
48
 
49
    /**
50
     * Get an array of header objects
51
     *
52
     * @return array
53
     */
54
    public function getAll()
55
    {
56
        return $this->headers;
57
    }
58
 
59
    /**
60
     * Alias of offsetGet
61
     */
62
    public function get($key)
63
    {
64
        return $this->offsetGet($key);
65
    }
66
 
67
    public function count()
68
    {
69
        return count($this->headers);
70
    }
71
 
72
    public function offsetExists($offset)
73
    {
74
        return isset($this->headers[strtolower($offset)]);
75
    }
76
 
77
    public function offsetGet($offset)
78
    {
79
        $l = strtolower($offset);
80
 
81
        return isset($this->headers[$l]) ? $this->headers[$l] : null;
82
    }
83
 
84
    public function offsetSet($offset, $value)
85
    {
86
        $this->add($value);
87
    }
88
 
89
    public function offsetUnset($offset)
90
    {
91
        unset($this->headers[strtolower($offset)]);
92
    }
93
 
94
    public function getIterator()
95
    {
96
        return new \ArrayIterator($this->headers);
97
    }
98
 
99
    public function toArray()
100
    {
101
        $result = array();
102
        foreach ($this->headers as $header) {
103
            $result[$header->getName()] = $header->toArray();
104
        }
105
 
106
        return $result;
107
    }
108
}