-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathobjects.php
710 lines (631 loc) · 15.1 KB
/
objects.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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
<?php
/**
* Open Graph protocol global types
*
* @link http://ogp.me/#types Open Graph protocol global types
* @package open-graph-protocol-tools
* @author Niall Kennedy <[email protected]>
* @version 1.3
* @copyright Public Domain
*/
if ( !class_exists('OpenGraphProtocol') )
require_once dirname(__FILE__) . '/open-graph-protocol.php';
abstract class OpenGraphProtocolObject {
const PREFIX ='';
const NS='';
/**
* Output the object as HTML <meta> elements
* @return string HTML meta element string
*/
public function toHTML() {
return rtrim( OpenGraphProtocol::buildHTML( get_object_vars($this), static::PREFIX ), PHP_EOL );
}
/**
* Convert a DateTime object to GMT and format as an ISO 8601 string.
* @param DateTime $date date to convert
* @return string ISO 8601 formatted datetime string
*/
public static function datetime_to_iso_8601( DateTime $date ) {
$date->setTimezone(new DateTimeZone('GMT'));
return $date->format('c');
}
/**
* Test a URL for validity.
*
* @uses OpenGraphProtocol::is_valid_url if OpenGraphProtocol::VERIFY_URLS is true
* @param string $url absolute URL addressable from the public web
* @return bool true if URL is non-empty string. if VERIFY_URLS set then URL must also properly respond to a HTTP request.
*/
public static function is_valid_url( $url ) {
if ( is_string($url) && !empty($url) ) {
if (OpenGraphProtocol::VERIFY_URLS) {
$url = OpenGraphProtocol::is_valid_url( $url, array( 'text/html', 'application/xhtml+xml' ) );
if (!empty($url))
return true;
} else {
return true;
}
}
return false;
}
}
class OpenGraphProtocolArticle extends OpenGraphProtocolObject {
/**
* Property prefix
* @var string
*/
const PREFIX = 'article';
/**
* prefix namespace
* @var string
*/
const NS = 'http://ogp.me/ns/article#';
/**
* When the article was first published.
* ISO 8601 formatted string.
* @var string
*/
protected $published_time;
/**
* When the article was last changed
* ISO 8601 formatted string.
* @var string
*/
protected $modified_time;
/**
* When the article is considered out-of-date
* ISO 8601 formatted string.
* @var string
*/
protected $expiration_time;
/**
* Writers of the article.
* Array of author URIs
* @var array
*/
protected $author;
/**
* High-level section or category
* @var string
*/
protected $section;
/**
* Content tag
* Array of tag strings
* @var array
*/
protected $tag;
/**
* Initialize arrays
*/
public function __construct() {
$this->author = array();
$this->tag = array();
}
/**
* When the article was first published
* @return string ISO 8601 formatted publication date and optional time
*/
public function getPublishedTime() {
return $this->published_time;
}
/**
* Set when the article was first published
* @param DateTime|string $pubdate ISO 8601 formatted datetime string or DateTime object for conversion
*/
public function setPublishedTime( $pubdate ) {
if ( $pubdate instanceof DateTime )
$this->published_time = static::datetime_to_iso_8601($pubdate);
else if ( is_string($pubdate) && strlen($pubdate) >= 10 ) // at least YYYY-MM-DD
$this->published_time = $pubdate;
return $this;
}
/**
* When article was last changed
* @return string ISO 8601 formatted modified date and optional time
*/
public function getModifiedTime() {
return $this->modified_time;
}
/**
* Set when the article was last changed
* @param DateTime|string $updated ISO 8601 formatted datetime string or DateTime object for conversion
*/
public function setModifiedTime( $updated ) {
if ( $updated instanceof DateTime )
$this->modified_time = static::datetime_to_iso_8601($updated);
else if ( is_string($updated) && strlen($updated) >= 10 ) // at least YYYY-MM-DD
$this->modified_time = $updated;
return $this;
}
/**
* Time the article content expires
* @return string ISO 8601 formatted expiration date and optional time
*/
public function getExpirationTime() {
return $this->expiration_time;
}
/**
* Set when the article content expires
* @param DateTime|string $expires ISO formatted datetime string or DateTime object for conversion
*/
public function setExpirationTime( $expires ) {
if ( $expires instanceof DateTime )
$this->expiration_time = static::datetime_to_iso_8601($expires);
else if ( is_string($expires) && strlen($expires) >= 10 )
$this->expiration_time = $expires;
return $this;
}
/**
* Article author URIs
* @return array Article author URIs
*/
public function getAuthors() {
return $this->author;
}
/**
* Add an author URI
* @param string $author_uri Author URI
*/
public function addAuthor( $author_uri ) {
if ( static::is_valid_url($author_uri) && !in_array($author_uri, $this->author))
$this->author[] = $author_uri;
return $this;
}
/**
* High-level section name
*/
public function getSection() {
return $this->section;
}
/**
* Set the top-level content section
* @param string $section
*/
public function setSection( $section ) {
if ( is_string($section) && !empty($section) )
$this->section = $section;
return $this;
}
/**
* Content tags
* @return array content tags
*/
public function getTags() {
return $this->tag;
}
/**
* Add a content tag
* @param string $tag content tag
*/
public function addTag( $tag ) {
if ( is_string($tag) && !empty($tag) )
$this->tag[] = $tag;
return $this;
}
}
class OpenGraphProtocolProfile extends OpenGraphProtocolObject {
/**
* Property prefix
* @var string
*/
const PREFIX = 'profile';
/**
* prefix namespace
* @var string
*/
const NS = 'http://ogp.me/ns/profile#';
/**
* A person's given name
* @var string
*/
protected $first_name;
/**
* A person's last name
* @var string
*/
protected $last_name;
/**
* The profile's unique username
* @var string
*/
protected $username;
/**
* Gender: male or female
*/
protected $gender;
/**
* Get the person's given name
* @return string given name
*/
public function getFirstName() {
return $this->first_name;
}
/**
* Set the person's given name
* @param string $first_name given name
*/
public function setFirstName( $first_name ) {
if ( is_string($first_name) && !empty($first_name) )
$this->first_name = $first_name;
return $this;
}
/**
* The person's family name
* @return string famil name
*/
public function getLastName() {
return $this->last_name;
}
/**
* Set the person's family name
* @param string $last_name family name
*/
public function setLastName( $last_name ) {
if ( is_string($last_name) && !empty($last_name) )
$this->last_name = $last_name;
return $this;
}
/**
* Person's username on your site
* @return string account username
*/
public function getUsername() {
return $this->username;
}
/**
* Set the account username
* @param string $username username
*/
public function setUsername( $username ) {
if ( is_string($username) && !empty($username) )
$this->username = $username;
return $this;
}
/**
* The person's gender. male|female
* @return string male|female
*/
public function getGender() {
return $this->gender;
}
/**
* Set the person's gender
* @param string $gender male|female
*/
public function setGender( $gender ) {
if ( is_string($gender) && ( $gender === 'male' || $gender === 'female' ) )
$this->gender = $gender;
return $this;
}
}
class OpenGraphProtocolBook extends OpenGraphProtocolObject {
/**
* Property prefix
* @var string
*/
const PREFIX = 'book';
/**
* prefix namespace
* @var string
*/
const NS = 'http://ogp.me/ns/book#';
/**
* Book authors as an array of URIs.
* The target URI is expected to have an Open Graph protocol type of 'profile'
* @var array
*/
protected $author;
/**
* International Standard Book Number. ISBN-10 and ISBN-13 accepted
* @link http://en.wikipedia.org/wiki/International_Standard_Book_Number ISBN
* @var string
*/
protected $isbn;
/**
* The date the book was released, or planned release if in future.
* Stored as an ISO 8601 date string normalized to UTC for consistency
* @var string
*/
protected $release_date;
/**
* Tag words describing book content and subjects
* @var array
*/
protected $tag;
public function __construct() {
$this->author = array();
$this->tag = array();
}
/**
* Book author URIs
* @return array author URIs
*/
public function getAuthors() {
return $this->author;
}
/**
* Add an author URI.
*
* @param string $author_uri
*/
public function addAuthor( $author_uri ) {
if ( static::is_valid_url($author_uri) && !in_array($author_uri, $this->author))
$this->author[] = $author_uri;
return $this;
}
/**
* International Standard Book Number
*
* @return string
*/
public function getISBN() {
return $this->isbn;
}
/**
* Set an International Standard Book Number
*
* @param string $isbn
*/
public function setISBN( $isbn ) {
if ( is_string( $isbn ) ) {
$isbn = trim( str_replace('-', '', $isbn) );
if ( strlen($isbn) === 10 && is_numeric( substr($isbn, 0 , 9) ) ) { // published before 2007
$verifysum = 0;
$chars = str_split( $isbn );
for( $i=0; $i<9; $i++ ) {
$verifysum += ( (int) $chars[$i] ) * (10 - $i);
}
$check_digit = 11 - ($verifysum % 11);
if ( $check_digit == $chars[9] || ($chars[9] == 'X' && $check_digit == 10) )
$this->isbn = $isbn;
} elseif ( strlen($isbn) === 13 && is_numeric( substr($isbn, 0, 12 ) ) ) {
$verifysum = 0;
$chars = str_split( $isbn );
for( $i=0; $i<12; $i++ ) {
$verifysum += ( (int) $chars[$i] ) * ( ( ($i%2) ===0 ) ? 1:3 );
}
$check_digit = 10 - ( $verifysum%10 );
if ( $check_digit == $chars[12] )
$this->isbn = $isbn;
}
}
return $this;
}
/**
* Book release date
*
* @return string release date in ISO 8601 format
*/
public function getReleaseDate() {
return $this->release_date;
}
/**
* Set the book release date
*
* @param DateTime|string $release_date release date as DateTime or as an ISO 8601 formatted string
*/
public function setReleaseDate( $release_date ) {
if ( $release_date instanceof DateTime )
$this->release_date = static::datetime_to_iso_8601($release_date);
else if ( is_string($release_date) && strlen($release_date) >= 10 ) // at least YYYY-MM-DD
$this->release_date = $release_date;
return $this;
}
/**
* Book subject tags
*
* @return array Topic tags
*/
public function getTags() {
return $this->tag;
}
/**
* Add a book topic tag
*
* @param string $tag topic tag
*/
public function addTag( $tag ) {
if ( is_string($tag) && !empty($tag) && !in_array($tag, $this->tag) )
$this->tag[] = $tag;
return $this;
}
}
/**
* Video movie, TV show, and other all share the same properies.
* Video episode extends this class to associate with a TV show
*
* @link http://ogp.me/#type_video Open Graph protocol video object
*/
class OpenGraphProtocolVideoObject extends OpenGraphProtocolObject {
/**
* Property prefix
* @var string
*/
const PREFIX = 'video';
/**
* prefix namespace
* @var string
*/
const NS = 'http://ogp.me/ns/video#';
/**
* Array of actor URLs
* @var array
*/
protected $actor;
/**
* Array of director URLs
* @var array
*/
protected $director;
/**
* Array of writer URIs
* @var array
*/
protected $writer;
/**
* Video duration in whole seconds
* @var int
*/
protected $duration;
/**
* The date the movie was first released. ISO 8601 formatted string
* @var string
*/
protected $release_date;
/**
* Tag words associated with the movie
* @var array
*/
protected $tag;
public function __construct() {
$this->actor = array();
$this->director = array();
$this->writer = array();
$this->tag = array();
}
/**
* Get an array of actor URLs
*
* @return array actor URLs
*/
public function getActors() {
return $this->actor;
}
/**
* Add an actor URL with an optional role association
*
* @param string $url Author URL of og:type profile
* @param string $role The role the given actor played in this video work.
*/
public function addActor( $url, $role='' ) {
if ( static::is_valid_url($url) && !in_array($url, $this->actor) ) {
if ( !empty($role) && is_string($role) )
$this->actor[] = array( $url, 'role' => $role );
else
$this->actor[] = $url;
}
return $this;
}
/**
* An array of director URLs
*
* @return array director URLs
*/
public function getDirectors() {
return $this->director;
}
/**
* Add a director profile URL
*
* @param string $url director profile URL
*/
public function addDirector( $url ) {
if ( static::is_valid_url($url) && !in_array($url, $this->director) )
$this->director[] = $url;
return $this;
}
/**
* An array of writer URLs
*
* @return array writer URLs
*/
public function getWriters() {
return $this->writer;
}
/**
* Add a writer profile URL
*
* @param string $url writer profile URL
*
* @return OpenGraphProtocolVideoObject
*/
public function addWriter( $url ) {
if ( static::is_valid_url($url) && !in_array($url, $this->writer) )
$this->writer[] = $url;
return $this;
}
/**
* Duration of the video in whole seconds
*
* @return int duration in whole seconds
*/
public function getDuration() {
return $this->duration;
}
/**
* Set the video duration in whole seconds
*
* @param int $duration video duration in whole seconds
*/
public function setDuration( $duration ) {
if ( is_int($duration) && $duration > 0 )
$this->duration = $duration;
return $this;
}
/**
* The release date as an ISO 8601 formatted string
*
* @return string release date as an ISO 8601 formatted string
*/
public function getReleaseDate() {
return $this->release_date;
}
/**
* Set the date this video was first released
*
* @param DateTime|string $release_date date video was first released
*/
public function setReleaseDate( $release_date ) {
if ( $release_date instanceof DateTime )
$this->release_date = static::datetime_to_iso_8601($release_date);
else if ( is_string($release_date) && strlen($release_date) >= 10 ) // at least YYYY-MM-DD
$this->release_date = $release_date;
return $this;
}
/**
* An array of tag words associated with this video
*
* @return array tags
*/
public function getTags() {
return $this->tag;
}
/**
* Add a tag word or topic related to this video
*
* @param string $tag tag word or topic
*/
public function addTag( $tag ) {
if ( is_string($tag) && !in_array($tag, $this->tag) )
$this->tag[] = $tag;
return $this;
}
}
/**
* @link http://ogp.me/#type_video.episode Video episode
*/
class OpenGraphProtocolVideoEpisode extends OpenGraphProtocolVideoObject {
/**
* URL of a video.tv_show which this episode belongs to
* @var string
*/
protected $series;
/**
* URL of a video.tv_show which this episode belongs to
*/
public function getSeries() {
return $this->series;
}
/**
* Set the URL of a video.tv_show which this episode belongs to
*
* @param string $url URL of a video.tv_show
*/
public function setSeries( $url ) {
if ( static::is_valid_url($url) )
$this->series = $url;
return $this;
}
}
?>