1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
4
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
5
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
6
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
7
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
8
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace TwitterWidgets\Timeline; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use TwitterWidgets\Options\WidgetOptionsInterface; |
15
|
|
|
use Zend\Filter\FilterChain; |
16
|
|
|
use Zend\Filter\StringToLower; |
17
|
|
|
use Zend\Filter\Word\SeparatorToSeparator; |
18
|
|
|
|
19
|
|
|
class TimelineBuilder implements TimelineBuilderInterface |
20
|
|
|
{ |
21
|
|
|
protected $options; |
22
|
|
|
protected $addJs; |
23
|
|
|
protected $filteredAttr; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param WidgetOptionsInterface $options |
27
|
|
|
*/ |
28
|
4 |
|
public function __construct(WidgetOptionsInterface $options) |
29
|
|
|
{ |
30
|
4 |
|
$this->options = $options; |
31
|
4 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
3 |
|
protected function buildWidget() |
37
|
|
|
{ |
38
|
3 |
|
$attributesList = $this->filteredAttr; |
39
|
3 |
|
$widget = '<a'; |
40
|
|
|
|
41
|
3 |
|
foreach ($attributesList as $attrKey => $attrValue) { |
42
|
1 |
|
$widget .= ' ' . $attrKey . '="' . $attrValue . '"'; |
43
|
|
|
} |
44
|
3 |
|
$widget .= '>'; |
45
|
3 |
|
$widget .= $this->options['href_text']; |
46
|
3 |
|
$widget .= '</a>'; |
47
|
|
|
|
48
|
3 |
|
if ($this->addJs) { |
49
|
1 |
|
return $widget . "\n<script>" . $this->getSingleWidgetJs() . "</script>"; |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
return $widget; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param bool $addJs |
57
|
|
|
* @return string |
58
|
|
|
* @throws InvalidArgumentException |
59
|
|
|
*/ |
60
|
4 |
|
public function renderWidget($addJs = true) |
61
|
|
|
{ |
62
|
4 |
|
if (!is_bool($addJs)) { |
63
|
1 |
|
throw new InvalidArgumentException('TimelineBuilder#renderWidget expects a boolean as parameter'); |
64
|
|
|
} |
65
|
3 |
|
$this->addJs = $addJs; |
66
|
3 |
|
$this->filteredAttr = $this->filterAttr(); |
67
|
|
|
|
68
|
3 |
|
return $this->buildWidget(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
* |
74
|
|
|
* Add the necessary code for a single widget |
75
|
|
|
*/ |
76
|
1 |
|
protected function getSingleWidgetJs() |
77
|
|
|
{ |
78
|
|
|
return '!function (d, s, id) { |
79
|
|
|
var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? \'http\' : \'https\'; |
80
|
|
|
if (!d.getElementById(id)) { |
81
|
|
|
js = d.createElement(s); |
82
|
|
|
js.id = id; |
83
|
|
|
js.src = p + "://platform.twitter.com/widgets.js"; |
84
|
|
|
fjs.parentNode.insertBefore(js, fjs); |
85
|
|
|
} |
86
|
1 |
|
}(document, "script", "twitter-wjs");'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
1 |
|
protected function filterAttr() |
93
|
|
|
{ |
94
|
1 |
|
if (!is_array($this->options)) { |
95
|
1 |
|
$this->options = $this->options->toArray(); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
$filterChain = new FilterChain(); |
99
|
|
|
$filterChain |
100
|
1 |
|
->attach(new SeparatorToSeparator('_', '-')) |
101
|
1 |
|
->attach(new StringToLower()); |
102
|
|
|
|
103
|
1 |
|
$filteredAttr = []; |
104
|
1 |
|
foreach ($this->options as $attribute => $value) { |
105
|
1 |
|
if (null !== $value) { |
106
|
1 |
|
$filteredAttr[$filterChain->filter($attribute)] = $value; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
unset($filteredAttr['href-text']); |
111
|
|
|
|
112
|
1 |
|
return $filteredAttr; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|