|
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\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use DateTime; |
|
15
|
|
|
use Throwable; |
|
16
|
|
|
use Twig\Environment; |
|
17
|
|
|
use Twig\TwigFunction; |
|
18
|
|
|
use WBW\Library\Symfony\Manager\QuoteManager; |
|
19
|
|
|
use WBW\Library\Symfony\Manager\QuoteManagerTrait; |
|
20
|
|
|
use WBW\Library\Symfony\Model\QuoteInterface; |
|
21
|
|
|
use WBW\Library\Symfony\Provider\QuoteProviderInterface; |
|
22
|
|
|
use WBW\Library\Types\Helper\ArrayHelper; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Quote Twig extension. |
|
26
|
|
|
* |
|
27
|
|
|
* @author webeweb <https://github.com/webeweb> |
|
28
|
|
|
* @package WBW\Bundle\CoreBundle\Twig\Extension |
|
29
|
|
|
*/ |
|
30
|
|
|
class QuoteTwigExtension extends AbstractTwigExtension { |
|
31
|
|
|
|
|
32
|
1 |
|
use QuoteManagerTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Service name. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
const SERVICE_NAME = "wbw.core.twig.extension.quote"; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Environment $twigEnvironment The Twig environment. |
|
45
|
|
|
* @param QuoteManager $quoteManager The quote manager. |
|
46
|
|
|
*/ |
|
47
|
160 |
|
public function __construct(Environment $twigEnvironment, QuoteManager $quoteManager) { |
|
48
|
160 |
|
parent::__construct($twigEnvironment); |
|
49
|
160 |
|
|
|
50
|
160 |
|
$this->setQuoteManager($quoteManager); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the Twig functions. |
|
55
|
|
|
* |
|
56
|
|
|
* @return TwigFunction[] Returns the Twig functions. |
|
57
|
40 |
|
*/ |
|
58
|
|
|
public function getFunctions(): array { |
|
59
|
40 |
|
|
|
60
|
40 |
|
return [ |
|
61
|
40 |
|
new TwigFunction("quote", [$this, "quoteFunction"], ["is_safe" => ["html"]]), |
|
62
|
|
|
new TwigFunction("quoteAuthor", [$this, "quoteAuthorFunction"], ["is_safe" => ["html"]]), |
|
63
|
|
|
new TwigFunction("quoteContent", [$this, "quoteContentFunction"], ["is_safe" => ["html"]]), |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get the quote provider. |
|
69
|
|
|
* |
|
70
|
|
|
* @param string|null $domain The domain. |
|
71
|
70 |
|
* @return QuoteProviderInterface|null Returns the quote provider. |
|
72
|
|
|
*/ |
|
73
|
70 |
|
protected function getQuoteProvider(string $domain = null): ?QuoteProviderInterface { |
|
74
|
10 |
|
|
|
75
|
|
|
if (false === $this->getQuoteManager()->hasProviders()) { |
|
76
|
|
|
return null; |
|
77
|
60 |
|
} |
|
78
|
40 |
|
|
|
79
|
|
|
if (null !== $domain) { |
|
80
|
|
|
return $this->getQuoteManager()->getProvider($domain); |
|
81
|
30 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->getQuoteManager()->getProviders()[0]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Display a quote author. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string|null $domain The domain. |
|
90
|
|
|
* @return string Returns the quote author. |
|
91
|
20 |
|
* @throws Throwable Throws an exception if an error occurs. |
|
92
|
|
|
*/ |
|
93
|
20 |
|
public function quoteAuthorFunction(string $domain = null): string { |
|
94
|
20 |
|
|
|
95
|
10 |
|
$quote = $this->quoteFunction($domain); |
|
96
|
|
|
if (null === $quote) { |
|
97
|
|
|
return ""; |
|
98
|
10 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $quote->getAuthor(); |
|
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Display a quote content. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string|null $domain The domain. |
|
107
|
|
|
* @return string Returns the quote content. |
|
108
|
20 |
|
* @throws Throwable Throws an exception if an error occurs. |
|
109
|
|
|
*/ |
|
110
|
20 |
|
public function quoteContentFunction(string $domain = null): ?string { |
|
111
|
20 |
|
|
|
112
|
10 |
|
$quote = $this->quoteFunction($domain); |
|
113
|
|
|
if (null === $quote) { |
|
114
|
|
|
return ""; |
|
115
|
10 |
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $quote->getContent(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the quote. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string|null $domain The domain. |
|
124
|
|
|
* @return QuoteInterface|null Returns the quote. |
|
125
|
70 |
|
* @throws Throwable Throws an exception if an error occurs. |
|
126
|
|
|
*/ |
|
127
|
70 |
|
public function quoteFunction(string $domain = null): ?QuoteInterface { |
|
128
|
70 |
|
|
|
129
|
40 |
|
$provider = $this->getQuoteProvider($domain); |
|
130
|
|
|
if (null === $provider) { |
|
131
|
|
|
return null; |
|
132
|
30 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
return ArrayHelper::get($provider->getQuotes(), (new DateTime())->format("m.d")); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|