Added Microdata class and test
Microdata class allows creating MicrodataParser from different sources.
This commit is contained in:
parent
7c3700e8cf
commit
daa8efa0fa
29
src/Microdata.php
Normal file
29
src/Microdata.php
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace YusufKandemir\MicrodataParser;
|
||||||
|
|
||||||
|
abstract class Microdata
|
||||||
|
{
|
||||||
|
public static function fromHTML($html, $documentURI = '')
|
||||||
|
{
|
||||||
|
$dom = new \DOMDocument;
|
||||||
|
$dom->loadHTML($html, LIBXML_NOERROR);
|
||||||
|
$dom->documentURI = $documentURI;
|
||||||
|
|
||||||
|
return new MicrodataParser($dom);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromHTMLFile($filename, $documentURI = '')
|
||||||
|
{
|
||||||
|
$dom = new \DOMDocument;
|
||||||
|
$dom->loadHTMLFile($filename);
|
||||||
|
$dom->documentURI = $documentURI;
|
||||||
|
|
||||||
|
return new MicrodataParser($dom);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromDOMDocument(\DOMDocument $dom)
|
||||||
|
{
|
||||||
|
return new MicrodataParser($dom);
|
||||||
|
}
|
||||||
|
}
|
36
tests/MicrodataTest.php
Normal file
36
tests/MicrodataTest.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace YusufKandemir\MicrodataParser\Tests;
|
||||||
|
|
||||||
|
use YusufKandemir\MicrodataParser\Microdata;
|
||||||
|
use YusufKandemir\MicrodataParser\MicrodataParser;
|
||||||
|
|
||||||
|
class MicrodataTest extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
protected $htmlFileName = __DIR__ . '/data/W3C/source.html';
|
||||||
|
|
||||||
|
public function testFromHTML()
|
||||||
|
{
|
||||||
|
$html = file_get_contents($this->htmlFileName);
|
||||||
|
$microdata = Microdata::fromHTML($html);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(MicrodataParser::class, $microdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromHTMLFile()
|
||||||
|
{
|
||||||
|
$microdata = Microdata::fromHTMLFile($this->htmlFileName);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(MicrodataParser::class, $microdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFromDOMDocument()
|
||||||
|
{
|
||||||
|
$dom = new \DOMDocument;
|
||||||
|
$dom->loadHTMLFile($this->htmlFileName);
|
||||||
|
|
||||||
|
$microdata = Microdata::fromDOMDocument($dom);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(MicrodataParser::class, $microdata);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user