1 | <?php |
||
16 | abstract class CacheControl |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $directives = []; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private static $commonMethods = [ |
||
27 | 'max-age' => 'withMaxAge', |
||
28 | 'no-cache' => 'withNoCache', |
||
29 | 'no-store' => 'withNoStore', |
||
30 | 'no-transform' => 'withNoTransform', |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var array Maps the directive names to the methods |
||
35 | */ |
||
36 | protected static $directiveMethods = []; |
||
37 | |||
38 | /** |
||
39 | * Create a new Cache-Control object from a header string. |
||
40 | * |
||
41 | * @param string $string |
||
42 | * @return static |
||
43 | */ |
||
44 | 6 | protected static function createFromString($string) |
|
74 | |||
75 | /** |
||
76 | * Set how many seconds to cache. |
||
77 | * |
||
78 | * @param int $seconds |
||
79 | * @return static |
||
80 | */ |
||
81 | 1 | public function withMaxAge($seconds) |
|
85 | |||
86 | /** |
||
87 | * @return int|null |
||
88 | */ |
||
89 | 1 | public function getMaxAge() |
|
93 | |||
94 | /** |
||
95 | * Set whether a representation should be cached. |
||
96 | * |
||
97 | * @param bool $flag |
||
98 | * @return static |
||
99 | */ |
||
100 | 1 | public function withNoCache($flag = true) |
|
104 | |||
105 | /** |
||
106 | * @return bool |
||
107 | */ |
||
108 | 1 | public function hasNoCache() |
|
112 | |||
113 | /** |
||
114 | * Set whether a representation should be stored. |
||
115 | * |
||
116 | * @param bool $flag |
||
117 | * @return static |
||
118 | */ |
||
119 | 1 | public function withNoStore($flag = true) |
|
123 | |||
124 | /** |
||
125 | * @return bool |
||
126 | */ |
||
127 | 1 | public function hasNoStore() |
|
131 | |||
132 | /** |
||
133 | * Set whether the payload can be transformed. |
||
134 | * |
||
135 | * @param bool $flag |
||
136 | * @return static |
||
137 | */ |
||
138 | 1 | public function withNoTransform($flag = true) |
|
142 | |||
143 | /** |
||
144 | * @return bool |
||
145 | */ |
||
146 | 1 | public function hasNoTransform() |
|
150 | |||
151 | /** |
||
152 | * Add a custom extension directive to the Cache-Control. |
||
153 | * |
||
154 | * @param string $name Name of the directive |
||
155 | * @param string $value Value of the directive as a string |
||
156 | * @return static |
||
157 | * @throws InvalidArgumentException If the name or the value of the directive is not a string |
||
158 | */ |
||
159 | 2 | public function withExtension($name, $value) |
|
167 | |||
168 | /** |
||
169 | * @param string $name |
||
170 | * @return string|null |
||
171 | */ |
||
172 | 1 | public function getExtension($name) |
|
176 | |||
177 | /** |
||
178 | * @return string Header string, which can added to the response. |
||
179 | */ |
||
180 | 5 | public function __toString() |
|
198 | |||
199 | /** |
||
200 | * Set a directive with the provided name and value. |
||
201 | * |
||
202 | * @param string $name Name of the directive |
||
203 | * @param string|int|bool|null $value Value of the directive |
||
204 | * @return static |
||
205 | */ |
||
206 | 6 | protected function withDirective($name, $value) |
|
225 | |||
226 | /** |
||
227 | * Returns true if the Cache-Control has a directive and false otherwise. |
||
228 | * |
||
229 | * @param string $name Name of the directive |
||
230 | * @return bool |
||
231 | */ |
||
232 | 2 | protected function hasDirective($name) |
|
236 | |||
237 | /** |
||
238 | * Returns the directive value if available, or `null` if not available. |
||
239 | * |
||
240 | * @param string $name Name of the directive |
||
241 | * @return string|int|null |
||
242 | */ |
||
243 | 2 | protected function getDirective($name) |
|
251 | |||
252 | /** |
||
253 | * Returns the method name to set the provided directive. If the directive cannot be set, the |
||
254 | * method returns `null`. |
||
255 | * |
||
256 | * @param string $directive Name of the directive |
||
257 | * @return string|null |
||
258 | */ |
||
259 | 6 | private static function getMethod($directive) |
|
271 | } |
||
272 |