Skip to content

Commit

Permalink
Create new Publisher class for SOC
Browse files Browse the repository at this point in the history
  • Loading branch information
mburtscher committed Jul 21, 2014
1 parent bc54eff commit e8980ef
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 16 deletions.
7 changes: 5 additions & 2 deletions examples/publish_website.php
Expand Up @@ -4,6 +4,7 @@
use Fusonic\OpenGraph\Elements\Image;
use Fusonic\OpenGraph\Elements\Video;
use Fusonic\OpenGraph\Objects\Website;
use Fusonic\OpenGraph\Publisher;

if (!$loader = @include __DIR__.'/../vendor/autoload.php') {
die('You must set up the project dependencies, run the following commands:'.PHP_EOL.
Expand Down Expand Up @@ -40,5 +41,7 @@
$audio->type = "audio/mp3";
$website->audios[] = $audio;

// Now render all tags
echo $website->getHtml();
// Create Publisher object and echo HTML code
$publisher = new Publisher();
$publisher->doctype = Publisher::DOCTYPE_XHTML;
echo $publisher->generateHtml($website);
17 changes: 3 additions & 14 deletions src/Fusonic/OpenGraph/Objects/ObjectBase.php
Expand Up @@ -266,7 +266,7 @@ public function getLargestImage()
{
return Linq::from($this->images)
->orderByDescending(
function (Elements\Image $image) {
function (Image $image) {
return $image->width * $image->height;
}
)
Expand All @@ -285,12 +285,12 @@ public function getImage($minWidth, $minHeight)
{
return Linq::from($this->images)
->where(
function (Elements\Image $image) use ($minWidth, $minHeight) {
function (Image $image) use ($minWidth, $minHeight) {
return $image->width >= $minWidth && $image->height >= $minHeight;
}
)
->orderBy(
function (Elements\Image $image) {
function (Image $image) {
return $image->width * $image->height;
}
)
Expand Down Expand Up @@ -360,15 +360,4 @@ public function getProperties()

return $properties;
}

public function getHtml()
{
$html = "";

foreach ($this->getProperties() as $property) {
$html .= sprintf("<meta property=\"%s\" content=\"%s\">\n", $property->key, htmlspecialchars($property->value));
}

return $html;
}
}
50 changes: 50 additions & 0 deletions src/Fusonic/OpenGraph/Publisher.php
@@ -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;
}
}
70 changes: 70 additions & 0 deletions tests/Fusonic/OpenGraph/Test/PublisherTest.php
@@ -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 &quot; quotes" ],
[ "some & ampersand", "some &amp; 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 tests/Fusonic/OpenGraph/Test/TestData/TestPublishObject.php
@@ -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),
];
}
}

0 comments on commit e8980ef

Please sign in to comment.