zero-to-prod/url 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zero-to-prod/url

最新稳定版本:v82.1.0

Composer 安装命令:

composer require zero-to-prod/url

包简介

A DataModel for a Url

README 文档

README

Repo GitHub Actions Workflow Status GitHub Actions Workflow Status Packagist Downloads Packagist Version GitHub repo size License Hits-of-Code

Contents

Introduction

Parse and access url components with a class.

Requirements

  • PHP 7.1 or higher.

Installation

Install Zerotoprod\Url via Composer:

composer require zero-to-prod/url

This will add the package to your project’s dependencies and create an autoloader entry for it.

Documentation Publishing

You can publish this README to your local documentation directory.

This can be useful for providing documentation for AI agents.

This can be done using the included script:

# Publish to default location (./docs/zero-to-prod/url)
vendor/bin/zero-to-prod-url

# Publish to custom directory
vendor/bin/zero-to-prod-url /path/to/your/docs

Automatic Documentation Publishing

You can automatically publish documentation by adding the following to your composer.json:

{
  "scripts": {
    "post-install-cmd": [
      "zero-to-prod-url"
    ],
    "post-update-cmd": [
      "zero-to-prod-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://']);

Contributing

Contributions, issues, and feature requests are welcome! Feel free to check the issues page if you want to contribute.

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature-branch).
  3. Commit changes (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature-branch).
  5. Create a new Pull Request.

统计信息

  • 总下载量: 1.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-26