Passed
Branch master (1e39e8)
by Caen
03:01
created

FrontMatter::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models;
4
5
use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Support\Arr;
8
9
/**
10
 * Object representing the YAML front matter of a Markdown file.
11
 *
12
 * @see \Hyde\Framework\Testing\Unit\FrontMatterModelTest
13
 */
14
class FrontMatter implements Arrayable, \Stringable
15
{
16
    public array $matter;
17
18
    public function __construct(array $matter = [])
19
    {
20
        $this->matter = $matter;
21
    }
22
23
    public function __toString(): string
24
    {
25
        return (new ConvertsArrayToFrontMatter())->execute($this->matter);
26
    }
27
28
    public function __get(string $key): mixed
29
    {
30
        return $this->get($key);
31
    }
32
33
    public function get(string $key = null, mixed $default = null): mixed
34
    {
35
        if ($key) {
36
            return Arr::get($this->matter, $key, $default);
37
        }
38
39
        return $this->matter;
40
    }
41
42
    public function set(string $key, mixed $value): static
43
    {
44
        $this->matter[$key] = $value;
45
46
        return $this;
47
    }
48
49
    public function toArray(): array
50
    {
51
        return $this->matter;
52
    }
53
54
    public static function fromArray(array $matter): static
55
    {
56
        return new static($matter);
57
    }
58
}
59