Переименовал классы и добавил выборку из второй БД
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
|
||||
use App\Library\Specialization;
|
||||
use App\Library\ContingentRow;
|
||||
|
||||
class SvedenParser
|
||||
class ContingentParser
|
||||
{
|
||||
private \DOMXPath $xpath;
|
||||
private string $template;
|
||||
@ -44,18 +44,17 @@ class SvedenParser
|
||||
public function getDataTable() : array
|
||||
{
|
||||
$data = $this->parse();
|
||||
$spec = new Specialization();
|
||||
$records = array();
|
||||
|
||||
for ($i = 0; $i < $data['numberAll']->length; $i++) {
|
||||
$spec->update(
|
||||
$contingentRow = new ContingentRow(
|
||||
$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();
|
||||
$records[] = $contingentRow->getData();
|
||||
}
|
||||
return $records;
|
||||
}
|
33
app/library/ContingentRow.php
Normal file
33
app/library/ContingentRow.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace App\Library;
|
||||
// Специальность, направление подготовки
|
||||
class ContingentRow
|
||||
{
|
||||
public function __construct(
|
||||
private string $eduCode,
|
||||
private string $eduName,
|
||||
private string $eduLevel,
|
||||
private string $eduForm,
|
||||
private int $contingent
|
||||
) {
|
||||
if ($contingent < 0) {
|
||||
throw new \Exception("Недействительная численность обучающихся!");
|
||||
}
|
||||
$this->eduCode = $eduCode;
|
||||
$this->eduName = $eduName;
|
||||
$this->eduLevel = $eduLevel;
|
||||
$this->eduForm = $eduForm;
|
||||
$this->contingent = $contingent;
|
||||
}
|
||||
|
||||
public function getData() : array
|
||||
{
|
||||
return [
|
||||
"spec_code" => $this->eduCode,
|
||||
"spec_name" => $this->eduName,
|
||||
"edu_level" => $this->eduLevel,
|
||||
"edu_forms"=> $this->eduForm,
|
||||
"contingent" => $this->contingent
|
||||
];
|
||||
}
|
||||
}
|
@ -6,9 +6,10 @@ use PDOException;
|
||||
|
||||
class Database extends PDO
|
||||
{
|
||||
public function __construct($dsn, $username, $password)
|
||||
public function __construct(string $dsn, string $username, string $password)
|
||||
{
|
||||
try {
|
||||
$dsn .= ";charset=utf8";
|
||||
parent::__construct(
|
||||
$dsn,
|
||||
$username,
|
||||
@ -28,20 +29,22 @@ class Database extends PDO
|
||||
|
||||
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']);
|
||||
$keys = array_keys($data);
|
||||
$sql = "INSERT INTO $table (";
|
||||
foreach ($keys as $key) {
|
||||
$sql .= "$key,";
|
||||
}
|
||||
$sql = substr_replace($sql, ")", -1);
|
||||
$sql .= " VALUES (";
|
||||
foreach ($keys as $key) {
|
||||
$sql .= ":$key,";
|
||||
}
|
||||
$sql = substr_replace($sql,"", -1);
|
||||
$stmt = $this->prepare($sql);
|
||||
|
||||
foreach ($data as $key => $row) {
|
||||
try {
|
||||
$stmt->bindParam(":$key", $row[$key]);
|
||||
$stmt->execute();
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
@ -49,7 +52,7 @@ class Database extends PDO
|
||||
}
|
||||
}
|
||||
|
||||
public function select(string $table) : array
|
||||
public function select(string $table) : array|null
|
||||
{
|
||||
$stmt = $this->prepare("SELECT * FROM $table");
|
||||
try {
|
||||
@ -61,4 +64,34 @@ class Database extends PDO
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
|
||||
public function selectWhere(
|
||||
string $table,
|
||||
array $atributes,
|
||||
array $filters
|
||||
) : array|null {
|
||||
// Строим запрос
|
||||
$sql = "SELECT ";
|
||||
foreach ($atributes as $atribute) {
|
||||
$sql .= "$atribute,";
|
||||
}
|
||||
$sql = substr_replace($sql," ", -1);
|
||||
$sql .= "FROM $table WHERE ";
|
||||
foreach ($filters as $key => $filter) {
|
||||
$sql .= "$key = :$key ";
|
||||
}
|
||||
$stmt = $this->prepare($sql);
|
||||
foreach ($filters as $key => $filter) {
|
||||
$stmt->bindParam(":$key", $filter);
|
||||
}
|
||||
// Выполняем
|
||||
try {
|
||||
$stmt->execute();
|
||||
$array = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
echo "Ошибка запроса: " . $e->getMessage() . "\n";
|
||||
} finally {
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
<?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
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user