ismaxim / urling
PHP 的 URL 解析器和构造器
Requires
- php: ^7.4|^8.0
- webmozart/assert: ^1.10.0
Requires (Dev)
- phpstan/phpstan: ^0.12.0
- phpunit/phpunit: ^9.5.0
This package is auto-updated.
Last update: 2024-09-29 05:56:25 UTC
README
Urling
⚙️ 安装
要安装此库 - 在您的终端中运行以下命令
composer require ismaxim/urling
🧙 使用
📖 概念
三大理念
📗 1. 两种处理 URL 的模式:解析模式 & 构造模式。
📘 2. 使用别名访问 URL 的具体部分(见 访问表,别名列 别名)。
📙 3. 基础编辑器用于处理整个 URL 和每个部分(见 基本用法 部分)。
🚀 开始
# Url parser mode use Urling\Urling; $urling = new Urling("https://github.com/ismaxim/urling#start"); $url_part_values = [ "protocol_value" => $urling->url->protocol->get(), "domain_value" => $urling->url->domain->get(), "routes_value" => $urling->url->routes->get(), "anchor_value" => $urling->url->anchor->get(), ]; print_r($url_part_values); /* RESULT: [ "protocol_value" => "https", "domain_value" => "github.com", "routes_value" => "ismaxim/urling", "anchor_value" => "start", ] */
# Url constructor mode use Urling\Urling; $urling = new Urling(); $urling->url->construct([ "protocol" => "https", "domain" => "github.com", "routes" => "ismaxim/urling", "anchor" => "start", ]); // Either you can set the value for each distinct part // in the url by accessing it directly, for example: $urling->url->protocol->add("https"); $urling->url->domain->add("github.com"); $urling->url->routes->add("ismaxim/urling"); $urling->url->anchor->add("start"); print_r($urling->url->get()); /* RESULT: "https://github.com/ismaxim/urling#start" */
🔑 访问
您可以通过使用其基本名称(见 访问表,URL 部分列 URL 部分)或请求其别名(见 访问表,别名列 别名)来访问具体 URL 部分,以解析它
$urling->url->scheme->... | $urling->url->protocol->... (other parts of url in a similar way).
👶 基本用法
基础编辑器 - URL 基础编辑器 和 URL 部分基础编辑器 几乎涵盖所有任务:添加、获取、更新或删除 URL 或其任何部分的值。 基础编辑器 是在原生 PHP 的 parse_url() 函数之上的 "CRUDable" 包装器,并且根据这一点,它们以类似的方式返回和修改值。唯一的显著区别是在解析 URL 或其部分时的调用语法。
// Working with URL $urling->url->add(); $urling->url->get(); $urling->url->update(); $urling->url->delete(); // Working with one of the URL parts $urling->url->scheme->add(); $urling->url->scheme->get(); $urling->url->scheme->update(); $urling->url->scheme->delete(); // For example, let's imagine that the URL is: https://github.com/ismaxim/urling#basic-usage // Then example workflow to parse this URL in part of "scheme" or "protocol" (see ACCESSING TABLE, column "Aliases") will seem to this: $urling->url->scheme->get(); # returns "https" (state of URL: https://github.com/ismaxim/urling#basic-usage) $urling->url->scheme->delete(); # returns null (state of URL: github.com/ismaxim/urling#basic-usage) $urling->url->scheme->add("ftp"); # returns "ftp" (state of URL: ftp://github.com/ismaxim/urling#basic-usage) $urling->url->scheme->update("smtp"); # returns "smtp" (state of URL: smtp://github.com/ismaxim/urling#basic-usage) // Work with other parts of URL can be done in a similar way.
🧔 高级用法
如果您需要执行类似 添加、获取、更新或删除 URL 任何部分的值的操作,但这超出了基础功能的范围,您可以使用基础编辑器的 添加、获取、更新或删除 函数作为前缀,以特定方法名称作为后缀,适合您要执行的任务,如
(添加、获取、更新、删除) + "SomeFunctionName" 用于具体任务。
注意:几乎所有函数将永久使用该代码约定。
示例
- getValueByName();
- getNameValuePairs();
- 等等...
$urling->url->params->getValueByName(); $urling->url->params->getNameValuePairs();
🧪 测试
实际上,所有测试都在 CI 构建中自动通过。
要测试此库 - 在您的终端中运行以下命令。
composer test
🤝 贡献
如果您使用此库无法解决的问题,请写出您的解决方案,并且如果您想帮助其他使用此库的开发者(或者如果您想在发布新版本后保持您的解决方案正常工作,该版本将进入包管理器依赖项) - 创建一个 pull-request。我们将非常高兴将您出色的代码添加到库中!
🐞 在 GitHub issues 上报告您发现的任何错误或问题。
✨ 创建自定义功能
您可以使用自己的代码扩展库的功能,对解析类进行编辑以解决您的问题。存在两种类型的解析类,第一个和主要的是 URL解析器,但也有其他,如 URL部分解析器。对于每个部分都有一个独立的解析器。
使用库或查看文档时,您可能会注意到与这个记录相同或类似的内容
$urling->url->params->get()
这个条目可能这样解释:“嘿,Urling,请对当前URL的‘params’部分进行询问并返回其值(这部分)”。
基本上,在扩展功能时,您几乎每次都会处理URL的一个部分,并处理或获取特定部分的价值。要了解如何访问所需部分的解析器,您可以查看 访问表。您只需要将 URL部分 和 别名 部分与 解析器 部分相匹配,然后转到所需的解析器文件并编写最好的代码!
📎 致谢
📃 许可证
MIT许可证(MIT)。请参阅 许可证文件 以获取更多信息。