Skip to content

Commit

Permalink
Merge pull request #20 from dorantor/master
Browse files Browse the repository at this point in the history
Initial support for VPAID
  • Loading branch information
sokil authored Oct 29, 2018
2 parents 36a524d + ef3b821 commit c6af3c8
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Creative/InLine/Linear.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Sokil\Vast\Creative\AbstractLinearCreative;
use Sokil\Vast\Creative\InLine\Linear\MediaFile;
use Sokil\Vast\Creative\InLine\Linear\AdParameters;

class Linear extends AbstractLinearCreative
{
Expand All @@ -12,6 +13,11 @@ class Linear extends AbstractLinearCreative
*/
private $mediaFilesDomElement;

/**
* @var \DOMElement
*/
private $adParametersDomElement;

/**
* Set duration value
*
Expand Down Expand Up @@ -60,6 +66,25 @@ public function createMediaFile()
return new MediaFile($mediaFileDomElement);
}

/**
* @param array|string $params
*
* @return AdParameters
*/
public function createAdParameters($params)
{
$this->adParametersDomElement = $this->getDomElement()->getElementsByTagName('AdParameters')->item(0);
if (!$this->adParametersDomElement) {
$this->adParametersDomElement = $this->getDomElement()->ownerDocument->createElement('AdParameters');
$this->getDomElement()->firstChild->appendChild($this->adParametersDomElement);
}

// object
$adParams = new AdParameters($this->adParametersDomElement);

return $adParams->setParams($params);
}

/**
* Convert seconds to H:m:i
* Hours could be more than 24
Expand Down
31 changes: 31 additions & 0 deletions src/Creative/InLine/Linear/AdParameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Sokil\Vast\Creative\InLine\Linear;

class AdParameters
{
private $domElement;

public function __construct(\DomElement $domElement)
{
$this->domElement = $domElement;
}

public function setParams($params)
{
if (is_array($params)) {
$params = json_encode($params);
}
$cdata = $this->domElement->ownerDocument->createCDATASection($params);

// update CData
if ($this->domElement->hasChildNodes()) {
$this->domElement->replaceChild($cdata, $this->domElement->firstChild);
} // insert CData
else {
$this->domElement->appendChild($cdata);
}

return $this;
}
}
14 changes: 14 additions & 0 deletions src/Creative/InLine/Linear/MediaFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,24 @@ public function setUrl($url)

/**
* @param int $bitrate
* @return $this
*/
public function setBitrate($bitrate)
{
$this->domElement->setAttribute('bitrate', (int) $bitrate);
return $this;
}

/**
* Please note that this attribute is deprecated since VAST 4.1 along with VPAID
*
* @param string $value
* @return $this
*/
public function setApiFramework($value)
{
$this->domElement->setAttribute('apiFramework', (string) $value);

return $this;
}
}
48 changes: 46 additions & 2 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Sokil\Vast;

use Sokil\Vast\Document;

abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase
{
/**
Expand All @@ -13,9 +15,51 @@ protected function assertVastXmlEquals($expectedXml, Document $actualVastDocumen
$actualXml = str_replace(
array("\r", "\n"),
'',
(string)$actualVastDocument
(string) $actualVastDocument
);

$this->assertEquals($expectedXml, $actualXml);
self::assertEquals($expectedXml, $actualXml);
}

/**
* Minify XML
*
* @param string $xml
* @return string
*/
protected function minifyXml($xml)
{
$document = new \DOMDocument();
$document->preserveWhiteSpace = false;
$document->loadXML($xml);

return $document->saveXML();
}

/**
* Get minified xml string using file from data/ dir
*
* @param $filename
* @return string
*/
protected function minifiedXmlFromData($filename)
{
$xml = file_get_contents(__DIR__ . '/data/' . $filename);

return $this->minifyXml($xml);
}

/**
* Assert xml equals between file from data/ dir and given Document
*
* @param string $filename
* @param Document $document
*/
protected function assertFileVsDocument($filename, Document $document)
{
self::assertEquals(
$this->minifiedXmlFromData($filename),
(string) $document
);
}
}
31 changes: 31 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,35 @@ public function testFromFile()
$this->assertInstanceOf('Sokil\Vast\Document', $document::fromFile(__DIR__ . '/vast.xml'));
}

/**
* VPAID creative test
*/
public function testVpaidCreative()
{
$factory = new Factory();
$document = $factory->create('3.0');

$ad = $document
->createInLineAdSection()
->setId('test-vpaid')
->setAdSystem('Ad Server Name')
->setAdTitle('VPAIDPreRoll')
;
/** @var \Sokil\Vast\Creative\InLine\Linear $creative */
$creative = $ad->createLinearCreative();
$creative
->createAdParameters(array(
'list' => array(
array('param1' => 'value1', 'param2' => 'value2')
),
))
;
$creative->createMediaFile()
->setApiFramework('VPAID')
->setType('application/javascript')
->setUrl('https://example.com/vpaid.js?v434')
;

$this->assertFileVsDocument('vpaid.xml', $document);
}
}
23 changes: 23 additions & 0 deletions tests/data/vpaid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<VAST version="3.0">
<Ad id="test-vpaid">
<InLine>
<AdSystem><![CDATA[Ad Server Name]]></AdSystem>
<AdTitle><![CDATA[VPAIDPreRoll]]></AdTitle>
<Creatives>
<Creative>
<Linear>
<AdParameters>
<![CDATA[{"list":[{"param1":"value1","param2":"value2"}]}]]>
</AdParameters>
<MediaFiles>
<MediaFile apiFramework="VPAID" type="application/javascript">
<![CDATA[https://example.com/vpaid.js?v434]]>
</MediaFile>
</MediaFiles>
</Linear>
</Creative>
</Creatives>
</InLine>
</Ad>
</VAST>

0 comments on commit c6af3c8

Please sign in to comment.