# PackageMetadata

> Represents the abbreviated npm registry metadata of a package: its dist-tags and its installable versions.

[API reference](/docs/api/index.md) › [Foxy](/docs/extensions/foxy/reference/index.md) › [Native](/docs/extensions/foxy/reference/native/index.md) › PackageMetadata

| final readonly class | `Foxy\Native\PackageMetadata` |
| --- | --- |
| Source | [`src/Native/PackageMetadata.php`](https://github.com/php-forge/foxy/blob/main/src/Native/PackageMetadata.php#L21) |

Represents the abbreviated npm registry metadata of a package: its dist-tags and its installable versions.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-distTags"></span>`$distTags` readonly constructor | `array<string, string>` | Dist-tag names mapped to versions. | `PackageMetadata` |
| <span id="property-name"></span>`$name` readonly constructor | `string` | Package name. | `PackageMetadata` |
| <span id="property-versions"></span>`$versions` readonly constructor | `array<string,` [`PackageVersion`](/docs/extensions/foxy/reference/native/packageversion/index.md)`>` | Versions keyed by version string, limited to the versions that Composer's [`VersionParser`](https://github.com/composer/semver/blob/528948210cc46f967a4b98e83b19e76f2b504993/src/VersionParser.php) can normalize. | `PackageMetadata` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Describes the installable versions returned for an npm package. | `PackageMetadata` |
| [`fromDocument()`](#method-fromDocument) static | Creates the metadata from a decoded registry document. | `PackageMetadata` |
| [`getDistTag()`](#method-getDistTag) | Returns the version a dist-tag points to, or `null` when the tag is unknown. | `PackageMetadata` |
| [`getVersion()`](#method-getVersion) | Returns the metadata of a version, or `null` when the version is unknown. | `PackageMetadata` |

## Method details

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

```php
public function __construct(string $name, array<string, string> $distTags, array<string, Foxy\Native\PackageVersion> $versions)
```

Describes the installable versions returned for an npm package.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Package name. |
| `$distTags` | `array<string, string>` | Dist-tag names mapped to versions. |
| `$versions` | `array<string,` [`PackageVersion`](/docs/extensions/foxy/reference/native/packageversion/index.md)`>` | Versions keyed by version string, limited to the versions that Composer's [`VersionParser`](https://github.com/composer/semver/blob/528948210cc46f967a4b98e83b19e76f2b504993/src/VersionParser.php) can normalize. |

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

```php
public static function fromDocument(string $name, mixed $document): self
```

Creates the metadata from a decoded registry document.

Versions whose key cannot be normalized by Composer's [`VersionParser`](https://github.com/composer/semver/blob/528948210cc46f967a4b98e83b19e76f2b504993/src/VersionParser.php) are skipped without validation; the
entry's own `version` field is not consulted. `dist.integrity` is preferred; a 40-character lowercase hex
`dist.shasum` yields a `sha1-` integrity value instead.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Package name used in error messages and copied into every version. |
| `$document` | `mixed` | Registry document decoded as associative arrays. |

**Returns**

`self`

**Throws**

- [`Foxy\Exception\RuntimeException`](/docs/extensions/foxy/reference/exception/runtimeexception/index.md): if the document, its dist-tags, or a normalizable version entry is malformed.

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

```php
public function getDistTag(string $tag): string|null
```

Returns the version a dist-tag points to, or `null` when the tag is unknown.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tag` | `string` |  |

**Returns**

`string|null`

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

```php
public function getVersion(string $version): Foxy\Native\PackageVersion|null
```

Returns the metadata of a version, or `null` when the version is unknown.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$version` | `string` |  |

**Returns**

[`PackageVersion`](/docs/extensions/foxy/reference/native/packageversion/index.md)`|null`