Skip to content

Commit a947407

Browse files
committed
first commit
0 parents  commit a947407

10 files changed

+167
-0
lines changed

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/php.xml

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/test.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Analyzers/ContentAnalyzer.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class ContentAnalyzer
4+
{
5+
private $fetcher;
6+
private $parser;
7+
8+
public function __construct(ContentFetcherInterface $fetcher, HtmlParserInterface $parser)
9+
{
10+
$this->fetcher = $fetcher;
11+
$this->parser = $parser;
12+
}
13+
14+
public function analyze(string $url): array
15+
{
16+
$html = $this->fetcher->fetch($url);
17+
$tagCounts = $this->parser->parse($html);
18+
return $tagCounts;
19+
}
20+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
interface ContentFetcherInterface
4+
{
5+
public function fetch(string $url): string;
6+
}

app/Fetchers/CurlContentFetcher.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
class CurlContentFetcher implements ContentFetcherInterface
4+
{
5+
public function fetch(string $url): string
6+
{
7+
$ch = curl_init($url);
8+
curl_setopt_array($ch, [
9+
CURLOPT_RETURNTRANSFER => true,
10+
CURLOPT_FOLLOWLOCATION => true,
11+
CURLOPT_MAXREDIRS => 10,
12+
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.50 Safari/537.36 OPR/65.0.3467.16 (Edition beta)',
13+
]);
14+
15+
$data = curl_exec($ch);
16+
17+
if ($data === false) {
18+
$error = curl_error($ch);
19+
curl_close($ch);
20+
throw new Exception("cURL Error: $error");
21+
}
22+
23+
curl_close($ch);
24+
25+
return $data;
26+
}
27+
}

app/Parsers/HtmlParserInterface.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
interface HtmlParserInterface
4+
{
5+
public function parse(string $html): array;
6+
}

app/Parsers/SimpleHtmlParser.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
class SimpleHtmlParser implements HtmlParserInterface
4+
{
5+
public function parse(string $html): array
6+
{
7+
$tagCounts = [];
8+
$pos = 0;
9+
10+
while (($start = strpos($html, '<', $pos)) !== false) {
11+
$end = strpos($html, '>', $start);
12+
if ($end === false) {
13+
break;
14+
}
15+
16+
$tag = substr($html, $start + 1, $end - $start - 1);
17+
$tag = strtolower($tag);
18+
19+
if (strpos($tag, '/') === 0) {
20+
// Закрывающий тег, удалить его из подсчета
21+
$tag = substr($tag, 1);
22+
} elseif (strpos($tag, ' ') !== false) {
23+
// Пометить и удалить атрибуты
24+
$tag = substr($tag, 0, strpos($tag, ' '));
25+
}
26+
27+
if (isset($tagCounts[$tag])) {
28+
$tagCounts[$tag]++;
29+
} else {
30+
$tagCounts[$tag] = 1;
31+
}
32+
33+
$pos = $end + 1;
34+
}
35+
36+
return $tagCounts;
37+
}
38+
}

public/index.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
require_once __DIR__ . '/../app/Fetchers/ContentFetcherInterface.php';
3+
require_once __DIR__ . '/../app/Fetchers/CurlContentFetcher.php';
4+
require_once __DIR__ . '/../app/Parsers/HtmlParserInterface.php';
5+
require_once __DIR__ . '/../app/Parsers/SimpleHtmlParser.php';
6+
require_once __DIR__ . '/../app/Analyzers/ContentAnalyzer.php';
7+
8+
9+
$url = $_GET['url'] ?? '';
10+
11+
try {
12+
if (!empty($url)) {
13+
$fetcher = new CurlContentFetcher();
14+
$parser = new SimpleHtmlParser();
15+
$analyzer = new ContentAnalyzer($fetcher, $parser);
16+
17+
$tagCounts = $analyzer->analyze($url);
18+
19+
echo json_encode($tagCounts);
20+
} else {
21+
echo 'Please provide a valid URL';
22+
}
23+
} catch (Exception $e) {
24+
echo 'Error: ' . $e->getMessage();
25+
}
26+

0 commit comments

Comments
 (0)