sveden-parser/app/library/ContingentManager.php

152 lines
4.6 KiB
PHP
Raw Normal View History

<?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 getOrgs(Database $db) : array
{
$sql = 'SELECT DISTINCT org_id FROM sveden_education_contingent';
$org = $db->selectQuery($sql);
$orgs = [];
foreach ($org as $o) {
$orgs[] = $o['org_id'];
}
return $orgs;
}
public function buildBaseUri(string $url): string
{
// Строит -> https://<base_uri>
if (strpos($url,'https://') === false && strpos($url,'http://') === false) {
$url = "http://$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;
}
public function getExceptionsHtml(string $filename) : array
{
$errorSites = [];
$array = file($filename);
for ($i = 0; $i < count($array); $i++) {
$arr = explode(' ', $array[$i]);
$errorSites[] = $arr[2];
}
return $errorSites;
}
public function getExceptionsHttpCurl(string $filename) : array
{
$array = file($filename);
$orgHttpError = [];
foreach ($array as $str) {
$data = explode (' ', $str);
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $data[0])
&& $data[3] != PHP_EOL) {
$orgHttpError[] = $data[2];
// $orgHttpError[] = ['org_id' => $data[2], 'site' => $data[3]];
}
}
$orgHttpError = array_unique($orgHttpError);
sort($orgHttpError);
return $orgHttpError;
}
}