sveden-parser/app/library/ContingentManager.php

122 lines
3.7 KiB
PHP

<?php
namespace App\Library;
use NilPortugues\Sql\QueryBuilder\Builder\GenericBuilder;
final class ContingentManager
{
private static ?ContingentManager $instance;
private ?GenericBuilder $builder;
private function __construct()
{
$this->builder = new GenericBuilder();
}
public static function getInstance() : ContingentManager
{
self::$instance ??= new self();
return self::$instance;
}
public function getSites(Database $db): array
{
// select kod as org_id, site from niimko.s_vuzes
// where ootype = 'vuz' and deleted = 'n' and fake = 'n'
$params = ['vuz', 'n', 'n', 'RU'];
$query = $this->builder->select()
->setTable('s_vuzes')
->setColumns(['org_id' => 'kod', 'site'])
->where('AND')
->equals('ootype', 'vuz')
->equals('deleted', 'n')
->equals('fake', 'n')
->equals('country', 'RU')
->end();
$sql = $this->builder->write($query);
$sites = $db->selectQuery($sql, $params);
return $sites;
}
public function insertContingent(Database $db, array $contingent) : void
{
$params = ['spec_code', 'spec_name', 'edu_level', 'edu_forms', 'contingent', 'spec_id', 'org_id'];
$sql = "insert into sveden_education_contingent"
."(". implode(',', $params) .") values";
for ($i = 0; $i < count($contingent); $i++) {
$sql .= "(";
foreach ($contingent[$i] as $key => $value) {
$sql .= ":$key". ($i+1).",";
}
$sql = substr_replace($sql,"),", -1);
}
$sql = substr_replace($sql,"", -1);
$db->insertQuery($sql, $contingent);
}
public function getSpecializations(Database $db) : array
{
// select id, kod from niimko.s_specs where oopkodes = 'gos3p'
$params = ['gos3p'];
$query = $this->builder->select()
->setTable('s_specs')
->setColumns(['id', 'kod'])
->where()
->equals('oopkodes','gos3p')
->end();
$sql = $this->builder->write($query);
$specializations = $db->selectQuery($sql, $params);
return $specializations;
}
public function buildURL(string $url): string
{
// Строит -> https://<base_uri>/sveden/education/
$offset = strpos($url, '/', strlen('http://'));
if ($offset) {
$url = substr_replace($url, '', $offset);
}
$url = "$url/sveden/education/";
if (str_contains($url, "http://")) {
$url = str_replace("http://","https://", $url);
} else {
$url = "https://$url";
}
$url = str_replace("www.","", $url);
return $url;
}
public function addSpecId(array &$contingent, array $specializations) : void
{
foreach ($contingent as $key => $con) {
$buf = null;
$needle = $con['spec_code'];
foreach ($specializations as $spec) {
if ($needle == $spec['kod']) {
$buf = $spec['id'];
}
}
$contingent[$key]['spec_id'] = $buf;
unset($buf);
}
}
public function addOrgId(array &$contingent, int $orgId) : void
{
for($i = 0; $i < count($contingent); $i++) {
$contingent[$i]['org_id'] = $orgId;
}
}
public function checkContingent(array $contingent) : bool
{
$count = 0;
foreach ($contingent as $value) {
$count += $value['contingent'];
}
return $count ? true : false;
}
}