97 lines
3.1 KiB
PHP
97 lines
3.1 KiB
PHP
<?php
|
||
namespace SvedenParser\PriemParser;
|
||
|
||
use SvedenParser\Color;
|
||
use SvedenParser\Parser;
|
||
use SvedenParser\Printer;
|
||
|
||
final class PriemParser extends Parser
|
||
{
|
||
private const TEMPLATE = '//tr[@itemprop="eduPriem"]//';
|
||
|
||
private const FIELDS = [
|
||
"eduCode" => "td",
|
||
"eduName" => "td",
|
||
"eduLevel" => "td",
|
||
"eduForm" => "td",
|
||
"numberBF" => "td",
|
||
"numberBR" => "td",
|
||
"numberBM" => "td",
|
||
"numberP" => "td",
|
||
"score" => "td"
|
||
];
|
||
public function getDataTable(): array
|
||
{
|
||
if (!$this->xpath) return [];
|
||
|
||
$data = $this->parse();
|
||
$records = [];
|
||
if (!$data) return [];
|
||
|
||
$equal = $data['eduName']->length;
|
||
foreach ($data as $field) {
|
||
if ($field->length == 0) {
|
||
return [];
|
||
}
|
||
if ($field->length != $equal) {
|
||
return [];
|
||
}
|
||
}
|
||
for ($i = 0; $i < $data['eduCode']->length; $i++) {
|
||
try {
|
||
$contingentRow = new PriemRow(
|
||
$data['eduCode']->item($i)->textContent,
|
||
$data['eduName']->item($i)->textContent,
|
||
$data['eduLevel']->item($i)->textContent,
|
||
$data['eduForm']->item($i)->textContent,
|
||
$data['score']->item($i)->textContent,
|
||
[
|
||
$data['numberBF']->item($i)->textContent,
|
||
$data['numberBR']->item($i)->textContent,
|
||
$data['numberBM']->item($i)->textContent,
|
||
$data['numberP']->item($i)->textContent,
|
||
],
|
||
);
|
||
$records[] = $contingentRow->getData();
|
||
} catch (\Exception $e) {
|
||
Printer::println($e->getMessage(), Color::RED);
|
||
}
|
||
|
||
}
|
||
return $records;
|
||
}
|
||
protected function parse(): array
|
||
{
|
||
$data = [];
|
||
foreach (self::FIELDS as $field => $tag) {
|
||
if (!is_array($tag)) {
|
||
$data[$field] = $this->xpath->query(
|
||
self::TEMPLATE . $tag . "[@itemprop=\"$field\"]"
|
||
);
|
||
} else {
|
||
// $th = $this->xpath->query(
|
||
// self::TEMPLATE . $tag[0] . "[@itemprop=\"$field\"]"
|
||
// );
|
||
// $td = $this->xpath->query(
|
||
// self::TEMPLATE . $tag[1] . "[@itemprop=\"$field\"]"
|
||
// );
|
||
// $data[$field] = $th->length > $td->length ? $th : $td;
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
public function getLink(): string
|
||
{
|
||
$needle = "Информация о результатах приёма";
|
||
$data = $this->dom->getElementsByTagName('a');
|
||
for ($i = 0; $i < $data->length; $i++) {
|
||
$haystack = $data->item($i)->textContent;
|
||
$isInformationOfContingent = strpos($haystack, $needle) !== false;
|
||
if ($isInformationOfContingent) {
|
||
return $data->item($i)->getAttribute('href');
|
||
}
|
||
}
|
||
return '';
|
||
}
|
||
}
|