-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from grgk/Parser-optimization
added option of injecting a custom parser,
- Loading branch information
Showing
15 changed files
with
356 additions
and
203 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* File analyze example | ||
* | ||
* Executes file seo analyze on local html file using custom parser. | ||
*/ | ||
|
||
require_once(__DIR__ . '/../vendor/autoload.php'); | ||
|
||
use SeoAnalyzer\Analyzer; | ||
use SeoAnalyzer\Factor; | ||
use SeoAnalyzer\HttpClient\Exception\HttpException; | ||
use SeoAnalyzer\Page; | ||
use SeoAnalyzer\Parser\ExampleCustomParser; | ||
|
||
try { | ||
$page = new Page('https://www.msn.com/pl-pl'); | ||
$parser = new ExampleCustomParser(); | ||
$page->setParser($parser); | ||
$analyzer = new Analyzer($page); | ||
$analyzer->metrics = $page->setMetrics([Factor::ALTS]); | ||
$results = $analyzer->analyze(); | ||
} catch (HttpException $e) { | ||
echo "Error loading page: " . $e->getMessage(); | ||
} catch (ReflectionException $e) { | ||
echo "Error loading metric file: " . $e->getMessage(); | ||
} | ||
|
||
print_r($results); |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,63 @@ | ||
<?php | ||
|
||
namespace SeoAnalyzer\Parser; | ||
|
||
use DOMDocument; | ||
use DOMElement; | ||
use DOMNodeList; | ||
|
||
abstract class AbstractParser implements ParserInterface | ||
{ | ||
/** | ||
* @var DOMDocument Dom representation of HTML document | ||
*/ | ||
protected $dom; | ||
|
||
/** | ||
* @param string $html Html document to parse. | ||
*/ | ||
public function __construct(string $html = null) | ||
{ | ||
$this->dom = new DOMDocument(); | ||
if (!empty($html)) { | ||
$this->setContent($html); | ||
} | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setContent($html): void | ||
{ | ||
$internalErrors = libxml_use_internal_errors(true); | ||
$this->dom->loadHTML($html, LIBXML_NOWARNING); | ||
libxml_use_internal_errors($internalErrors); | ||
} | ||
|
||
/** | ||
* Removes specified tags with it's content from DOM. | ||
* | ||
* @param string $tag | ||
*/ | ||
protected function removeTags(string $tag) | ||
{ | ||
$tagsToRemove = []; | ||
foreach ($this->getDomElements($tag) as $tag) { | ||
$tagsToRemove[] = $tag; | ||
} | ||
foreach ($tagsToRemove as $item) { | ||
$item->parentNode->removeChild($item); | ||
} | ||
} | ||
|
||
/** | ||
* Returns DOM elements by tag name. | ||
* | ||
* @param string $name | ||
* @return DOMNodeList|DOMElement[] | ||
*/ | ||
protected function getDomElements(string $name): DOMNodeList | ||
{ | ||
return $this->dom->getElementsByTagName($name); | ||
} | ||
} |
Oops, something went wrong.