|
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\Bundle\CoreBundle\Provider\QuoteProviderInterface; |
|
18
|
|
|
use WBW\Library\Core\Argument\ArrayHelper; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Quote provider. |
|
22
|
|
|
* |
|
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
|
24
|
|
|
* @package WBW\Bundle\CoreBundle\Quote |
|
25
|
|
|
*/ |
|
26
|
|
|
class QuoteProvider implements QuoteProviderInterface { |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Filename. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $filename; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Quotes. |
|
37
|
|
|
* |
|
38
|
|
|
* @var QuoteInterface[] |
|
39
|
|
|
*/ |
|
40
|
|
|
private $quotes; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $filename The filename. |
|
46
|
|
|
* @throws FileNotFoundException Throws a file not found exception. |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($filename) { |
|
49
|
|
|
$this->setFilename($filename); |
|
50
|
|
|
$this->setQuotes([]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
*{@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getAuthors() { |
|
57
|
|
|
|
|
58
|
|
|
$authors = []; |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->quotes as $current) { |
|
61
|
|
|
if (true === in_array($current->getAuthor(), $authors)) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
$authors[] = $current->getAuthor(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
asort($authors); |
|
68
|
|
|
|
|
69
|
|
|
return $authors; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the filename. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string Returns the filename. |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getFilename() { |
|
78
|
|
|
return $this->filename; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the quotes. |
|
83
|
|
|
* |
|
84
|
|
|
* @return QuoteInterface[] Returns the quotes. |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getQuotes() { |
|
87
|
|
|
return $this->quotes; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Parses. |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function parse() { |
|
96
|
|
|
|
|
97
|
|
|
$fileContent = file_get_contents($this->filename); |
|
98
|
|
|
|
|
99
|
|
|
$yamlContent = Yaml::parse($fileContent); |
|
100
|
|
|
|
|
101
|
|
|
foreach ($yamlContent as $k => $v) { |
|
102
|
|
|
|
|
103
|
|
|
$date = DateTime::createFromFormat("!m.d", $k); |
|
104
|
|
|
|
|
105
|
|
|
$model = new Quote(); |
|
106
|
|
|
$model->setDate(false === $date ? nulll : $date); |
|
107
|
|
|
$model->setAuthor(ArrayHelper::get($v, "author")); |
|
108
|
|
|
$model->setContent(ArrayHelper::get($v, "content")); |
|
109
|
|
|
|
|
110
|
|
|
$this->quotes[] = $model; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Set the filename. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $filename The filename. |
|
118
|
|
|
* @return QuoteProvider Returns this quote provider. |
|
119
|
|
|
* @throws FileNotFoundException Throws a file not found exception. |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function setFilename($filename) { |
|
122
|
|
|
if (false === realpath($filename)) { |
|
123
|
|
|
throw new FileNotFoundException(sprintf("The file \"%s\" was not found", $filename)); |
|
124
|
|
|
} |
|
125
|
|
|
$this->filename = realpath($filename); |
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Set the quotes. |
|
131
|
|
|
* |
|
132
|
|
|
* @param QuoteInterface[] $quotes The quotes. |
|
133
|
|
|
* @return QuoteProvider Returns this quote provider. |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function setQuotes(array $quotes) { |
|
136
|
|
|
$this->quotes = $quotes; |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|