Convert parameter to state variable

This will provide a better syntax and also fix long lines warning.
This commit is contained in:
Yusuf Kandemir 2018-12-27 10:10:35 +03:00
parent c38fbeca48
commit c3746713f0
2 changed files with 36 additions and 14 deletions

View File

@ -4,24 +4,34 @@ namespace YusufKandemir\MicrodataParser;
class MicrodataParser class MicrodataParser
{ {
/** @var callable|null */
protected $absoluteUriHandler;
/**
* @param callable|null $absoluteUriHandler
*
* @see MicrodataElementParser::$absoluteUriHandler
*/
public function __construct(callable $absoluteUriHandler = null)
{
$this->absoluteUriHandler = $absoluteUriHandler;
}
/** /**
* Parses HTML string, extracts microdata from it * Parses HTML string, extracts microdata from it
* *
* @param string $html HTML string to be parsed * @param string $html HTML string to be parsed
* @param string $documentURI DocumentURI to be used in absolutizing URIs * @param string $documentURI DocumentURI to be used in absolutizing URIs
* @param callable|null $absoluteUriHandler
*
* @see MicrodataElementParser::$absoluteUriHandler
* *
* @return \stdClass * @return \stdClass
*/ */
public function parseHTML(string $html, string $documentURI = '', callable $absoluteUriHandler = null) : \stdClass public function parseHTML(string $html, string $documentURI = '') : \stdClass
{ {
$dom = new \DOMDocument; $dom = new \DOMDocument;
$dom->loadHTML($html, \LIBXML_NOERROR); $dom->loadHTML($html, \LIBXML_NOERROR);
$dom->documentURI = $documentURI; $dom->documentURI = $documentURI;
return $this->parse($dom, $absoluteUriHandler); return $this->parse($dom);
} }
/** /**
@ -29,19 +39,16 @@ class MicrodataParser
* *
* @param string $path Path to the file to be parsed * @param string $path Path to the file to be parsed
* @param string $documentURI DocumentURI to be used in absolutizing URIs * @param string $documentURI DocumentURI to be used in absolutizing URIs
* @param callable|null $absoluteUriHandler
*
* @see MicrodataElementParser::$absoluteUriHandler
* *
* @return \stdClass * @return \stdClass
*/ */
public function parseHTMLFile(string $path, string $documentURI = '', callable $absoluteUriHandler = null) : \stdClass public function parseHTMLFile(string $path, string $documentURI = '') : \stdClass
{ {
$dom = new \DOMDocument; $dom = new \DOMDocument;
$dom->loadHTMLFile($path, \LIBXML_NOERROR); $dom->loadHTMLFile($path, \LIBXML_NOERROR);
$dom->documentURI = $documentURI; $dom->documentURI = $documentURI;
return $this->parse($dom, $absoluteUriHandler); return $this->parse($dom);
} }
/** /**
@ -65,17 +72,30 @@ class MicrodataParser
/** /**
* @param \DOMDocument $dom * @param \DOMDocument $dom
* @param callable|null $absoluteUriHandler
* *
* @see MicrodataElementParser::$absoluteUriHandler * @see MicrodataElementParser::$absoluteUriHandler
* *
* @return \stdClass * @return \stdClass
*/ */
protected function parse(\DOMDocument $dom, callable $absoluteUriHandler = null) : \stdClass protected function parse(\DOMDocument $dom) : \stdClass
{ {
$elementParser = new MicrodataElementParser($absoluteUriHandler); $elementParser = new MicrodataElementParser($this->absoluteUriHandler);
$documentParser = new MicrodataDocumentParser($dom, $elementParser); $documentParser = new MicrodataDocumentParser($dom, $elementParser);
return $documentParser->parse(); return $documentParser->parse();
} }
/**
* @param callable|null $absoluteUriHandler
*
* @see MicrodataElementParser::$absoluteUriHandler
*
* @return MicrodataParser
*/
public function setAbsoluteUriHandler(callable $absoluteUriHandler = null) : self
{
$this->absoluteUriHandler = $absoluteUriHandler;
return $this;
}
} }

View File

@ -68,7 +68,9 @@ class MicrodataParserTest extends DataDrivenTestCase
return $baseUri . $value; return $baseUri . $value;
}; };
$resultAfter = $parser->parseHTML($data['source'], $data['uri'], $absoluteUriHandler); $resultAfter = $parser
->setAbsoluteUriHandler($absoluteUriHandler)
->parseHTML($data['source'], $data['uri']);
$resultAfterUri = $resultAfter->items[0]->properties->work[0]; $resultAfterUri = $resultAfter->items[0]->properties->work[0];
$this->assertContains($baseUri, $resultAfterUri); $this->assertContains($baseUri, $resultAfterUri);