MarkdownDocument::stripExtension()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chinstrap\Core\Objects;
6
7
use Chinstrap\Core\Contracts\Objects\Document;
8
use DateTime;
9
use JsonSerializable;
10
11
final class MarkdownDocument implements Document, JsonSerializable
12
{
13
    protected string $content;
14
15
    /**
16
     * @var array
17
     */
18
    protected array $data = [];
19
20
    protected string $path;
21
22
    /**
23
     * Date/time updated
24
     *
25
     * @var DateTime|null
26
     */
27
    private $updatedAt;
28
29
    public function __construct()
30
    {
31
        $this->content = '';
32
        $this->data = [];
33
        $this->path = '';
34
    }
35
36
    public function getContent(): string
37
    {
38
        return $this->content;
39
    }
40
41
    public function setContent(string $data): Document
42
    {
43
        $this->content = $data;
44
        return $this;
45
    }
46
47
    /**
48
     * @return mixed
49
     */
50
    public function getField(string $key)
51
    {
52
        if (!isset($this->data[$key])) {
53
            return null;
54
        }
55
        return $this->data[$key];
56
    }
57
58
    /**
59
     * @param string $key
60
     * @param array|string $value
61
     */
62
    public function setField(string $key, $value): Document
63
    {
64
        $this->data[$key] = $value;
65
        return $this;
66
    }
67
68
    public function getPath(): string
69
    {
70
        return $this->path;
71
    }
72
73
    public function getUrl(): string
74
    {
75
        return '/' . preg_replace('/index$/', '', $this->stripExtension($this->getPath()));
76
    }
77
78
    public function setPath(string $path): Document
79
    {
80
        $this->path = $path;
81
        return $this;
82
    }
83
84
    public function getFields(): array
85
    {
86
        return $this->data;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function jsonSerialize(): array
93
    {
94
        $data = $this->getFields();
95
        $data['content'] = $this->getContent();
96
        $data['url'] = $this->getUrl();
97
        return $data;
98
    }
99
100
    public function getUpdatedAt(): DateTime
101
    {
102
        return $this->updatedAt;
103
    }
104
105
    public function setUpdatedAt(DateTime $updatedAt): Document
106
    {
107
        $this->updatedAt = $updatedAt;
108
        return $this;
109
    }
110
111
    private function stripExtension(string $path): string
112
    {
113
        return preg_replace('/.(markdown|md)$/', '', $path);
114
    }
115
116
    public function __toString(): string
117
    {
118
        return $this->content;
119
    }
120
121
    public function __get(string $name): string
122
    {
123
        if ($name === 'content') {
124
            return $this->getContent();
125
        }
126
        if ($name === 'path') {
127
            return $this->getPath();
128
        }
129
        return $this->getField($name);
130
    }
131
132
    /**
133
     * @return self
134
     */
135
    public function __set(string $name, string $value)
136
    {
137
        if ($name === 'content') {
138
            $this->setContent($value);
139
        } elseif ($name === 'path') {
140
            $this->setPath($value);
141
        } else {
142
            $this->setField($name, $value);
143
        }
144
        return $this;
145
    }
146
}
147