germania-kg/urlprefixer

1.1.0 2023-01-03 14:13 UTC

This package is auto-updated.

Last update: 2024-03-01 00:17:45 UTC


README

Prefix any URL with a base URL, if not absolute or hashtagged “jump link”.

Packagist PHP version Tests

Installation with Composer

$ composer require germania-kg/urlprefixer

Usage

<?php
use Germania\UrlPrefixer\UrlPrefixer;

// Your prefix, normally a 'base URL'
$prefixer = new UrlPrefixer( 'https://myapp.com/project' );

echo $prefixer('/index.html');
// Result: "https://myapp.com/project/index.html"

// If URL is absolute, nothing happens:
echo $prefixer('//dist/styles.css');
// Result: "//dist/styles.css"

// If URL is hashtagged, nothing happens as well:
echo $prefixer('#navigation');
// Result: "#navigation"

Custom URL prefixes

You may pass a second parameter to the Callable to override the default prefix you set with on instantiation:

<?php
// Your prefix, normally a 'base URL'
$prefixer = new UrlPrefixer( 'https://myapp.com/project' );

echo $prefixer('/logo.jpg', 'https://cdn.test.com');
// Result: "https://cdn.test.com/logo.jpg"

// Like above, nothing happens when URL is absolute:
echo $prefixer('//dist/styles.css', 'https://cdn.test.com');
// Result: "//dist/styles.css"

Issues

See issues list.

Development

$ git clone https://github.com/GermaniaKG/UrlPrefixer.git
$ cd UrlPrefixer
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit