# ContentRange

> Represents and parses HTTP Content-Range header values.

[API reference](/docs/api/index.md) › [PSR bridge](/docs/extensions/psr-bridge/reference/index.md) › [emitter](/docs/extensions/psr-bridge/reference/emitter/index.md) › ContentRange

| final class | `yii2\extensions\psrbridge\emitter\ContentRange` |
| --- | --- |
| Implements | [`Stringable`](https://www.php.net/manual/en/class.stringable.php) |
| Source | [`src/emitter/ContentRange.php`](https://github.com/yii2-extensions/psr-bridge/blob/main/src/emitter/ContentRange.php#L18) |

Represents and parses HTTP Content-Range header values.

[`ContentRangeUnit`](/docs/extensions/psr-bridge/reference/emitter/contentrangeunit/index.md) Supported range units.

See also: <https://datatracker.ietf.org/doc/html/rfc7233#section-4.2> (RFC 7233 section 4.2.).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-first"></span>`$first` readonly constructor | `int` | First byte position in the range. | `ContentRange` |
| <span id="property-last"></span>`$last` readonly constructor | `int` | Last byte position in the range. | `ContentRange` |
| <span id="property-length"></span>`$length` readonly constructor | `int\|string` | Total length of the resource ('*' for unknown length). | `ContentRange` |
| <span id="property-unit"></span>`$unit` readonly constructor | [`ContentRangeUnit`](/docs/extensions/psr-bridge/reference/emitter/contentrangeunit/index.md) | Unit of measurement (for example, [`ContentRangeUnit::BYTES`](/docs/extensions/psr-bridge/reference/emitter/contentrangeunit/index.md#case-BYTES)). | `ContentRange` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a new instance of the [`ContentRange`](/docs/extensions/psr-bridge/reference/emitter/contentrange/index.md) class. | `ContentRange` |
| [`__toString()`](#method-__toString) | Converts the Content-Range to its string representation. | `ContentRange` |
| [`fromHeader()`](#method-fromHeader) static | Creates a new [`ContentRange`](/docs/extensions/psr-bridge/reference/emitter/contentrange/index.md) instance from a Content-Range header string. | `ContentRange` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(yii2\extensions\psrbridge\emitter\ContentRangeUnit $unit, int $first, int $last, int|string $length)
```

Creates a new instance of the [`ContentRange`](/docs/extensions/psr-bridge/reference/emitter/contentrange/index.md) class.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$unit` | [`ContentRangeUnit`](/docs/extensions/psr-bridge/reference/emitter/contentrangeunit/index.md) | Unit of measurement (for example, [`ContentRangeUnit::BYTES`](/docs/extensions/psr-bridge/reference/emitter/contentrangeunit/index.md#case-BYTES)). |
| `$first` | `int` | First byte position in the range. |
| `$last` | `int` | Last byte position in the range. |
| `$length` | `int\|string` | Total length of the resource ('*' for unknown length). |

### __toString() <span id="method-__toString"></span>

```php
public function __toString(): string
```

Converts the Content-Range to its string representation.

**Returns**

`string`: String representation in format '&lt;unit> &lt;first>-&lt;last>/&lt;length>'.

### fromHeader() <span id="method-fromHeader"></span>

```php
public static function fromHeader(string $header): self|null
```

Creates a new [`ContentRange`](/docs/extensions/psr-bridge/reference/emitter/contentrange/index.md) instance from a Content-Range header string.

Parses a Content-Range header value in the format '&lt;unit> &lt;first>-&lt;last>/&lt;length>' (for example,
"bytes 0-1233/1234" or "bytes 42-1233/*").

Accepts a matching byte-range expression and checks that the first byte does not exceed the last.
It does not verify that the total length exceeds the last byte or reject surrounding header text.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$header` | `string` | Content-Range header value to parse. |

**Returns**

`self|null`: ContentRange instance if parsing succeeds, `null` if the header is invalid or unsupported or the range is inconsistent.