Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
FrontMatter
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
8 / 8
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromArray
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\Markdown\Models;
6
7use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
8use Hyde\Support\Concerns\Serializable;
9use Hyde\Support\Contracts\SerializableContract;
10use Illuminate\Support\Arr;
11use Stringable;
12
13/**
14 * Object representing the YAML front matter of a Markdown file.
15 *
16 * The data here is equal to the YAML. Unless you are using the data to construct dynamic data,
17 * you probably want to call the `get()` method on the Page object, as that will let you
18 * access dynamic computed data if it exists, or it will fall back to this class's data.
19 *
20 * For package developers:
21 * Use $page->data('foo') to access page data + front matter
22 * Use $page->matter('foo') to access front matter only
23 * You can also get the front matter object using $page->matter (which is an instance of this class)
24 *
25 *
26 * @phpstan-consistent-constructor
27 */
28class FrontMatter implements Stringable, SerializableContract
29{
30    use Serializable;
31
32    protected array $data;
33
34    public function __construct(array $matter = [])
35    {
36        $this->data = $matter;
37    }
38
39    public function __toString(): string
40    {
41        return (new ConvertsArrayToFrontMatter())->execute($this->data);
42    }
43
44    /** @return mixed|array */
45    public function __get(string $key): mixed
46    {
47        return $this->get($key);
48    }
49
50    /** @return mixed|array */
51    public function get(string $key = null, mixed $default = null): mixed
52    {
53        if ($key) {
54            return Arr::get($this->data, $key, $default);
55        }
56
57        return $this->data;
58    }
59
60    public function set(string $key, mixed $value): static
61    {
62        $this->data[$key] = $value;
63
64        return $this;
65    }
66
67    public function has(string $key): bool
68    {
69        return Arr::has($this->data, $key);
70    }
71
72    public function toArray(): array
73    {
74        return $this->data;
75    }
76
77    public static function fromArray(array $matter): static
78    {
79        return new static($matter);
80    }
81}