PHP code example of gumslone / laravel-package-url

1. Go to this page and download the library: Download gumslone/laravel-package-url library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gumslone / laravel-package-url example snippets


use Gumslone\PackageUrl\PackageUrl;

$purl = PackageUrl::fromString('pkg:composer/laravel/[email protected]');

$purl->type;       // "composer"
$purl->namespace;  // "laravel"
$purl->name;       // "framework"
$purl->version;    // "11.55.0"
$purl->qualifiers; // []
$purl->subpath;    // null

(string) $purl;    // "pkg:composer/laravel/[email protected]"
$purl->toArray();  // ['type' => 'composer', 'namespace' => 'laravel', ...]

// Build from components (with normalization + validation)
PackageUrl::fromArray([
    'type' => 'PYPI',
    'name' => 'Django_package',
    'version' => '1.11.1',
])->toString(); // "pkg:pypi/[email protected]"  (type + name normalized)

// Immutable withers (each returns a new normalized PackageUrl)
$purl->withVersion('12.0.0');
$purl->withNamespace('symfony')->withName('console');
$purl->withType('composer');
$purl->withSubpath('src/Console');
$purl->withQualifier('repository_url', 'https://example.org'); // '' removes it
$purl->withoutQualifier('repository_url');
$purl->withoutVersion();

// Read
$purl->qualifier('repository_url');  // ?string
$purl->equals('pkg:composer/laravel/[email protected]'); // true

// Type checks
$purl->isType('composer');   // true (case-insensitive)
$purl->isGeneric();          // false

use Purl;

Purl::isValid('pkg:npm/[email protected]');        // true
Purl::parse('pkg:npm/[email protected]');          // PackageUrl
Purl::parseMany([...]);                          // Collection<PackageUrl>, invalid skipped

// Find every purl embedded in a block of text (SBOM, manifest, log, changelog)
Purl::extractAll($text);                         // Collection<PackageUrl>, de-duplicated

// Type
Purl::isType('pkg:cargo/[email protected]', 'cargo'); // true
Purl::isGeneric('pkg:generic/[email protected]');      // true

// Can this purl produce a URL?
Purl::hasRepositoryUrl('pkg:composer/laravel/[email protected]'); // true
Purl::hasDownloadUrl('pkg:npm/[email protected]');                // true
Purl::hasDownloadUrl('pkg:npm/left-pad');                      // false (no version)
Purl::hasRegistryApiUrl('pkg:cargo/[email protected]');               // true
Purl::isConvertibleToUrl('pkg:npm/left-pad');                 // true (any of the above)

// Can this URL be turned into a purl?
Purl::isConvertibleDownloadUrl('https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz'); // true
Purl::isConvertibleUrl('[email protected]:laravel/framework.git'); // true (also accepts repo/SSH URLs)

Purl::repositoryUrl('pkg:composer/laravel/[email protected]');
// https://packagist.org/packages/laravel/framework

Purl::repositoryUrl('pkg:pypi/[email protected]');
// https://pypi.org/project/django/5.0/

Purl::registryApiUrl('pkg:npm/[email protected]');
// https://registry.npmjs.org/left-pad

Purl::downloadUrl('pkg:npm/@babel/[email protected]');
// https://registry.npmjs.org/@babel/core/-/core-7.0.0.tgz

Purl::downloadUrl('pkg:maven/org.apache.commons/[email protected]');
// https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.14.0/commons-lang3-3.14.0.jar

Purl::osvEcosystem('pkg:pypi/[email protected]');       // "PyPI"       (OSV.dev)
Purl::osvEcosystem('pkg:composer/laravel/[email protected]'); // "Packagist"
Purl::githubEcosystem('pkg:npm/[email protected]'); // "NPM"        (GitHub Advisories)

Purl::fromRepositoryUrl('https://github.com/laravel/framework');
// pkg:github/laravel/framework

Purl::fromRepositoryUrl('[email protected]:laravel/framework.git');
// pkg:github/laravel/framework

Purl::fromRepositoryUrl('https://github.com/laravel/framework/tree/v11.0.0');
// pkg:github/laravel/[email protected]

Purl::fromDownloadUrl('https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz');
// pkg:npm/[email protected]

Purl::fromDownloadUrl('https://files.pythonhosted.org/packages/ab/cd/Django-5.0-py3-none-any.whl');
// pkg:pypi/[email protected]

Purl::fromDownloadUrl('https://repo1.maven.org/maven2/org/apache/commons/commons-lang3/3.14.0/commons-lang3-3.14.0.jar');
// pkg:maven/org.apache.commons/[email protected]

Purl::fromDownloadUrl('https://github.com/laravel/framework/archive/refs/tags/v11.0.0.tar.gz');
// pkg:github/laravel/[email protected]

// Try both, whichever matches
Purl::fromUrl($anyPackageOrRepoUrl);