effectra/link

Effectra Link 包。

v1.0.0 2023-06-19 17:11 UTC

This package is auto-updated.

Last update: 2024-09-18 13:04:54 UTC


README

Effectra\Link 是一个 PHP 库,提供了各种 PSR 链接接口的实现。它提供了 Link、EvolvableLink 和 LinkProvider 等类,允许您处理超媒体链接、管理可变链接以及为您的应用程序检索链接。

功能

  • 创建和操作超媒体链接
  • 管理可变链接
  • 通过关系检索链接
  • 生成包含链接的 HTML 字符串

要求

  • PHP 7.4 或更高版本

安装

您可以通过 Composer 安装 Effectra\Link 包。只需运行以下命令

composer require effectra/link

用法

创建链接

use Effectra\Link\Link;

$link = new Link('http://example.com', false, ['rel' => 'self'], ['title' => 'Example Link']);

操作链接

// Add a relation to the link
$link = $link->withRel('next');

// Remove a relation from the link
$link = $link->withoutRel('self');

// Add an attribute to the link
$link = $link->withAttribute('class', 'link-class');

// Remove an attribute from the link
$link = $link->withoutAttribute('title');

检索链接

use Effectra\Link\LinkProvider;

$link1 = new Link('http://example.com', false, ['rel' => 'self']);
$link2 = new Link('http://example.com/posts', false, ['rel' => 'collection']);

$links = [$link1, $link2];

$linkProvider = new LinkProvider($links, []);

$allLinks = $linkProvider->getLinks(); // Returns an array of LinkInterface objects

$collectionLinks = $linkProvider->getLinksByRel('collection'); // Returns an array of EvolvableLinkInterface objects with the 'collection' relation

使用链接生成 HTML

$html = $linkProvider->withHTML($links); // Generates an HTML string with links

有关更多信息和高阶用法,请参阅文档。

贡献

欢迎贡献!如果您有任何错误报告、功能请求或建议,请在该 GitHub 仓库中打开一个问题。

许可证

Effectra\Link 是开源软件,采用 MIT 许可证。有关更多信息,请参阅LICENSE 文件。


Feel free to customize the content to fit your specific needs.