InteractsWithFrontMatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A matter() 0 7 2
A data() 0 6 1
A has() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Concerns;
6
7
use Illuminate\Support\Arr;
8
9
use function array_filter;
10
use function array_merge;
11
use function blank;
12
13
/**
14
 * Adds methods to a class to allow it to fluently interact with its front matter.
15
 */
16
trait 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
}
53