1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matks\MarkdownBlogBundle\Blog; |
4
|
|
|
|
5
|
|
|
class Post |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Post names must be unique. |
9
|
|
|
* |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $name; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Date with format YYYY-MM-DD. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $publishDate; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
private $category; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string[] |
28
|
|
|
*/ |
29
|
|
|
private $tags; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $contentFilepath; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $contentFilepath |
38
|
|
|
* @param string $name |
39
|
|
|
* @param string $publishDate |
40
|
|
|
* @param string $category |
41
|
|
|
* @param string[] $tags |
42
|
|
|
*/ |
43
|
|
|
public function __construct($contentFilepath, $name, $publishDate, $category = null, $tags = []) |
44
|
|
|
{ |
45
|
1 |
|
if (false === file_exists($contentFilepath)) { |
46
|
1 |
|
throw new \InvalidArgumentException("$contentFilepath is not a file"); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$hasMdExtension = ('.md' === substr($contentFilepath, -3)); |
50
|
1 |
|
if (false === $hasMdExtension) { |
51
|
|
|
throw new \InvalidArgumentException("$contentFilepath extension is not .md"); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$this->category = $category; |
55
|
1 |
|
$this->contentFilepath = $contentFilepath; |
56
|
1 |
|
$this->name = $name; |
57
|
1 |
|
$this->publishDate = $publishDate; |
58
|
1 |
|
$this->tags = $tags; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getCategory() |
65
|
|
|
{ |
66
|
|
|
return $this->category; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getContentFilepath() |
73
|
|
|
{ |
74
|
|
|
return $this->contentFilepath; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getName() |
81
|
|
|
{ |
82
|
1 |
|
return $this->name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getPublishDate() |
89
|
|
|
{ |
90
|
|
|
return $this->publishDate; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string[] |
95
|
|
|
*/ |
96
|
|
|
public function getTags() |
97
|
|
|
{ |
98
|
|
|
return $this->tags; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getHtml() |
105
|
|
|
{ |
106
|
1 |
|
$markdownFileContent = file_get_contents($this->contentFilepath); |
107
|
1 |
|
$parsedown = new \Parsedown(); |
108
|
|
|
|
109
|
1 |
|
return $parsedown->text($markdownFileContent); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|