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
/
Header.php
519 lines (450 loc) · 12 KB
/
Header.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
<?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;
/**
* The header of the article. A header can hold an Image,
* Title, Authors and Dates for publishing and modification of the article.
*
* <header>
* <figure>
* <ui:image src={$this->getHeroBackground()} />
* </figure>
* <h1>{$this->name}</h1>
* <address>
* <a rel="facebook" href="http://facebook.com/everton.rosario">Everton</a>
* Everton Rosario is a passionate mountain biker on Facebook
* </address>
* <time
* class="op-published"
* datetime={date('c', $this->time)}>
* {date('F jS, g:ia', $this->time)}
* </time>
* <time
* class="op-modified"
* datetime={date('c', $last_update)}>
* {date('F jS, g:ia', $last_update)}
* </time>
* </header>
*/
class Header extends Element implements ChildrenContainer
{
/**
* @var Image|Video|Slideshow|null for the image or video on the header.
*
* @see Image
* @see Slideshow
* @see Video
*/
private $cover;
/**
* H1 The title of the Article that will be displayed on header.
*/
private $title;
/**
* H2 The subtitle of the Article that will be displayed on header.
*/
private $subtitle;
/**
* @var Author[] Authors of the article.
*/
private $authors = [];
/**
* @var Time of publishing for the article
*/
private $published;
/**
* @var Time of modification of the article, if it has
* updated.
*/
private $modified;
/**
* @var H3 Header kicker
*/
private $kicker;
/**
* @var Ad[] Ads of the article.
*/
private $ads = [];
/**
* @var Sponsor The sponsor for this article. See Branded Content.
*/
private $sponsor;
private function __construct()
{
}
/**
* @return Header
*/
public static function create()
{
return new self();
}
/**
* Sets the cover of InstantArticle with Image or Video
*
* @param Image|Video|Slideshow $cover The cover for the header of the InstantArticle
*
* @return $this
*/
public function withCover($cover)
{
Type::enforce(
$cover,
[
Image::getClassName(),
Slideshow::getClassName(),
Video::getClassName()
]
);
$this->cover = $cover;
return $this;
}
/**
* Sets the title of InstantArticle
*
* @param string|H1 $title The title of the InstantArticle
*
* @return $this
*/
public function withTitle($title)
{
Type::enforce($title, array(Type::STRING, H1::getClassName()));
if (Type::is($title, Type::STRING)) {
$this->title = H1::create()->appendText($title);
} else {
$this->title = $title;
}
return $this;
}
/**
* Sets the subtitle of InstantArticle
*
* @param string|H2 $subtitle The subtitle of the InstantArticle
*
* @return $this
*/
public function withSubTitle($subtitle)
{
Type::enforce($subtitle, array(Type::STRING, H2::getClassName()));
if (Type::is($subtitle, Type::STRING)) {
$this->subtitle = H2::create()->appendText($subtitle);
} else {
$this->subtitle = $subtitle;
}
return $this;
}
/**
* Append another author to the article
*
* @param Author $author The author name
*
* @return $this
*/
public function addAuthor($author)
{
Type::enforce($author, Author::getClassName());
$this->authors[] = $author;
return $this;
}
/**
* Replace all authors within this Article
*
* @param Author[] $authors All the authors
*
* @return $this
*/
public function withAuthors($authors)
{
Type::enforceArrayOf($authors, Author::getClassName());
$this->authors = $authors;
return $this;
}
/**
* Sets the publish Time for this article. REQUIRED
*
* @param Time $published The time and date of publishing of this article. REQUIRED
*
* @return $this
*/
public function withPublishTime($published)
{
Type::enforce($published, Time::getClassName());
$this->published = $published;
return $this;
}
/**
* Sets the update Time for this article. Optional
*
* @param Time $modified The time and date that this article was modified. Optional
*
* @return $this
*/
public function withModifyTime($modified)
{
Type::enforce($modified, Time::getClassName());
$this->modified = $modified;
return $this;
}
/**
* Sets the update Time for this article. Optional
*
* @param Time $time The time and date that this article was modified. Optional
*
* @return $this
*/
public function withTime($time)
{
Type::enforce($time, Time::getClassName());
if ($time->getType() === Time::MODIFIED) {
$this->withModifyTime($time);
} else {
$this->withPublishTime($time);
}
return $this;
}
/**
* Kicker text for the article header.
*
* @param H3|string The kicker text to be set
*
* @return $this
*/
public function withKicker($kicker)
{
Type::enforce($kicker, array(Type::STRING, H3::getClassName()));
if (Type::is($kicker, Type::STRING)) {
$this->kicker = H3::create()->appendText($kicker);
} else {
$this->kicker = $kicker;
}
return $this;
}
/**
* Append another ad to the article
*
* @param Ad $ad Code for displaying an ad
*
* @return $this
*/
public function addAd($ad)
{
Type::enforce($ad, Ad::getClassName());
$this->ads[] = $ad;
return $this;
}
/**
* Replace all ads within this Article
*
* @param Ad[] $ads All the ads
*
* @return $this
*/
public function withAds($ads)
{
Type::enforceArrayOf($ads, Ad::getClassName());
$this->ads = $ads;
return $this;
}
/**
* Sets the sponsor for this Article.
*
* @param Sponsor $sponsor The sponsor of article to be set.
*
* @return $this
*/
public function withSponsor($sponsor)
{
Type::enforce($sponsor, Sponsor::getClassName());
$this->sponsor = $sponsor;
return $this;
}
/**
* @return Image|Slideshow|Video The cover for the header of the InstantArticle
*/
public function getCover()
{
return $this->cover;
}
/**
* @return string|H1 $title The title of the InstantArticle
*/
public function getTitle()
{
return $this->title;
}
/**
* @return string|H2 $subtitle The subtitle of the InstantArticle
*/
public function getSubtitle()
{
return $this->subtitle;
}
/**
* @return Author[] All the authors
*/
public function getAuthors()
{
return $this->authors;
}
/**
* @return Time The time and date of publishing of this article
*/
public function getPublished()
{
return $this->published;
}
/**
* @return Time The time and date that this article was modified.
*/
public function getModified()
{
return $this->modified;
}
/**
* @return string The kicker text to be set
*/
public function getKicker()
{
return $this->kicker;
}
/**
* @return Ad[] All the ads
*/
public function getAds()
{
return $this->ads;
}
/**
* @return Sponsor the sponsor of this Article.
*/
public function getSponsor()
{
return $this->sponsor;
}
/**
* Structure and create the full ArticleImage in a XML format DOMElement.
*
* @param \DOMDocument $document where this element will be appended. Optional
*
* @return \DOMElement
*/
public function toDOMElement($document = null)
{
if (!$document) {
$document = new \DOMDocument();
}
$element = $document->createElement('header');
Element::appendChild($element, $this->cover, $document);
Element::appendChild($element, $this->title, $document);
Element::appendChild($element, $this->subtitle, $document);
Element::appendChild($element, $this->published, $document);
Element::appendChild($element, $this->modified, $document);
if ($this->authors) {
foreach ($this->authors as $author) {
Element::appendChild($element, $author, $document);
}
}
if ($this->kicker && $this->kicker->isValid()) {
$kicker_element = $this->kicker->toDOMElement($document);
$kicker_element->setAttribute('class', 'op-kicker');
$element->appendChild($kicker_element);
}
if (count($this->ads) === 1) {
$this->ads[0]->disableDefaultForReuse();
Element::appendChild($element, $this->ads[0], $document);
} elseif (count($this->ads) >= 2) {
$ads_container = $document->createElement('section');
$ads_container->setAttribute('class', 'op-ad-template');
$default_is_set = false;
$has_valid_ad = false;
foreach ($this->ads as $ad) {
if ($default_is_set) {
$ad->disableDefaultForReuse();
}
if ($ad->getIsDefaultForReuse()) {
$default_is_set = true;
}
if ($ad->isValid()) {
$ads_container->appendChild($ad->toDOMElement($document));
$has_valid_ad = true;
}
}
if ($has_valid_ad) {
$element->appendChild($ads_container);
}
}
Element::appendChild($element, $this->sponsor, $document);
return $element;
}
/**
* Overrides the Element::isValid().
*
* @see Element::isValid().
* @return true for valid tag, false otherwise.
*/
public function isValid()
{
$has_ad = count($this->ads) > 0;
$has_valid_ad = false;
if ($has_ad) {
foreach ($this->ads as $ad) {
if ($ad->isValid()) {
$has_valid_ad = true;
break;
}
}
}
return
($this->title && $this->title->isValid()) ||
$has_valid_ad;
}
/**
* Implements the ChildrenContainer::getContainerChildren().
*
* @see ChildrenContainer::getContainerChildren().
* @return array of Elements contained by Header.
*/
public function getContainerChildren()
{
$children = array();
if ($this->cover) {
$children[] = $this->cover;
}
if ($this->title) {
$children[] = $this->title;
}
if ($this->subtitle) {
$children[] = $this->subtitle;
}
if ($this->published) {
$children[] = $this->published;
}
if ($this->modified) {
$children[] = $this->modified;
}
if ($this->authors) {
foreach ($this->authors as $author) {
$children[] = $author;
}
}
if ($this->kicker) {
$children[] = $this->kicker;
}
if (count($this->ads) > 0) {
foreach ($this->ads as $ad) {
$children[] = $ad;
}
}
if ($this->sponsor) {
$children[] = $this->sponsor;
}
return $children;
}
}