ergebnis / front-matter
提供前端解析器。
Requires
- php: ~8.1.0 || ~8.2.0 || ~8.3.0
- symfony/yaml: ^5.0.0 || ^6.0.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.39.0
- ergebnis/data-provider: ^3.0.0
- ergebnis/license: ^2.2.0
- ergebnis/php-cs-fixer-config: ~6.10.0
- ergebnis/phpunit-slow-test-detector: ^2.3.2
- fakerphp/faker: ^1.23.0
- infection/infection: ~0.27.6
- phpunit/phpunit: ^10.4.2
- psalm/plugin-phpunit: ~0.18.4
- rector/rector: ~0.18.6
- vimeo/psalm: ^5.15.0
This package is auto-updated.
Last update: 2024-09-10 06:29:28 UTC
README
此项目提供了一个具有前端解析器的 composer
包。
安装
运行
composer require ergebnis/front-matter
用法
此包包含一个 Ergebnis\FrontMatter\Parser
接口,并提供以下解析器
YamlParser
使用 YamlParser
,您可以测试一个 string
是否具有 YAML 前端内容
<?php declare(strict_types=1); use Ergebnis\FrontMatter; $parser = new FrontMatter\YamlParser(); $unparsedContentWithoutFrontMatter = FrontMatter\Content::fromString('Hello, how are you today?'); $parser->hasFrontMatter($unparsedContentWithoutFrontMatter); // false $unparsedContentWithFrontMatter = FrontMatter\Content::fromString(<<<TXT --- page: title: "Hello" description: "Good to see you, how can I help you?" --- TXT); $parser->hasFrontMatter($unparsedContentWithFrontMatter); // true
使用 YamlParser
,您可以将一个 string
进行解析,无论其是否具有 YAML 前端内容
<?php declare(strict_types=1); use Ergebnis\FrontMatter; $parser = new FrontMatter\YamlParser(); $unparsedContentWithoutFrontMatter = FrontMatter\Content::fromString('Hello, how are you today?'); /** @var FrontMatter\Parsed $parsedWithoutFrontMatter */ $parsedWithoutFrontMatter = $parser->parse($unparsedContentWithoutFrontMatter); $unparsedContentWithFrontMatter = FrontMatter\Content::fromString(<<<TXT --- page: title: "Hello" description: "Good to see you, how can I help you?" --- TXT); /** @var FrontMatter\Parsed $parsedWithFrontMatter */ $parsedWithFrontMatter = $parser->parse($unparsedContentWithoutFrontMatter); var_dump($parsedWithFrontMatter->frontMatter()->data()->has('page.title')); // true var_dump($parsedWithFrontMatter->frontMatter()->data()->get('page.title')); // "Hello"
❗ 当前端内容是无效的 YAML 时,YamlParser
将抛出 Ergebnis\FrontMatter\Exception\FrontMatterCanNotBeParsed
异常;当前端内容不描述对象时,将抛出 Ergebnis\FrontMatter\Exception\FrontMatterIsNotAnObject
异常。
💡 无论值是否具有前端内容,YamlParser
成功时都会返回一个 Ergebnis\FrontMatter\Parsed
值对象。
变更日志
此项目的维护者在一个 变更日志 中记录了此项目的显著变更。
贡献
此项目的维护者建议遵循 贡献指南。
行为准则
此项目的维护者要求贡献者遵循 行为准则。
一般支持策略
此项目的维护者提供有限的支持。
您可以通过 赞助 @localheinz 或 为此项目相关的服务请求发票 来支持此项目的维护。
PHP 版本支持策略
此项目支持具有 活跃和安全支持 的 PHP 版本。
此项目的维护者在 PHP 版本首次发布后添加对其的支持,并在其达到安全支持结束时取消对其的支持。
安全策略
此项目有一个 安全策略。
许可协议
此项目使用 MIT 许可协议。
鸣谢
此项目受 webuni/front-matter
启发,最初由 Martin Hasoň 以 MIT 许可协议发布。
社交
关注 Twitter 上的 @localheinz 和 @ergebnis。