1 | <?php |
||
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 |
|
70 | |||
71 | 2 | public function configure(array $config) |
|
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 |
|
112 | |||
113 | 1 | public function __toString() : string |
|
117 | } |
||
118 |