mini\Util\CacheControlHeader class

Documentation

Immutable Cache-Control header manipulation

Inspired by PSR-7 UriInterface - all with* methods return new instances.

Usage:

$cache = new CacheControlHeader('public, max-age=3600');

// Query directives
$cache->has('public');           // true
$cache->get('max-age');          // '3600'
$cache->get('private');          // null (not set)
$cache->get('no-cache');         // true (set but no value)

// Modify (returns new instance)
$cache = $cache->with('private')->without('public');
$cache = $cache->with('max-age', 60);

// Utility methods
$cache = $cache->withRestrictedVisibility('private');  // public -> private
$cache = $cache->withMaxTtl(60);  // cap max-age at 60

// Render
echo $cache;  // "private, max-age=60"

Inheritance

Implements: Stringable

Constants (1)

Name Value
VISIBILITY_LEVELS array ( 'public' => 1, 'private' => 2, 'no-cache' => 3, 'no-store' => 4, )

Properties (1)

private array $directives

Methods (15)

Create from existing header value

Parse Cache-Control header string into directives

Check if a directive is set

Get a directive's value

Get all directives

Check if header is empty (no directives)

Add or update a directive

Remove a directive

Restrict visibility to at most the given level

Ensure visibility is at least as restrictive as 'private'

Ensure visibility is 'no-store' (most restrictive)

Cap max-age to at most the given value

Cap s-maxage (shared cache TTL) to at most the given value

Set must-revalidate directive

Render to Cache-Control header string

Source

src/Util/CacheControlHeader.php:32-282