ergebnis / front-matter
Provides a front matter parser.
Installs: 19 745
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.4 || ^8.0
- symfony/yaml: ^5.4.0 || ^6.0.0
Requires (Dev)
- ergebnis/composer-normalize: ^2.18.0
- ergebnis/license: ^1.1.0
- ergebnis/php-cs-fixer-config: ~3.4.0
- ergebnis/test-util: ^1.5.0
- infection/infection: ~0.25.5
- phpunit/phpunit: ^9.5.10
- psalm/plugin-phpunit: ~0.16.1
- vimeo/psalm: ^4.15.0
README
Provides a front matter parser.
Installation
Run
$ composer require ergebnis/front-matter
Usage
This packages comes with an Ergebnis\FrontMatter\Parser
interface and provides the following parsers:
YamlParser
With the YamlParser
, you can test if a string
has YAML front matter:
<?php use Ergebnis\FrontMatter; $parser = new FrontMatter\YamlParser(); $valueWithoutFrontMatter = 'Hello, how are you today?'; $parser->hasFrontMatter($valueWithoutFrontMatter); // false $valueWithFrontMatter = <<<TXT --- page: title: "Hello" description: "Good to see you, how can I help you?" --- TXT; $parser->hasFrontMatter($valueWithFrontMatter); // true
With the YamlParser
, you can parse a string
, regardless of whether it has YAML front matter or not.
<?php use Ergebnis\FrontMatter; $parser = new FrontMatter\YamlParser(); $valueWithoutFrontMatter = 'Hello, how are you today?'; /** @var FrontMatter\Parsed $parsed */ $parsed = $parser->parse($valueWithoutFrontMatter); $valueWithFrontMatter = <<<TXT --- page: title: "Hello" description: "Good to see you, how can I help you?" --- TXT; /** @var FrontMatter\Parsed $parsed */ $parsed = $parser->parse($valueWithoutFrontMatter);
❗ The YamlParser
will throw an Ergebnis\FrontMatter\Exception\InvalidFrontMatter
exception when
- the front matter is invalid YAML
- the front matter data does not describe an associative array
💡 The YamlParser
returns an Ergebnis\FrontMatter\Parsed
value object on success, regardless of whether the value has front matter or not.
Changelog
Please have a look at CHANGELOG.md
.
Contributing
Please have a look at CONTRIBUTING.md
.
Code of Conduct
Please have a look at CODE_OF_CONDUCT.md
.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.
Credits
This package is inspired by webuni/front-matter
, originally licensed under MIT by Martin Hasoň.
Curious what I am building?
📬 Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.