Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc54eff
commit e8980ef
Showing
5 changed files
with
155 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Fusonic\OpenGraph; | ||
|
||
use Fusonic\OpenGraph\Objects\ObjectBase; | ||
|
||
/** | ||
* Class for generating Open Graph tags from objects. | ||
*/ | ||
class Publisher | ||
{ | ||
const DOCTYPE_HTML5 = 1; | ||
const DOCTYPE_XHTML = 2; | ||
|
||
public $doctype = self::DOCTYPE_HTML5; | ||
|
||
public function __construct() | ||
{ | ||
} | ||
|
||
public function generateHtml(ObjectBase $object) | ||
{ | ||
$html = ""; | ||
$format = "<meta property=\"%s\" content=\"%s\"" . ($this->doctype == self::DOCTYPE_XHTML ? " />" : ">"); | ||
|
||
foreach ($object->getProperties() as $property) { | ||
if ($html !== "") { | ||
$html .= "\n"; | ||
} | ||
|
||
if ($property->value === null) { | ||
continue; | ||
} elseif ($property->value instanceof \DateTime) { | ||
$value = $property->value->format("c"); | ||
} elseif (is_object($property->value)) { | ||
throw new \UnexpectedValueException(sprintf("Cannot handle value of type '%0' for property '%1'.", get_class($property->value), $property->key)); | ||
} elseif ($property->value === true) { | ||
$value = "1"; | ||
} elseif ($property->value === false) { | ||
$value = "0"; | ||
} else { | ||
$value = (string)$property->value; | ||
} | ||
|
||
$html .= sprintf($format, $property->key, htmlspecialchars($value)); | ||
} | ||
|
||
return $html; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Fusonic\OpenGraph\Test; | ||
|
||
require_once(__DIR__ . DIRECTORY_SEPARATOR . "TestData" . DIRECTORY_SEPARATOR . "TestPublishObject.php"); | ||
|
||
use Fusonic\OpenGraph\Publisher; | ||
use Fusonic\OpenGraph\Test\TestData\TestPublishObject; | ||
|
||
class PublisherTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var Publisher | ||
*/ | ||
private $publisher; | ||
|
||
protected function setUp() | ||
{ | ||
$this->publisher = new Publisher(); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
public function testGenerateHtmlNull() | ||
{ | ||
$object = new TestPublishObject(null); | ||
|
||
$result = $this->publisher->generateHtml($object); | ||
|
||
$this->assertEquals("", $result); | ||
} | ||
|
||
public function testGenerateHtmlValuesProvider() | ||
{ | ||
return [ | ||
[ true, "1" ], | ||
[ false, "0" ], | ||
[ 1, "1" ], | ||
[ -1, "-1" ], | ||
[ 1.11111, "1.11111" ], | ||
[ -1.11111, "-1.11111" ], | ||
[ new \DateTime("2014-07-21T20:14:00+02:00"), "2014-07-21T20:14:00+02:00" ], | ||
[ "string", "string" ], | ||
[ "some \" quotes", "some " quotes" ], | ||
[ "some & ampersand", "some & ampersand" ], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider testGenerateHtmlValuesProvider | ||
*/ | ||
public function testGenerateHtmlValues($value, $expectedContent) | ||
{ | ||
$object = new TestPublishObject($value); | ||
|
||
$result = $this->publisher->generateHtml($object); | ||
|
||
$this->assertEquals('<meta property="' . TestPublishObject::KEY . '" content="' . $expectedContent . '">', $result); | ||
} | ||
|
||
/** | ||
* @expectedException \UnexpectedValueException | ||
*/ | ||
public function testGenerateHtmlUnsupportedObject() | ||
{ | ||
$object = new TestPublishObject(new \stdClass()); | ||
|
||
$this->publisher->generateHtml($object); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
tests/Fusonic/OpenGraph/Test/TestData/TestPublishObject.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Fusonic\OpenGraph\Test\TestData; | ||
|
||
use Fusonic\OpenGraph\Objects\ObjectBase; | ||
use Fusonic\OpenGraph\Property; | ||
|
||
class TestPublishObject extends ObjectBase | ||
{ | ||
const KEY = "og:title"; | ||
|
||
private $value; | ||
|
||
public function __construct($value) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->value = $value; | ||
} | ||
|
||
public function getProperties() | ||
{ | ||
return [ | ||
new Property(self::KEY, $this->value), | ||
]; | ||
} | ||
} |