scratch – Blame information for rev 87

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace Fusonic\OpenGraph;
4  
5 use Fusonic\OpenGraph\Objects\ObjectBase;
6  
7 /**
8 * Class for generating Open Graph tags from objects.
9 */
10 class Publisher
11 {
12 const DOCTYPE_HTML5 = 1;
13 const DOCTYPE_XHTML = 2;
14  
15 /**
16 * Defines the style in which HTML tags should be written. Use one of Publisher::DOCTYPE_HTML5 or
17 * Publisher::DOCTYPE_XHTML.
18 *
19 * @var int
20 */
21 public $doctype = self::DOCTYPE_HTML5;
22  
23 public function __construct()
24 {
25 }
26  
27 public function generateHtml(ObjectBase $object)
28 {
29 $html = "";
30 $format = "<meta property=\"%s\" content=\"%s\"" . ($this->doctype == self::DOCTYPE_XHTML ? " />" : ">");
31  
32 foreach ($object->getProperties() as $property) {
33 if ($html !== "") {
34 $html .= "\n";
35 }
36  
37 if ($property->value === null) {
38 continue;
39 } elseif ($property->value instanceof \DateTime) {
40 $value = $property->value->format("c");
41 } elseif (is_object($property->value)) {
42 throw new \UnexpectedValueException(
43 sprintf(
44 "Cannot handle value of type '%0' for property '%1'.",
45 get_class($property->value),
46 $property->key
47 )
48 );
49 } elseif ($property->value === true) {
50 $value = "1";
51 } elseif ($property->value === false) {
52 $value = "0";
53 } else {
54 $value = (string)$property->value;
55 }
56  
57 $html .= sprintf($format, $property->key, htmlspecialchars($value));
58 }
59  
60 return $html;
61 }
62 }