Skip to content

Commit 279c1ae

Browse files
committed
added option of injecting a custom parser,
added example of injecting custom parser, some cleanup, refactoring and optimization
1 parent dfebfec commit 279c1ae

15 files changed

+366
-174
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"psr/simple-cache": "^1.0"
1515
},
1616
"require-dev": {
17-
"phpunit/phpunit": "^7"
17+
"phpunit/phpunit": "^7",
18+
"ext-json": "*"
1819
},
1920
"autoload": {
2021
"psr-4": {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* File analyze example
4+
*
5+
* Executes file seo analyze on local html file using custom parser.
6+
*/
7+
8+
require_once(__DIR__ . '/../vendor/autoload.php');
9+
10+
use SeoAnalyzer\Analyzer;
11+
use SeoAnalyzer\Factor;
12+
use SeoAnalyzer\HttpClient\Exception\HttpException;
13+
use SeoAnalyzer\Page;
14+
use SeoAnalyzer\Parser\ExampleCustomParser;
15+
16+
try {
17+
$page = new Page('https://www.msn.com/pl-pl');
18+
$parser = new ExampleCustomParser();
19+
$page->setParser($parser);
20+
$analyzer = new Analyzer($page);
21+
$analyzer->metrics = $page->setMetrics([Factor::ALTS]);
22+
$results = $analyzer->analyze();
23+
} catch (HttpException $e) {
24+
echo "Error loading page: " . $e->getMessage();
25+
} catch (ReflectionException $e) {
26+
echo "Error loading metric file: " . $e->getMessage();
27+
}
28+
29+
print_r($results);

src/Metric/MetricFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace SeoAnalyzer\Metric;
44

5+
use ReflectionException;
6+
57
class MetricFactory
68
{
79
/**
810
* @param string $key
911
* @param null $inputData
1012
* @return mixed
13+
* @throws ReflectionException
1114
*/
1215
public static function get(string $key, $inputData = null)
1316
{
@@ -24,6 +27,6 @@ public static function get(string $key, $inputData = null)
2427
}
2528
return $metric;
2629
}
27-
throw new \ReflectionException('Metric class ' . $class .' not exists');
30+
throw new ReflectionException('Metric class ' . $class .' not exists');
2831
}
2932
}

src/Metric/Page/SizeMetric.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Page.php

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use SeoAnalyzer\HttpClient\Exception\HttpException;
88
use SeoAnalyzer\Metric\MetricFactory;
99
use ReflectionException;
10+
use SeoAnalyzer\Parser\Parser;
11+
use SeoAnalyzer\Parser\ParserInterface;
1012

1113
class Page
1214
{
@@ -52,19 +54,33 @@ class Page
5254
*/
5355
public $client;
5456

57+
/**
58+
* @var ParserInterface
59+
*/
60+
public $parser;
61+
5562
/**
5663
* Page constructor.
5764
*
5865
* @param string|null $url
5966
* @param string|null $locale
6067
* @param ClientInterface|null $client
68+
* @param ParserInterface|null $parser
6169
*/
62-
public function __construct(string $url = null, string $locale = null, ClientInterface $client = null)
63-
{
70+
public function __construct(
71+
string $url = null,
72+
string $locale = null,
73+
ClientInterface $client = null,
74+
ParserInterface $parser = null
75+
) {
6476
$this->client = $client;
6577
if (empty($client)) {
6678
$this->client = new Client();
6779
}
80+
$this->parser = $parser;
81+
if (empty($parser)) {
82+
$this->parser = new Parser();
83+
}
6884
if (!empty($url)) {
6985
$this->url = $this->setUpUrl($url);
7086
$this->getContent();
@@ -74,6 +90,26 @@ public function __construct(string $url = null, string $locale = null, ClientInt
7490
}
7591
}
7692

93+
/**
94+
* Sets custom Http Client.
95+
*
96+
* @param ClientInterface $client
97+
*/
98+
public function setClient(ClientInterface $client): void
99+
{
100+
$this->client = $client;
101+
}
102+
103+
/**
104+
* Sets custom Html Parser.
105+
*
106+
* @param ParserInterface $parser
107+
*/
108+
public function setParser(ParserInterface $parser): void
109+
{
110+
$this->parser = $parser;
111+
}
112+
77113
/**
78114
* Verifies URL and sets up some basic metrics.
79115
*
@@ -180,21 +216,23 @@ protected function getHttpClientOptions()
180216
*/
181217
public function parse()
182218
{
183-
$parser = new Parser($this->content);
219+
if (empty($this->content)) {
220+
$this->getContent();
221+
}
222+
$this->parser->setContent($this->content);
184223
$this->setFactors([
185-
Factor::META_META => $parser->getMeta(),
186-
Factor::HEADERS => $parser->getHeaders(),
187-
Factor::META_TITLE => $parser->getTitle(),
188-
Factor::TEXT => $parser->getText(),
189-
Factor::ALTS => $parser->getAlts()
224+
Factor::META_META => $this->parser->getMeta(),
225+
Factor::HEADERS => $this->parser->getHeaders(),
226+
Factor::META_TITLE => $this->parser->getTitle(),
227+
Factor::TEXT => $this->parser->getText(),
228+
Factor::ALTS => $this->parser->getAlts()
190229
]);
191230
}
192231

193232
/**
194233
* Returns page metrics.
195234
*
196235
* @return array
197-
* @throws HttpException
198236
* @throws ReflectionException
199237
*/
200238
public function getMetrics(): array
@@ -208,7 +246,6 @@ public function getMetrics(): array
208246
* @param array $config
209247
* @return array
210248
* @throws ReflectionException
211-
* @throws HttpException
212249
*/
213250
public function setMetrics(array $config)
214251
{
@@ -218,9 +255,6 @@ public function setMetrics(array $config)
218255

219256
private function initializeFactors()
220257
{
221-
if (empty($this->content)) {
222-
$this->getContent();
223-
}
224258
if (empty($this->dom)) {
225259
$this->parse();
226260
}
@@ -335,7 +369,7 @@ public function getMetricsConfig()
335369
*
336370
* @param array $config Metrics config
337371
* @return array
338-
* @throws \ReflectionException
372+
* @throws ReflectionException
339373
*/
340374
public function setUpMetrics(array $config)
341375
{

src/Parser.php

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)