Passed
Push — master ( 3a6cbe...28e8ac )
by Caen
03:01 queued 12s
created

InteractsWithFrontMatter::data()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Concerns;
6
7
use function array_filter;
8
use function array_merge;
9
use function blank;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Adds methods to a class to allow it to fluently interact with the front matter.
14
 */
15
trait InteractsWithFrontMatter
16
{
17
    /**
18
     * Get a value from the computed page data, or fallback to the page's front matter, then to the default value.
19
     *
20
     * @return \Hyde\Markdown\Models\FrontMatter|mixed
21
     */
22
    public function data(string $key = null, mixed $default = null): mixed
23
    {
24
        return Arr::get(array_filter(array_merge(
25
            $this->matter->toArray(),
26
            (array) $this,
27
        )), $key, $default);
28
    }
29
30
    /**
31
     * Get the front matter object, or a value from within.
32
     *
33
     * @return \Hyde\Markdown\Models\FrontMatter|mixed
34
     */
35
    public function matter(string $key = null, mixed $default = null): mixed
36
    {
37
        return $this->matter->get($key, $default);
38
    }
39
40
    /**
41
     * See if a value exists in the computed page data or the front matter.
42
     */
43
    public function has(string $key): bool
44
    {
45
        return ! blank($this->data($key));
46
    }
47
}
48