babanywalters/mf-cleaner

清理 microformats2 数组结构

v0.2.0 2022-11-15 20:04 UTC

This package is auto-updated.

Last update: 2024-09-15 23:59:25 UTC


README

Latest Stable Version License Total Downloads

处理 canonical microformats2 数组结构的小助手。与 indieweb/php-mf2 对应。

安装

babanywalters/mf-cleaner 目前已在 PHP 7.3、7.4、8.0 和 8.1 上进行了测试,并且与之兼容。

使用 composer 安装 barnabywalters/mf-cleaner

composer.phar require barnabywalters/mf-cleaner
composer.phar install (or composer.phar update)

版本化的发布版本都经过了 GPG 签名,因此您可以验证代码未被篡改。

gpg --recv-keys 1C00430B19C6B426922FE534BEF8CE58118AD524
cd vendor/barnabywalters/mf-cleaner
git tag -v v0.2.0 # Replace with the version you have installed

用法

大多数函数都是自我解释的,并且如果有什么不清楚的地方,都会在 docblocks 中提供摘要。以下示例展示了最常用的用法

<?php

require __DIR__ . '/vendor/autoload.php';

// Alias the namespace to ”Mf2” for convenience
use BarnabyWalters\Mf2;

// Check if an array structure is a microformat

$hCard = [
	'type' => ['h-card'],
	'properties' => [
		'name' => ['Example McExampleface'],
		'photo' => [['value' => 'https://example.org/photo.png', 'alt' => 'a photo of an example']],
		'logo' => ['https://example.org/logo.png']
	]
];

Mf2\isMicroformat($hCard); // true
Mf2\isMicroformat([1, 2, 3, 4, 'key' => 'value']); // false

Mf2\hasProp($hCard, 'name'); // true

Mf2\getPlaintext($hCard, 'name'); // 'Example McExampleface'

Mf2\getPlaintext($hCard, 'photo'); // 'https://example.org/photo.png'
Mf2\getImgAlt($hCard, 'photo'); // ['value' => 'https://example.org/photo.png', 'alt' => 'a photo of an example']

Mf2\getImgAlt($hCard, 'logo'); // ['value' => 'https://example.org/logo.png', 'alt' => '']

$hEntry = [
	'type' => ['h-entry'],
	'properties' => [
		'published' => ['2013-06-12 12:00:00'],
		'author' => [$hCard],
		'summary' => ['A plaintext summary with <>&" HTML special characters :o'],
		'content' => [['value' => 'Hi!', 'html' => '<p><em>Hi!</em></p>']]
	]
];

Mf2\flattenMicroformats($hEntry); // returns array with $hEntry followed by $hCard
Mf2\getAuthor($hEntry); // returns $hCard. Is an incomplete but still useful implementation of https://indieweb.org/authorship-spec which doesn’t follow links.

// Get the published datetime, fall back to updated if that’s present check that
// it can be parsed by \DateTime, return null if it can’t be found or is invalid
Mf2\getPublished($hEntry, true, null); // '2013-06-12 12:00:00'

Mf2\getHtml($hEntry, 'content'); // '<p><em>Hi!</em></p>'
Mf2\getHtml($hEntry, 'summary'); // "A plaintext summary with &lt;&gt;&amp;&quot; HTML special characters :o"

$microformats = [
	'items' => [$hEntry, $hCard]
];

Mf2\isMicroformatCollection($microformats); // true

Mf2\findMicroformatsByType($microformats, 'h-card'); // [$hCard]

Mf2\findMicroformatsByProperty($microformats, 'published'); // [$hEntry]

Mf2\findMicroformatsByCallable($microformats, function ($mf) {
	return Mf2\hasProp($mf, 'published') and Mf2\hasProp($mf, 'author');
}); // [$hEntry]

贡献

如果您对这个库的使用有任何疑问,请加入 indieweb dev 聊天室,并 ping barnaby 或询问那里的其他友好人士。

如果您发现库中的错误或问题,或想提出功能建议,请 创建一个问题

如果讨论导致您想要提交一个 pull request,遵循此流程(尽管不是必需的),将增加其快速被接受的机会

  • 将此仓库 Fork 到您自己的 github 账户,并将其克隆到您的开发计算机上。
  • 运行 ./run_coverage.sh 并确保所有测试都通过——您需要 XDebug 以获取代码覆盖率数据。
  • 如果适用,编写失败的回归测试,例如修复错误时。
  • 进行更改。
  • 运行 ./run_coverage.shopen docs/coverage/index.html。确保您所做的更改被测试所覆盖。mf-cleaner 在其开发初期就达到了近 100% 的测试覆盖率,这个数字永远不会下降!
  • 运行 ./vendor/bin/psalm 并修复它提出的任何警告。
  • 安装并运行 ./phpDocumentor.phar 以重新生成文档(如果适用)。
  • 推送您的更改并提交 PR。

变更日志

v0.2.0

2022-11-15

在八年的沉睡之后,维护者突然活跃起来,发布了长期等待的更新...

重大变更

  • 将最低 PHP 版本提高到 7.3
  • 将主分支从 master 重命名为 main。如果您需要 dev-master,您需要将其重命名为 dev-main

其他更改

  • 添加了对 img-alt 结构的支持。 getPlaintext()toPlaintext() 正确返回 value 值。添加了 isImgAlt()toImgAlt()getImgAlt(),所有这些都与您预期的一样。
  • removeFalsePositiveRootMicroformats() 的初始实现,以将 mf2 数据重新结构化为在已知非-mf2 h-* 类名时可用于的结构
  • 添加了更多测试以改进覆盖率
  • 设置 GH Action CI 以测试 PHP 7.3、7.4、8.0 和 8.1
  • 设置 /docs 以包含生成的文档(感谢 HongPong!)和公共代码覆盖率信息
  • getAuthor 还会查找 h-feed 作者属性(感谢 aaronpk!)
  • 将深层嵌套的 Functions 文件移动到更浅的位置,以方便使用
  • 开始签署发布标签以启用验证
  • 更新了readme使用说明

v0.1.4

2014-10-06

v0.1.3

2014-05-16

  • 修复了导致getAuthor返回非h-card微格式的问题

v0.1.2

v0.1.1

v0.1.0

  • 初始版本