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\Http\Message\Header;
6
 
7
/**
8
 * Provides helpful functionality for Cache-Control headers
9
 */
10
class CacheControl extends Header
11
{
12
    /** @var array */
13
    protected $directives;
14
 
15
    public function add($value)
16
    {
17
        parent::add($value);
18
        $this->directives = null;
19
    }
20
 
21
    public function removeValue($searchValue)
22
    {
23
        parent::removeValue($searchValue);
24
        $this->directives = null;
25
    }
26
 
27
    /**
28
     * Check if a specific cache control directive exists
29
     *
30
     * @param string $param Directive to retrieve
31
     *
32
     * @return bool
33
     */
34
    public function hasDirective($param)
35
    {
36
        $directives = $this->getDirectives();
37
 
38
        return isset($directives[$param]);
39
    }
40
 
41
    /**
42
     * Get a specific cache control directive
43
     *
44
     * @param string $param Directive to retrieve
45
     *
46
     * @return string|bool|null
47
     */
48
    public function getDirective($param)
49
    {
50
        $directives = $this->getDirectives();
51
 
52
        return isset($directives[$param]) ? $directives[$param] : null;
53
    }
54
 
55
    /**
56
     * Add a cache control directive
57
     *
58
     * @param string $param Directive to add
59
     * @param string $value Value to set
60
     *
61
     * @return self
62
     */
63
    public function addDirective($param, $value)
64
    {
65
        $directives = $this->getDirectives();
66
        $directives[$param] = $value;
67
        $this->updateFromDirectives($directives);
68
 
69
        return $this;
70
    }
71
 
72
    /**
73
     * Remove a cache control directive by name
74
     *
75
     * @param string $param Directive to remove
76
     *
77
     * @return self
78
     */
79
    public function removeDirective($param)
80
    {
81
        $directives = $this->getDirectives();
82
        unset($directives[$param]);
83
        $this->updateFromDirectives($directives);
84
 
85
        return $this;
86
    }
87
 
88
    /**
89
     * Get an associative array of cache control directives
90
     *
91
     * @return array
92
     */
93
    public function getDirectives()
94
    {
95
        if ($this->directives === null) {
96
            $this->directives = array();
97
            foreach ($this->parseParams() as $collection) {
98
                foreach ($collection as $key => $value) {
99
                    $this->directives[$key] = $value === '' ? true : $value;
100
                }
101
            }
102
        }
103
 
104
        return $this->directives;
105
    }
106
 
107
    /**
108
     * Updates the header value based on the parsed directives
109
     *
110
     * @param array $directives Array of cache control directives
111
     */
112
    protected function updateFromDirectives(array $directives)
113
    {
114
        $this->directives = $directives;
115
        $this->values = array();
116
 
117
        foreach ($directives as $key => $value) {
118
            $this->values[] = $value === true ? $key : "{$key}={$value}";
119
        }
120
    }
121
}