Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
InteractsWithFrontMatter
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
3 / 3
4
100.00% covered (success)
100.00%
1 / 1
 data
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 matter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Concerns;
6
7use Illuminate\Support\Arr;
8
9use function array_filter;
10use function array_merge;
11use function blank;
12
13/**
14 * Adds methods to a class to allow it to fluently interact with its front matter.
15 */
16trait InteractsWithFrontMatter
17{
18    /**
19     * Get a value from the computed page data, or fallback to the page's front matter, then to the default value.
20     *
21     * @return \Hyde\Markdown\Models\FrontMatter|mixed
22     */
23    public function data(string $key = null, mixed $default = null): mixed
24    {
25        return Arr::get(array_filter(array_merge(
26            $this->matter->toArray(),
27            (array) $this,
28        )), $key, $default);
29    }
30
31    /**
32     * Get the front matter object, or a value from within.
33     *
34     * @return \Hyde\Markdown\Models\FrontMatter|mixed
35     */
36    public function matter(string $key = null, mixed $default = null): mixed
37    {
38        if ($key) {
39            return $this->matter->get($key, $default);
40        }
41
42        return $this->matter;
43    }
44
45    /**
46     * See if a value exists in the computed page data or the front matter.
47     */
48    public function has(string $key): bool
49    {
50        return ! blank($this->data($key));
51    }
52}