1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\HTTP\Headers;
use ICanBoogie\Accessor\AccessorTrait;
/**
* Representation of the `Cache-Control` header field.
*
* <pre>
* <?php
*
* use ICanBoogie\HTTP\Headers\CacheControl;
*
* $cc = CacheControl::from('public, max-age=3600');
* echo $cc->cacheable; // true
* echo $cc->max_age; // 3600
*
* $cc->cacheable = 'no-cache';
* $cc->max_age = null;
* $cc->no_store = true;
* $cc->must_revalidate = true;
* echo $cc; // no-cache, no-store, must-revalidate
* </pre>
*
* @property bool $cacheable
*
* @see http://tools.ietf.org/html/rfc2616#section-14.9
*/
class CacheControl
{
use AccessorTrait;
static protected $cacheable_values = [
'private',
'public',
'no-cache'
];
static protected $booleans = [
'no-store',
'no-transform',
'only-if-cached',
'must-revalidate',
'proxy-revalidate'
];
static protected $placeholder = [
'cacheable'
];
/**
* Returns the default values of the instance.
*
* @return array
*/
static protected function get_default_values()
{
return [
'no_store' => false,
'max_age' => null,
's_maxage' => null,
'max_stale' => null,
'min_fresh' => null,
'no_transform' => false,
'only_if_cached' => false,
'must_revalidate' => false,
'proxy_revalidate' => false,
'extensions' => []
];
}
/**
* Parses the provided cache directive.
*
* @param string $cache_directive
*
* @return array Returns an array made of the properties and extensions.
*/
static protected function parse($cache_directive)
{
$directives = explode(',', $cache_directive);
$directives = array_map('trim', $directives);
$properties = self::get_default_values();
$extensions = [];
foreach ($directives as $value)
{
if (in_array($value, self::$booleans))
{
$property = strtr($value, '-', '_');
$properties[$property] = true;
}
if (in_array($value, self::$cacheable_values))
{
$properties['cacheable'] = $value;
}
else if (preg_match('#^([^=]+)=(.+)$#', $value, $matches))
{
list(, $directive, $value) = $matches;
$property = strtr($directive, '-', '_');
if (is_numeric($value))
{
$value = 0 + $value;
}
if (!array_key_exists($property, $properties))
{
$extensions[$property] = $value;
continue;
}
$properties[$property] = $value;
}
}
return [ $properties, $extensions ];
}
/**
* Create an instance from the provided source.
*
* @param string $source
*
* @return CacheControl
*/
static public function from($source)
{
if ($source instanceof self)
{
return $source;
}
return new static($source);
}
/**
* Whether the request/response is cacheable. The following properties are supported: `public`,
* `private` and `no-cache`. The variable may be empty in which case the cacheability of the
* request/response is unspecified.
*
* Scope: request, response.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1
*
* @var string
*/
private $cacheable;
/**
* @return string
*/
protected function get_cacheable()
{
return $this->cacheable;
}
/**
* @param $value
*/
protected function set_cacheable($value)
{
if ($value === false)
{
$value = 'no-cache';
}
if ($value !== null && !in_array($value, self::$cacheable_values))
{
throw new \InvalidArgumentException(\ICanBoogie\format
(
"%var must be one of: public, private, no-cache. Give: %value", [
'var' => 'cacheable',
'value' => $value
]
));
}
$this->cacheable = $value;
}
/**
* Whether the request/response is can be stored.
*
* Scope: request, response.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.2
*
* @var bool
*/
public $no_store = false;
/**
* Indicates that the client is willing to accept a response whose age is no greater than the
* specified time in seconds. Unless `max-stale` directive is also included, the client is not
* willing to accept a stale response.
*
* Scope: request.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
*
* @var int
*/
public $max_age;
/**
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3
*
* @var int
*/
public $s_maxage;
/**
* Indicates that the client is willing to accept a response that has exceeded its expiration
* time. If max-stale is assigned a value, then the client is willing to accept a response
* that has exceeded its expiration time by no more than the specified number of seconds. If
* no value is assigned to max-stale, then the client is willing to accept a stale response
* of any age.
*
* Scope: request.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3
*
* @var string
*/
public $max_stale;
/**
* Indicates that the client is willing to accept a response whose freshness lifetime is no
* less than its current age plus the specified time in seconds. That is, the client wants a
* response that will still be fresh for at least the specified number of seconds.
*
* Scope: request.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3
*
* @var int
*/
public $min_fresh;
/**
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.5
*
* Scope: request, response.
*
* @var bool
*/
public $no_transform = false;
/**
* Scope: request.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
*
* @var bool
*/
public $only_if_cached = false;
/**
* Scope: response.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
*
* @var bool
*/
public $must_revalidate = false;
/**
* Scope: response.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.4
*
* @var bool
*/
public $proxy_revalidate = false;
/**
* Scope: request, response.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.6
*
* @var array
*/
public $extensions = [];
/**
* If they are defined, the object is initialized with the cache directives.
*
* @param string $cache_directives Cache directives.
*/
public function __construct($cache_directives=null)
{
if ($cache_directives)
{
$this->modify($cache_directives);
}
}
/**
* Returns cache directives.
*
* @return string
*/
public function __toString()
{
$cache_directive = '';
foreach (get_object_vars($this) as $directive => $value)
{
$directive = strtr($directive, '_', '-');
if (in_array($directive, self::$booleans))
{
if (!$value)
{
continue;
}
$cache_directive .= ', ' . $directive;
}
else if (in_array($directive, self::$placeholder))
{
if (!$value)
{
continue;
}
$cache_directive .= ', ' . $value;
}
else if (is_array($value))
{
// TODO: 20120831: extentions
continue;
}
else if ($value !== null && $value !== false)
{
$cache_directive .= ", $directive=$value";
}
}
return $cache_directive ? substr($cache_directive, 2) : '';
}
/**
* Sets the cache directives, updating the properties of the object.
*
* Unknown directives are stashed in the {@link $extensions} property.
*
* @param string $cache_directive
*/
public function modify($cache_directive)
{
list($properties, $extensions) = static::parse($cache_directive);
foreach ($properties as $property => $value)
{
$this->$property = $value;
}
$this->extensions = $extensions;
}
}