1 | <?php |
||
22 | final class CommentsCountWidget extends BaseWidget |
||
23 | { |
||
24 | const SCRIPT_NAME = 'count.js'; |
||
25 | const SCRIPT_ID = 'dsq-count-scr'; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $defaultOptions = [ |
||
31 | 'url' => null, |
||
32 | 'label' => null, |
||
33 | 'as_link' => true, |
||
34 | 'identifier' => null, |
||
35 | ]; |
||
36 | |||
37 | 4 | public function render(array $options = []) : string |
|
38 | { |
||
39 | 4 | $options = array_merge(self::$defaultOptions, $options); |
|
40 | |||
41 | 4 | $label = htmlspecialchars((string) $options['label'], ENT_QUOTES, 'UTF-8'); |
|
42 | |||
43 | 4 | $attribs = []; |
|
44 | |||
45 | 4 | if (isset($options['identifier'])) { |
|
46 | 2 | $attribs['data-disqus-identifier'] = $options['identifier']; |
|
47 | } |
||
48 | |||
49 | 4 | if ($options['as_link']) { |
|
50 | 3 | return $this->renderLink($options['url'] . '#disqus_thread', $label, $attribs); |
|
51 | } |
||
52 | |||
53 | 1 | if (!empty($options['url'])) { |
|
54 | 1 | $attribs['data-disqus-url'] = $options['url']; |
|
55 | } |
||
56 | |||
57 | 1 | return $this->renderElement($label, $attribs); |
|
58 | } |
||
59 | |||
60 | 3 | private function renderLink(string $href, string $label, array $attribs) : string |
|
61 | { |
||
62 | 3 | $attribs['href'] = $href; |
|
63 | |||
64 | return '<a' |
||
65 | 3 | . ' ' . $this->htmlAttribsToString($attribs) . '>' |
|
66 | 3 | . $label |
|
67 | 3 | . '</a>'; |
|
68 | } |
||
69 | |||
70 | 1 | private function renderElement(string $label, array $attribs) : string |
|
77 | |||
78 | 1 | public function visit(Code $code) : Code |
|
87 | } |
||
88 |