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 TwitterWidgetBundle\Extension; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use TwitterWidgets\Assets\OneTimeJsProvider; |
15
|
|
|
use TwitterWidgets\Options\WidgetOptions; |
16
|
|
|
use TwitterWidgets\Options\WidgetOptionsInterface; |
17
|
|
|
use TwitterWidgets\Timeline\TimelineBuilderInterface; |
18
|
|
|
|
19
|
|
|
class TimelineWidget extends \Twig_Extension |
20
|
|
|
{ |
21
|
|
|
protected $widgetOptions; |
22
|
|
|
protected $timeline; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param WidgetOptions $widgetOptions |
26
|
|
|
* @param TimelineBuilderInterface $timeline |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct(WidgetOptions $widgetOptions, TimelineBuilderInterface $timeline) |
29
|
|
|
{ |
30
|
6 |
|
$this->widgetOptions = $widgetOptions; |
31
|
6 |
|
$this->timeline = $timeline; |
32
|
6 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
1 |
|
public function getFunctions() |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
1 |
|
new \Twig_SimpleFunction('tw', [$this, 'renderWidget'], ['is_safe' => ['html']]), |
41
|
1 |
|
new \Twig_SimpleFunction('twJs', [$this, 'getOneTimeWidgetJs'], ['is_safe' => ['html']]), |
42
|
|
|
|
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
1 |
|
public function getName() |
50
|
|
|
{ |
51
|
1 |
|
return 'timelinewidgetextensions'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array|WidgetOptionsInterface $options |
56
|
|
|
* @param bool $addJs |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
2 |
|
public function renderWidget($options, $addJs = true) |
60
|
|
|
{ |
61
|
2 |
|
if (!is_array($options) && !($options instanceof WidgetOptionsInterface)) { |
62
|
1 |
|
throw new InvalidArgumentException( |
63
|
1 |
|
'"options" must be an array or an implementation of WidgetOptionsInterface' |
64
|
|
|
); |
65
|
|
|
} |
66
|
1 |
|
$this->widgetOptions->setFromArray($options); |
|
|
|
|
67
|
|
|
|
68
|
1 |
|
return $this->timeline->renderWidget($addJs); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
1 |
|
public function getOneTimeWidgetJs() |
75
|
|
|
{ |
76
|
1 |
|
return (new OneTimeJsProvider())->getOneTimeWidgetJs(); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.