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
|
|
|
|
30
|
5 |
|
public function __construct(SplFileInfo $fileInfo, string $relativeSource) |
31
|
|
|
{ |
32
|
5 |
|
parent::__construct($fileInfo, $relativeSource); |
33
|
|
|
|
34
|
5 |
|
$this->ensurePathStartsWithDate($fileInfo); |
35
|
|
|
|
36
|
4 |
|
$this->date = PathAnalyzer::detectDate($fileInfo); |
37
|
4 |
|
$this->filenameWithoutDate = PathAnalyzer::detectFilenameWithoutDate($fileInfo); |
38
|
4 |
|
} |
39
|
|
|
|
40
|
1 |
|
public function getDate() : DateTimeInterface |
41
|
|
|
{ |
42
|
1 |
|
return $this->date; |
43
|
|
|
} |
44
|
|
|
|
45
|
4 |
|
public function getDateInFormat(string $format) : string |
46
|
|
|
{ |
47
|
4 |
|
return $this->date->format($format); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public function getFilenameWithoutDate() : string |
51
|
|
|
{ |
52
|
4 |
|
return $this->filenameWithoutDate; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function offsetGet($offset) |
56
|
|
|
{ |
57
|
|
|
if ($offset === 'content') { |
58
|
|
|
return $this->getContent(); |
59
|
|
|
} |
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
|
5 |
|
private function ensurePathStartsWithDate(SplFileInfo $fileInfo) |
93
|
|
|
{ |
94
|
5 |
|
if (! PathAnalyzer::startsWithDate($fileInfo)) { |
95
|
1 |
|
throw new Exception( |
96
|
1 |
|
'Post name has to start with a date in "Y-m-d" format. E.g. "2016-01-01-name.md".' |
97
|
|
|
); |
98
|
|
|
} |
99
|
4 |
|
} |
100
|
|
|
} |
101
|
|
|
|