This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Interactive.php
264 lines (228 loc) · 6.02 KB
/
Interactive.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
namespace Facebook\InstantArticles\Elements;
use Facebook\InstantArticles\Validators\Type;
/**
* An Interactive graphic.
*
* Example:
* <figure class="op-interactive">
* <iframe class="no-margin" src="http://example.com/custom-interactive" height="60"></iframe>
* <figcaption>This graphic is awesome.</figcaption>
* </figure>
*
* @see {link:https://developers.intern.facebook.com/docs/instant-articles/reference/interactive}
*/
class Interactive extends ElementWithHTML implements ChildrenContainer
{
const NO_MARGIN = 'no-margin';
const COLUMN_WIDTH = 'column-width';
/**
* @var Caption Descriptive text for your social embed.
*/
private $caption;
/**
* @var int The width of your interactive graphic.
*/
private $width;
/**
* @var int The height of your interactive graphic.
*/
private $height;
/**
* @var string The source of the content for your interactive graphic.
*/
private $source;
/**
* @var string The width setting for the interactive graphic.
* @see Interactive::NO_MARGIN
* @see Interactive::COLUMN_WIDTH
*/
private $margin;
private function __construct()
{
}
public static function create()
{
return new self();
}
/**
* Sets the caption for the social embed.
*
* @param Caption $caption - Descriptive text for your social embed.
*
* @return $this
*/
public function withCaption($caption)
{
Type::enforce($caption, Caption::getClassName());
$this->caption = $caption;
return $this;
}
/**
* Sets the width of your interactive graphic.
*
* @param int $width The height of your interactive graphic.
*
* @return $this
*/
public function withWidth($width)
{
Type::enforce($width, Type::INTEGER);
$this->width = $width;
return $this;
}
/**
* Sets the height of your interactive graphic.
*
* @param int $height The height of your interactive graphic.
*
* @return $this
*/
public function withHeight($height)
{
Type::enforce($height, Type::INTEGER);
$this->height = $height;
return $this;
}
/**
* Sets the source for the interactive graphic.
*
* @param string $source The source of the content for your interactive graphic.
*
* @return $this
*/
public function withSource($source)
{
Type::enforce($source, Type::STRING);
$this->source = $source;
return $this;
}
/**
* Sets the margin setting of your interactive graphic.
*
* @param string $margin The margin setting of your interactive graphic.
*
* @see Interactive::NO_MARGIN
* @see Interactive::COLUMN_WIDTH
*
* @return $this
*/
public function withMargin($margin)
{
Type::enforceWithin(
$margin,
[
Interactive::NO_MARGIN,
Interactive::COLUMN_WIDTH
]
);
$this->margin = $margin;
return $this;
}
/**
* @return Caption the caption element
*/
public function getCaption()
{
return $this->caption;
}
/**
* @return int the width
*/
public function getWidth()
{
return $this->width;
}
/**
* @return int the height
*/
public function getHeight()
{
return $this->height;
}
/**
* @return string url source
*/
public function getSource()
{
return $this->source;
}
/**
* @return string the margin
*/
public function getMargin()
{
return $this->margin;
}
/**
* Structure and create the full Interactive in a DOMElement.
*
* @param \DOMDocument $document - The document where this element will be appended (optional).
*
* @return \DOMElement
*/
public function toDOMElement($document = null)
{
if (!$document) {
$document = new \DOMDocument();
}
$figure = $document->createElement('figure');
$iframe = $document->createElement('iframe');
$figure->appendChild($iframe);
$figure->setAttribute('class', 'op-interactive');
// Caption markup optional
Element::appendChild($figure, $this->caption, $document);
if ($this->source) {
$iframe->setAttribute('src', $this->source);
}
if ($this->margin) {
$iframe->setAttribute('class', $this->margin);
}
if ($this->width) {
$iframe->setAttribute('width', $this->width);
}
if ($this->height) {
$iframe->setAttribute('height', $this->height);
}
// Ad markup
if ($this->html) {
// Here we do not care about what is inside the iframe
// because it'll be rendered in a sandboxed webview
$this->dangerouslyAppendUnescapedHTML($iframe, $this->html);
} else {
$iframe->appendChild($document->createTextNode(''));
}
return $figure;
}
/**
* Overrides the Element::isValid().
*
* @see Element::isValid().
* @return true for valid Interactive that contains valid source or html, false otherwise.
*/
public function isValid()
{
return $this->html || (!Type::isTextEmpty($this->source) && $this->height && $this->width);
}
/**
* Implements the ChildrenContainer::getContainerChildren().
*
* @see ChildrenContainer::getContainerChildren().
* @return array of Elements contained by Image.
*/
public function getContainerChildren()
{
$children = array();
if ($this->caption) {
$children[] = $this->caption;
}
return $children;
}
}