1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2019 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\CoreBundle\Quote; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Symfony\Component\Filesystem\Exception\FileNotFoundException; |
16
|
|
|
use Symfony\Component\Yaml\Yaml; |
17
|
|
|
use WBW\Library\Core\Argument\ArrayHelper; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* YAML Quote provider. |
21
|
|
|
* |
22
|
|
|
* @author webeweb <https://github.com/webeweb/> |
23
|
|
|
* @package WBW\Bundle\CoreBundle\Quote |
24
|
|
|
*/ |
25
|
|
|
class YamlQuoteProvider extends AbstractQuoteProvider { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Filename. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $filename; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param string $filename The filename. |
38
|
|
|
* @throws FileNotFoundException Throws a file not found exception. |
39
|
|
|
*/ |
40
|
|
|
public function __construct($filename) { |
41
|
|
|
$this->setFilename($filename); |
42
|
|
|
$this->setQuotes([]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function getDomain() { |
49
|
|
|
return basename($this->getFilename(), ".yml"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the filename. |
54
|
|
|
* |
55
|
|
|
* @return string Returns the filename. |
56
|
|
|
*/ |
57
|
|
|
public function getFilename() { |
58
|
|
|
return $this->filename; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function init() { |
65
|
|
|
|
66
|
|
|
$fileContent = file_get_contents($this->filename); |
67
|
|
|
|
68
|
|
|
$yamlContent = Yaml::parse($fileContent); |
69
|
|
|
|
70
|
|
|
foreach ($yamlContent as $k => $v) { |
71
|
|
|
|
72
|
|
|
// Force year to manage the leap years. |
73
|
|
|
$date = DateTime::createFromFormat("!Y.m.d", "2016." . $k); |
74
|
|
|
|
75
|
|
|
$model = new Quote(); |
76
|
|
|
$model->setDate(false === $date ? null : $date); |
77
|
|
|
$model->setAuthor(ArrayHelper::get($v, "author")); |
78
|
|
|
$model->setContent(ArrayHelper::get($v, "content")); |
79
|
|
|
|
80
|
|
|
$this->quotes[$k] = $model; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set the filename. |
86
|
|
|
* |
87
|
|
|
* @param string $filename The filename. |
88
|
|
|
* @return YamlQuoteProvider Returns this quote provider. |
89
|
|
|
* @throws FileNotFoundException Throws a file not found exception. |
90
|
|
|
*/ |
91
|
|
|
protected function setFilename($filename) { |
92
|
|
|
if (false === realpath($filename)) { |
93
|
|
|
throw new FileNotFoundException(sprintf("The file \"%s\" was not found", $filename)); |
94
|
|
|
} |
95
|
|
|
$this->filename = realpath($filename); |
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|