Разбил на классы
This commit is contained in:
35
app/app.php
Normal file
35
app/app.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace App;
|
||||
|
||||
use App\Library\SvedenParser;
|
||||
use App\Library\Database;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
$dbconfig = [
|
||||
"host" => "10.90.1.201",
|
||||
"database" => "opendata",
|
||||
"user" => "niimko_user",
|
||||
"password" => "MOhA17FeboXE"
|
||||
];
|
||||
|
||||
$client = new Client();
|
||||
$response = $client->get('https://marsu.ru/sveden/education/eduChislen.php');
|
||||
$html = $response->getBody()->getContents();
|
||||
|
||||
$parser = new SvedenParser($html, '//tr[@itemprop="eduChislen"]//');
|
||||
$data = $parser->getDataTable();
|
||||
|
||||
$db = new Database(
|
||||
"mysql:host={$dbconfig['host']};dbname={$dbconfig['database']}",
|
||||
$dbconfig['user'],
|
||||
$dbconfig['password']
|
||||
);
|
||||
|
||||
// print_r($data);
|
||||
|
||||
// $db->insert('sveden_table_education', $data);
|
||||
$data = $db->select('sveden_education_contingent');
|
||||
|
||||
echo "<pre>";
|
||||
print_r($data);
|
||||
echo "</pre>";
|
64
app/library/Database.php
Normal file
64
app/library/Database.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class Database extends PDO
|
||||
{
|
||||
public function __construct($dsn, $username, $password)
|
||||
{
|
||||
try {
|
||||
parent::__construct(
|
||||
$dsn,
|
||||
$username,
|
||||
$password,
|
||||
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
|
||||
);
|
||||
echo "Подлючено успешно!\n";
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка подключения:". $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
echo "Подключение прервано!\n";
|
||||
}
|
||||
|
||||
public function insert(string $table, array $data) : void
|
||||
{
|
||||
$stmt = $this->prepare("
|
||||
INSERT INTO sveden_table_education
|
||||
(spec_code, spec_name, edu_forms, edu_level, contingent)
|
||||
VALUES
|
||||
(:spec_code, :spec_name, :edu_forms, :edu_level, :contingent)"
|
||||
);
|
||||
foreach ($data as $row) {
|
||||
try {
|
||||
$stmt->bindParam(':spec_code', $row['eduCode']);
|
||||
$stmt->bindParam(':spec_name', $row['eduName']);
|
||||
$stmt->bindParam(':edu_forms', $row['edoForms']);
|
||||
$stmt->bindParam(':edu_level', $row['eduLevel']);
|
||||
$stmt->bindParam(':contingent', $row['contingent']);
|
||||
|
||||
$stmt->execute();
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function select(string $table) : array
|
||||
{
|
||||
$stmt = $this->prepare("SELECT * FROM $table");
|
||||
try {
|
||||
$stmt->execute();
|
||||
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
} finally {
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
}
|
27
app/library/Size.php
Normal file
27
app/library/Size.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
// Численность обучающихся
|
||||
// по разным формам бюджета
|
||||
class Size
|
||||
{
|
||||
// Всего [обучающихся]
|
||||
private int $all;
|
||||
// Из низ иностранные граждане
|
||||
private int $foreigners;
|
||||
public function __construct() {}
|
||||
|
||||
public function update(
|
||||
int|string $all,
|
||||
int|string $foreigners
|
||||
): void {
|
||||
$this->all = (int)$all;
|
||||
$this->foreigners = (int)$foreigners;
|
||||
}
|
||||
|
||||
public function getData(): array{
|
||||
return [
|
||||
"Всего" => $this->all,
|
||||
"Из них численность обучающихся,
|
||||
являющихся иностранными гражданами" => $this->foreigners
|
||||
];
|
||||
}
|
||||
}
|
43
app/library/Specialization.php
Normal file
43
app/library/Specialization.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
// Специальность, направление подготовки
|
||||
class Specialization
|
||||
{
|
||||
// Код специальности
|
||||
private string $eduCode;
|
||||
// Название специальности, направления подготовки
|
||||
private string $eduName;
|
||||
//Уровень образования
|
||||
private string $eduLevel;
|
||||
//Формы обучения
|
||||
private string $eduForm;
|
||||
// Общая численность обучающихся
|
||||
private int $contingent;
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function update(
|
||||
string $eduCode,
|
||||
string $eduName,
|
||||
string $eduLevel,
|
||||
string $eduForm,
|
||||
int $contingent
|
||||
) : void {
|
||||
$this->eduCode = $eduCode;
|
||||
$this->eduName = $eduName;
|
||||
$this->eduLevel = $eduLevel;
|
||||
$this->eduForm = $eduForm;
|
||||
$this->contingent = $contingent;
|
||||
}
|
||||
|
||||
public function getData() : array
|
||||
{
|
||||
return [
|
||||
"eduCode" => $this->eduCode,
|
||||
"eduName" => $this->eduName,
|
||||
"eduLevel" => $this->eduLevel,
|
||||
"edoForms"=> $this->eduForm,
|
||||
"contingent" => $this->contingent
|
||||
];
|
||||
}
|
||||
}
|
62
app/library/SvedenParser.php
Normal file
62
app/library/SvedenParser.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
|
||||
use App\Library\Specialization;
|
||||
|
||||
class SvedenParser
|
||||
{
|
||||
private \DOMXPath $xpath;
|
||||
private string $template;
|
||||
private const FIELDS = [
|
||||
"eduCode" => "td",
|
||||
"eduName" => "td",
|
||||
"eduLevel" => "td",
|
||||
"eduForm" => "td",
|
||||
"numberBF" => "th",
|
||||
"numberBFF" => "th",
|
||||
"numberBR" => "th",
|
||||
"numberBRF" => "th",
|
||||
"numberBM" => "th",
|
||||
"numberBMF" => "th",
|
||||
"numberP" => "th",
|
||||
"numberPF" => "th",
|
||||
"numberAll" => "th"
|
||||
];
|
||||
|
||||
public function __construct(string $html, string $template)
|
||||
{
|
||||
libxml_use_internal_errors(true);
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadHTML($html);
|
||||
$this->xpath = new \DOMXPath($dom);
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
private function parse(): array
|
||||
{
|
||||
$data = array();
|
||||
foreach (self::FIELDS as $field => $tag) {
|
||||
$data[$field] = $this->xpath->query($this->template . $tag . "[@itemprop=\"$field\"]");
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getDataTable() : array
|
||||
{
|
||||
$data = $this->parse();
|
||||
$spec = new Specialization();
|
||||
$records = array();
|
||||
|
||||
for ($i = 0; $i < $data['numberAll']->length; $i++) {
|
||||
$spec->update(
|
||||
$data['eduCode']->item($i)->textContent,
|
||||
$data['eduName']->item($i)->textContent,
|
||||
$data['eduLevel']->item($i)->textContent,
|
||||
$data['eduForm']->item($i)->textContent,
|
||||
(int)$data['numberAll']->item($i)->textContent
|
||||
);
|
||||
$records[] = $spec->getData();
|
||||
}
|
||||
return $records;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user