ergebnis/front-matter

Provides a GitHub repository template for a PHP library, using GitHub actions.

Fund package maintenance!
localheinz

0.2.0 2020-12-30 09:58 UTC

README

Integrate Merge Prune Release Renew

Code Coverage Type Coverage

Latest Stable Version Total 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\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.