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