1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Disqus Helper package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Nikola Posa <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For full copyright and license information, please refer to the LICENSE file, |
8
|
|
|
* located at the package root folder. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace DisqusHelper; |
14
|
|
|
|
15
|
|
|
use DisqusHelper\Widget\WidgetLocatorInterface; |
16
|
|
|
use DisqusHelper\Widget\WidgetManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @method string thread(array $options = []) |
20
|
|
|
* @method string commentsCount(array $options = []) |
21
|
|
|
* |
22
|
|
|
* @author Nikola Posa <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class Disqus |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $shortName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var WidgetLocatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $widgetLocator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Code |
38
|
|
|
*/ |
39
|
|
|
private $code; |
40
|
|
|
|
41
|
7 |
|
private function __construct() |
42
|
|
|
{ |
43
|
7 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $shortName Unique identifier of some Disqus website. |
47
|
|
|
* @param WidgetLocatorInterface $widgetLocator OPTIONAL |
48
|
|
|
* |
49
|
|
|
* @return Disqus |
50
|
|
|
*/ |
51
|
7 |
|
public static function create( |
52
|
|
|
string $shortName, |
53
|
|
|
WidgetLocatorInterface $widgetLocator = null |
54
|
|
|
) : Disqus { |
55
|
7 |
|
$disqusHelper = new self(); |
56
|
|
|
|
57
|
7 |
|
$disqusHelper->shortName = $shortName; |
58
|
|
|
|
59
|
7 |
|
$disqusHelper->widgetLocator = $widgetLocator ?? WidgetManager::createWithDefaultWidgets(); |
60
|
|
|
|
61
|
7 |
|
$disqusHelper->code = Code::create($shortName); |
62
|
|
|
|
63
|
7 |
|
return $disqusHelper; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function getShortName() : string |
67
|
|
|
{ |
68
|
1 |
|
return $this->shortName; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
public function configure(array $config) |
72
|
|
|
{ |
73
|
2 |
|
$this->code->mergeConfig($config); |
74
|
2 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Proxies calls to some Disqus widget and returns HTML output. |
78
|
|
|
* |
79
|
|
|
* @param string $widgetId |
80
|
|
|
* @param array $args |
81
|
|
|
* |
82
|
|
|
* @throws Exception\InvalidArgumentException |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
5 |
|
public function __call($widgetId, $args) : string |
87
|
|
|
{ |
88
|
5 |
|
$widget = $this->widgetLocator->get($widgetId); |
89
|
|
|
|
90
|
4 |
|
if (($options = array_shift($args)) !== null) { |
91
|
1 |
|
if (!is_array($options)) { |
92
|
1 |
|
throw new Exception\InvalidArgumentException("Widget options argument should be array"); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
$widget->visit($this->code); |
97
|
|
|
|
98
|
3 |
|
return $widget->render($options ?: []); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Builds JS code which loads configuration and necessary assets. |
103
|
|
|
* |
104
|
|
|
* This method should be called after using and rendering widgets, usually before closing </body> tag. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
3 |
|
public function getCode() : string |
109
|
|
|
{ |
110
|
3 |
|
return $this->code->toHtml(); |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
public function __toString() : string |
114
|
|
|
{ |
115
|
1 |
|
return $this->getCode(); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|