Skip to content

Latest commit

 

History

History
432 lines (279 loc) · 7.41 KB

Url.md

File metadata and controls

432 lines (279 loc) · 7.41 KB

URL Documentation

\Greg\Support\Url is working with URLs.

Table of contents:

Constants

const UA = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

Methods:

  • hasSchema - Determine if an URL has schema;
  • noSchema - Get an URL without schema;
  • schema - Set current schema to an URL;
  • shorted - Set short schema to an URL;
  • secured - Set secured schema to an URL;
  • unsecured - Set unsecured schema to an URL;
  • schemly - Add current schema to an URL if was not defined;
  • shortly - Set short schema to an URL if was not defined;
  • securely - Set secured schema to an URL if was not defined;
  • unsecurely - Set unsecured schema to an URL if was not defined;
  • absolute - Transform a relative URL to server absolute URL;
  • relative - Transform an URL to relative URL;
  • serverRelative - Transform a server absolute URL to relative URL;
  • host - Get host from an URL;
  • hostLevel - Get host level from an URL;
  • hostEquals - Determine if two URLs are equal by their host;
  • root - Get root of an URL;
  • path - Remove query string of an URL;
  • base - Get server base URL;
  • addQuery - Add a query to an URL;
  • init - Init an cURL handle for an URL;
  • effective - Get effective URL of an URL;
  • contents - Get contents of an URL.

hasSchema

Determine if an URL has schema.

hasSchema(string $absolute): boolean

$absolute - The URL.

Example:

\Greg\Support\Url::hasSchema('example.com'); // result: false

\Greg\Support\Url::hasSchema('http://example.com'); // result: true

noSchema

Get an URL without schema.

noSchema(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::noSchema('http://example.com'); // result: example.com

schema

Set current schema to an URL.

schema(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::schema('example.com'); // result: http://example.com

shorted

Set short schema to an URL.

shorted(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::shorted('http://example.com/styles.css'); // result: //example.com/styles.css

secured

Set secured schema to an URL.

secured(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::secured('http://example.com'); // result: https://example.com

unsecured

Set unsecured schema to an URL.

unsecured(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::unsecured('https://example.com'); // result: http://example.com

schemly

Add current schema to an URL if was not defined.

schemly(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::schemly('example.com'); // result: http://example.com

\Greg\Support\Url::schemly('https://example.com'); // result: https://example.com

shortly

Set short schema to an URL if was not defined.

shorted(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::shortly('http://example.com/styles.css'); // result: //example.com/styles.css

securely

Set secured schema to an URL if was not defined.

securely(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::securely('http://example.com'); // result: https://example.com

unsecurely

Set unsecured schema to an URL if was not defined.

unsecurely(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::unsecurely('https://example.com'); // result: http://example.com

absolute

Transform a relative URL to server absolute URL.

absolute(string $relative): string

$relative - The URL.

Example:

// Let say server url is localhost

\Greg\Support\Url::absolute('/foo'); // result: http://localhost/foo

relative

Transform an absolute URL to relative URL.

relative(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::relative('http://example.com/foo'); // result: /foo

serverRelative

Transform a server absolute URL to relative URL.

serverRelative(string $absolute): string

$absolute - The URL.

Example:

// Let say server url is localhost

\Greg\Support\Url::serverRelative('http://example.com/foo'); // result: http://example.com/foo

\Greg\Support\Url::serverRelative('http://localhost/foo'); // result: /foo

host

Get host from an URL.

host(string $absolute, boolean $stripWWW = true): string

$absolute - The URL;
$stripWWW - Strip www from URL.

Example:

\Greg\Support\Url::host('http://example.com/foo'); // result: example.com

hostLevel

Get host level from an URL.

hostLevel(string $absolute, int $level = 2, boolean $stripWWW = true): string

$absolute - The URL;
$level - Host level;
$stripWWW - Strip www from URL.

Example:

\Greg\Support\Url::hostLevel('http://mobile.example.com/foo'); // result: example.com

\Greg\Support\Url::hostLevel('http://mobile.example.com/foo', 3); // result: mobile.example.com

hostEquals

Determine if two URLs are equal by their host.

hostEquals(string $absolute1, string $absolute2, int $level = 2): boolean

$absolute1 - The first URL;
$absolute2 - The second URL;
$level - Host level.

Example:

\Greg\Support\Url::hostEquals('http://v1.example.com/foo', 'http://v2.example.com/foo'); // result: true

root

Get root of an URL.

root(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::root('http://example.com/foo'); // result: http://example.com

path

Get without query string.

path(string $url): string

$url - The URL.

Example:

\Greg\Support\Url::path('/foo?bar=BAR'); // result: /foo

base

Get server base URL.

base(string $path = '/', boolean $absolute = false): string

$path - Path;
$absolute - Return absolute URL.

Example:

// Let say your base uri is /module

\Greg\Support\Url::base('/foo'); // result: /module/foo

addQuery

Add a query to an URL.

addQuery(string $url, string|array $query): string

$url - URL;
$query - Query.

Example:

\Greg\Support\Url::addQuery('/?foo=FOO', ['bar' => 'BAR']); // result: /?foo=FOO&bar=BAR

init

Init an cURL handle for an URL.

init(string $absolute, boolean $verbose = false): resource

$absolute - The URL;
$verbose - Verbose.

Example:

$handle = \Greg\Support\Url::init('http://example.com');

$contents = curl_exec($handle);

effective

Get effective URL of an URL.

effective(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::effective('http://some.old.example.com'); // result: http://example.com

contents

Get contents of an URL.

contents(string $absolute): string

$absolute - The URL.

Example:

\Greg\Support\Url::contents('http://example.com'); // result: contents of the url