The ETagHeaderBuilder class is used to build the ETag header.
The ETag header is a string that uniquely identifies a specific version of a resource. It is used to determine if the
resource has changed since the last request.
You can set the ETag value directly.
Generate a strong ETag. The ETag will be a hash of the content and represent the real version of the resource.
$headers = (new ETagHeaderBuilder())
->etag('123456')
->toHeaders();Generate a weak ETag. The ETag will be a hash of the content and not represent the real version of the resource.
$headers = (new ETagHeaderBuilder())
->etag('123456', true)
->toHeaders();
// or using the weekETag method
$headers = (new ETagHeaderBuilder())
->etag('123456')
->weekETag()
->toHeaders();Generate a strong ETag from the content with a specific hash algorithm.
$headers = (new ETagHeaderBuilder())
->computedETag($content, fn($data) => md5($data))
->toHeaders();
// or using a string function name (since 'md5' is callable in PHP)
$headers = (new ETagHeaderBuilder())
->computedETag($content, 'md5')
->toHeaders();The class also provides immutable setters, which return a new instance of the builder.
$builder = new ETagHeaderBuilder();
$newBuilder = $builder->withEtag('123456');$builder = new ETagHeaderBuilder();
$newBuilder = $builder->withComputedETag($content, 'md5');$builder = new ETagHeaderBuilder();
$newBuilder = $builder->withEtag('123456')->withWeekEtag();You can reset the ETag value.
$headers = (new ETagHeaderBuilder())
->etag('123456')
->resetETag()
->toHeaders(); // returns []This method returns a new instance without the ETag.
$builder = (new ETagHeaderBuilder())->etag('123456');
$newBuilder = $builder->withoutETag();You can reset the weak flag to make the ETag strong.
$headers = (new ETagHeaderBuilder())
->etag('123456', true)
->resetWeekETag()
->toHeaders(); // ETag will be strongThis method returns a new instance with a strong ETag.
$builder = (new ETagHeaderBuilder())->etag('123456', true);
$newBuilder = $builder->withoutWeekETag();The toHeaders() method returns an array with the ETag header.
$headers = (new ETagHeaderBuilder())
->etag('123456')
->toHeaders();
// ['etag' => '"123456"']The getETag() method returns the ETag string.
$etag = (new ETagHeaderBuilder())
->etag('123456')
->getETag();
// '"123456"'The class also has a __toString() method, so you can cast the builder to a string.
$etag = (string)(new ETagHeaderBuilder())->etag('123456');
// '"123456"'You can check if the ETag is set using isEmpty() and isNotEmpty().
$builder = new ETagHeaderBuilder();
$builder->isEmpty(); // true
$builder->isNotEmpty(); // false
$builder->etag('123456');
$builder->isEmpty(); // false
$builder->isNotEmpty(); // trueThis documentation was AI-generated.