Разбил на классы

This commit is contained in:
Alexander 2024-08-08 13:32:27 +03:00
parent 9dffa8c5d9
commit 1f96b88ac2
13 changed files with 205 additions and 158 deletions

35
app/app.php Normal file
View 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
View 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;
}
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace App\Library;
// Специальность, направление подготовки
class Specialization
{
@ -10,37 +11,32 @@ class Specialization
private string $eduLevel;
//Формы обучения
private string $eduForm;
// Численность обучающихся
// private array $size;
// Общая численность обучающихся
private int $contingent;
public function __construct() {}
public function update(
string $eduCode,
string $eduName,
string $eduLevel,
string $eduForm,
// array $size,
int $contingent
) : void {
$this->eduCode = $eduCode;
$this->eduName = $eduName;
$this->eduLevel = $eduLevel;
$this->eduForm = $eduForm;
// $this->size = $size;
$this->contingent = $contingent;
}
public function getData() : array
{
return [
"spec_code" => $this->eduCode,
"spec_name" => $this->eduName,
"edu_level" => $this->eduLevel,
"edu_forms"=> $this->eduForm,
// "size" => $this->size,
"eduCode" => $this->eduCode,
"eduName" => $this->eduName,
"eduLevel" => $this->eduLevel,
"edoForms"=> $this->eduForm,
"contingent" => $this->contingent
];
}

View 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;
}
}

View File

@ -1,5 +1,11 @@
{
"require": {
"guzzlehttp/guzzle": "^7.0"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"App\\Library\\": "app/library"
}
}
}

View File

@ -1,2 +1,4 @@
<?php
require 'parser.php';
require_once 'log/log.php';
require_once 'vendor/autoload.php';
require_once 'app/app.php';

4
log/log.php Normal file
View File

@ -0,0 +1,4 @@
<?php
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', 'log/php-error.log');

7
log/php-error.log Normal file
View File

@ -0,0 +1,7 @@
[08-Aug-2024 10:24:24 UTC] PHP Warning: Undefined variable $array in /home/developer/sp/app/library/Database.php on line 61
[08-Aug-2024 10:24:24 UTC] PHP Fatal error: Uncaught TypeError: App\Library\Database::select(): Return value must be of type array, null returned in /home/developer/sp/app/library/Database.php:61
Stack trace:
#0 /home/developer/sp/app/app.php(31): App\Library\Database->select()
#1 /home/developer/sp/index.php(4): require_once('...')
#2 {main}
thrown in /home/developer/sp/app/library/Database.php on line 61

View File

@ -1,144 +0,0 @@
<?php
require 'vendor/autoload.php';
require_once 'Specialization.php';
// require_once 'Size.php';
use GuzzleHttp\Client;
const TEMPLATE = '//tr[@itemprop="eduChislen"]//';
$dbconfig = [
'host' => '10.90.1.201',
'database' => 'opendata',
'user' => 'niimko_user',
'password' => 'MOhA17FeboXE'
];
$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',
];
$data = [];
$client = new Client();
$response = $client->get('https://marsu.ru/sveden/education/eduChislen.php');
$html = $response->getBody()->getContents();
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($fields as $field => $tag) {
$data[$field] = $xpath->query(TEMPLATE . $tag . "[@itemprop=\"$field\"]");
}
// echo "<pre>";
// print_r($data['numberAll'][0]);
// echo "</pre>";
$specialization = new Specialization();
// $sizeFederalBudget = new Size();
// $sizeRussianBudget = new Size();
// $sizeLocalBudget = new Size();
// $sizeIndividualsOrLegalEntitiesBudget = new Size();
$records = [];
$size = [];
for ($i = 0; $i < $data['numberAll']->length; $i++) {
// $sizeFederalBudget->update(
// $data['numberBF']->item($i)->textContent,
// $data['numberBFF']->item($i)->textContent
// );
// $size['бюджетных ассигнований федерального бюджета'] = $sizeFederalBudget->getData();
// $sizeRussianBudget->update(
// $data['numberBR']->item($i)->textContent,
// $data['numberBRF']->item($i)->textContent
// );
// $size['бюджетов субъектов Российской Федерации'] = $sizeRussianBudget->getData();
// $sizeLocalBudget->update(
// $data['numberBM']->item($i)->textContent,
// $data['numberBMF']->item($i)->textContent
// );
// $size['местных бюджетов'] = $sizeLocalBudget->getData();
// $sizeIndividualsOrLegalEntitiesBudget->update(
// $data['numberP']->item($i)->textContent,
// $data['numberPF']->item($i)->textContent
// );
// $size['средств физических и (или) юридических лиц'] = $sizeIndividualsOrLegalEntitiesBudget->getData();
$specialization->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[] = $specialization->getData();
$size = [];
}
// echo "<pre>";
// print_r($records[0]);
// echo "</pre>";
try {
$pdo = new PDO(
"mysql:host={$dbconfig['host']};dbname={$dbconfig['database']}",
$dbconfig['user'],
$dbconfig['password']
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Подключение успешно!";
} catch (PDOException $e) {
echo "Ошибка подключения: " . $e->getMessage() . "\n";
}
$sql = "
INSERT
INTO sveden_education_contingent (
spec_code,
spec_name,
edu_level,
edu_forms,
contingent
) VALUES (
:spec_code,
:spec_name,
:edu_level,
:edu_forms,
:contingent
)
";
// Почему-то не сработала
// foreach ($records as $record) {
// $sth = $pdo->prepare($sql);
// try {
// $sth->execute($record);
// } catch (PDOException $e) {
// echo "Ошибка выполнения запроса:". $e->getMessage() . "";
// }
// }
$sql = 'SELECT * FROM sveden_education_contingent';
$array = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
echo "<pre>";
print_r($array);
echo "</pre>";

View File

@ -6,9 +6,11 @@ $vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-message/src', $vendorDir . '/psr/http-factory/src'),
'Psr\\Http\\Message\\' => array($vendorDir . '/psr/http-factory/src', $vendorDir . '/psr/http-message/src'),
'Psr\\Http\\Client\\' => array($vendorDir . '/psr/http-client/src'),
'GuzzleHttp\\Psr7\\' => array($vendorDir . '/guzzlehttp/psr7/src'),
'GuzzleHttp\\Promise\\' => array($vendorDir . '/guzzlehttp/promises/src'),
'GuzzleHttp\\' => array($vendorDir . '/guzzlehttp/guzzle/src'),
'App\\Library\\' => array($baseDir . '/app/library'),
'App\\' => array($baseDir . '/app'),
);

View File

@ -24,13 +24,18 @@ class ComposerStaticInit045658d81f6d9d3243e731dda7bf04d1
'GuzzleHttp\\Promise\\' => 19,
'GuzzleHttp\\' => 11,
),
'A' =>
array (
'App\\Library\\' => 12,
'App\\' => 4,
),
);
public static $prefixDirsPsr4 = array (
'Psr\\Http\\Message\\' =>
array (
0 => __DIR__ . '/..' . '/psr/http-message/src',
1 => __DIR__ . '/..' . '/psr/http-factory/src',
0 => __DIR__ . '/..' . '/psr/http-factory/src',
1 => __DIR__ . '/..' . '/psr/http-message/src',
),
'Psr\\Http\\Client\\' =>
array (
@ -48,6 +53,14 @@ class ComposerStaticInit045658d81f6d9d3243e731dda7bf04d1
array (
0 => __DIR__ . '/..' . '/guzzlehttp/guzzle/src',
),
'App\\Library\\' =>
array (
0 => __DIR__ . '/../..' . '/app/library',
),
'App\\' =>
array (
0 => __DIR__ . '/../..' . '/app',
),
);
public static $classMap = array (