ergebnis/front-matter

Provides a front matter parser.

2.1.0 2023-02-28 16:53 UTC

README

Integrate Merge Release Renew

Code Coverage Type Coverage

Latest Stable Version Total Downloads Monthly Downloads

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\FrontMatterCanNotBeParsed exception when the front matter is invalid YAML and an Ergebnis\FrontMatter\Exception\FrontMatterIsNotAnObject exception when the front matter does not describe an object.

💡 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 up to?

Follow me on Twitter!