|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of Symplify |
|
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_Sculpin\Renderable\File; |
|
11
|
|
|
|
|
12
|
|
|
use ArrayAccess; |
|
13
|
|
|
use DateTimeInterface; |
|
14
|
|
|
use Exception; |
|
15
|
|
|
use SplFileInfo; |
|
16
|
|
|
use Symplify\PHP7_Sculpin\Utils\PathAnalyzer; |
|
17
|
|
|
|
|
18
|
|
|
final class PostFile extends AbstractFile implements ArrayAccess |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var DateTimeInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $date; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $filenameWithoutDate; |
|
29
|
5 |
|
|
|
30
|
|
|
public function __construct(SplFileInfo $fileInfo, string $relativeSource) |
|
31
|
5 |
|
{ |
|
32
|
5 |
|
parent::__construct($fileInfo, $relativeSource); |
|
33
|
4 |
|
|
|
34
|
4 |
|
$this->ensurePathStartsWithDate($fileInfo); |
|
35
|
4 |
|
|
|
36
|
|
|
$this->date = PathAnalyzer::detectDate($fileInfo); |
|
37
|
1 |
|
$this->filenameWithoutDate = PathAnalyzer::detectFilenameWithoutDate($fileInfo); |
|
38
|
|
|
} |
|
39
|
1 |
|
|
|
40
|
|
|
public function getDate() : DateTimeInterface |
|
41
|
|
|
{ |
|
42
|
4 |
|
return $this->date; |
|
43
|
|
|
} |
|
44
|
4 |
|
|
|
45
|
|
|
public function getDateInFormat(string $format) : string |
|
46
|
|
|
{ |
|
47
|
4 |
|
return $this->date->format($format); |
|
48
|
|
|
} |
|
49
|
4 |
|
|
|
50
|
|
|
public function getFilenameWithoutDate() : string |
|
51
|
|
|
{ |
|
52
|
5 |
|
return $this->filenameWithoutDate; |
|
53
|
|
|
} |
|
54
|
5 |
|
|
|
55
|
1 |
|
public function offsetGet($offset) |
|
56
|
1 |
|
{ |
|
57
|
|
|
if ($offset === 'content') { |
|
58
|
|
|
return $this->getContent(); |
|
59
|
4 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
if ($offset === 'date') { |
|
62
|
|
|
return $this->getDate(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (!isset($this->configuration[$offset])) { |
|
66
|
|
|
throw new \Exception(sprintf( |
|
67
|
|
|
'Value "%s" was not found for "%s" object. Available values are "%s"', |
|
68
|
|
|
$offset, |
|
69
|
|
|
get_class(), |
|
70
|
|
|
implode('", "', array_keys($this->configuration)) |
|
71
|
|
|
)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this->configuration[$offset]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function offsetExists($offset) |
|
78
|
|
|
{ |
|
79
|
|
|
throw new Exception('not supported'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function offsetSet($offset, $value) |
|
83
|
|
|
{ |
|
84
|
|
|
throw new Exception('not supported'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function offsetUnset($offset) |
|
88
|
|
|
{ |
|
89
|
|
|
throw new Exception('not supported'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function ensurePathStartsWithDate(SplFileInfo $fileInfo) |
|
93
|
|
|
{ |
|
94
|
|
|
if (! PathAnalyzer::startsWithDate($fileInfo)) { |
|
95
|
|
|
throw new Exception( |
|
96
|
|
|
'Post name has to start with a date in "Y-m-d" format. E.g. "2016-01-01-name.md".' |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|