Parse a url into a class.

v71.2.4 2024-09-08 16:50 UTC

This package is auto-updated.

Last update: 2024-09-08 16:50:56 UTC


README

Repo Latest Version on Packagist test Downloads

Parse and access url components with a class.

Installation

Install the package via Composer:

composer require zerotoprod/url

Usage

Use the from() static method to map array keys to class properties.

It is recommended to extend your own Url class with the \Zerotoprod\Url\Url class.

class Url extends \Zerotoprod\Url\Url
{
}

$url = Url::from(
    parse_url('example.com')
); 

$url->host // 'example.com'

// Passing an array 
$url = Url::new()
        ->set_host('example.com')
        ->set_scheme('https')
        ->set_path('/search')
        ->set_query('q=openai');

$url->host; // 'example.com'

Helper Methods

The Url class includes several helper methods for constructing URLs with specific schemes and ports.

<?php

use Zerotoprod\Url\Url;

// Create a new URL instance and set the host
$url = Url::new()->set_host('example.com');

// Using the toProtocol method with custom schemes and ports
echo $url->toProtocol('http');  // Outputs: "http://example.com:8080"
echo $url->toProtocol('https', 8081);  // Outputs: "https://example.com:8081"


// Using different helper methods to generate URLs for various schemes
echo $url->toSsl();      // Outputs: "ssl://example.com:443"
echo $url->toFtp();      // Outputs: "ftp://example.com:21"
echo $url->toFtps();     // Outputs: "ftps://example.com:990"
echo $url->toSftp();     // Outputs: "sftp://example.com:22"
echo $url->toTcp();      // Outputs: "tcp://example.com:80"
echo $url->toUdp();      // Outputs: "udp://example.com:53"
echo $url->toTls();      // Outputs: "tls://example.com:443"
echo $url->toWs();       // Outputs: "ws://example.com:80"
echo $url->toWss();      // Outputs: "wss://example.com:443"
echo $url->toPop3();     // Outputs: "pop3://example.com:110"
echo $url->toImap();     // Outputs: "imap://example.com:143"
echo $url->toSmtp();     // Outputs: "smtp://example.com:25"

// Optionally, you can pass a custom port to any of these methods
echo $url->toSsl(8443);  // Outputs: "ssl://example.com:8443"
echo $url->toFtp(2121);  // Outputs: "ftp://example.com:2121"

Suggested Traits

Parsable

The Parsable trait provides a method for parsing a URL string and ensuring that it starts with a supported protocol. This is useful when you want to handle various types of URLs and ensure they conform to a specific format before processing.

Installation

The Parsable trait is included within this package, so no additional installation is required.

Usage

To use the Zerotoprod\Url\Parsable trait in your class, simply include it:

class Url extends \Zerotoprod\Url\Url
{
    use \Zerotoprod\Url\Parsable;
}

Url::parse('example.com'); // Defaults to 'https://example.com'
Url::parse('example.com', 'custom://', ['http://', 'custom://']);

Transformable

The Transformable trait provides methods to convert an object’s properties into an array or a JSON string. This is particularly useful for serializing your data models.

composer require zerotoprod/transformable

Usage

To use the Zerotoprod\Transformable\Transformable trait in your class, simply include it:

class Url extends \Zerotoprod\Url\Url
{
    use \Zerotoprod\Transformable\Transformable;
}

$Url = Url::from(
    parse_url('example.com')
);

$array = $Url->toArray();
$json = $Url->toJson();