Commit 25ef4d55 authored by Dainis Abols's avatar Dainis Abols
Browse files

Version 1.0.0

parents
Pipeline #129 canceled with stages
/.idea
/vendor
This diff is collapsed.
# Connector for VIIS schools list
Install via Composer
```
composer req luitd/viis-schools
```
## Quick access
``` php
$ViisSchools = new Lu\VIISSchools\VIISSchools('user', 'pass');
$schools = $ViisSchools->getSchoolList();
```
## Options
| VIISSchools::setTestEnvironment() | Set test environment flag |
| VIISSchools::setReturnType() | Change return type. Possible options:<br>ReturnType::JSON *Default*<br>ReturnType::XML<br>ReturnType::ARRAY<br>ReturnType::OBJECT |
## Requirements
- PHP 7.2
- PHP SOAP Extension
{
"name": "luitd/viis-schools",
"version": "1.0.0",
"description": "Connector for VIIS schools list. Fetches official active schools list.",
"type": "library",
"authors": [
{
"name": "Dainis Abols",
"email": "dainis@dainisabols.lv"
}
],
"license": "GPL-3.0-or-later",
"require": {
"php": ">=7.2.0"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"autoload": {
"psr-4": {
"Lu\\VIISSchools\\": "src/"
}
},
"config": {
"bin-dir": "bin"
}
}
\ No newline at end of file
<?php
namespace Lu\VIISSchools\Helpers;
use Lu\VIISSchools\Models\Address;
use Lu\VIISSchools\Models\School;
/**
* Class Builder
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @since 30.09.2020
*
* @package Lu\VIISSchools\Helpers
*/
class Builder
{
/**
* Build array
*
* @param $schools
*
* @return array
*/
public static function makeArray($schools)
{
// Load xml into array structure
$xml = new \SimpleXMLElement($schools);
// Rebuild int regular array
$data = [];
foreach ($xml as $element) {
$school = [];
// Load attributes
foreach ($element->attributes() as $attribute => $value) {
$school[$attribute] = trim(reset($value), '"');
}
// Load address
foreach ($element->children()->attributes() as $attribute => $value) {
$school['address'][$attribute] = reset($value);
}
$data[] = $school;
}
return $data;
}
/**
* Build object
*
* @param $schools
*
* @return array
*/
public static function makeObject($schools)
{
// Load xml into array structure
$xml = new \SimpleXMLElement($schools);
// Rebuild int regular array
$data = [];
foreach ($xml as $element) {
$school = new School();
// Load address
$address = new Address();
$address->fullAddress = reset($element->children()->attributes()->{'AddressText'});
$address->code = reset($element->children()->attributes()->{'AddressCode'});
$address->area = reset($element->children()->attributes()->{'AddressArea'});
$address->city = reset($element->children()->attributes()->{'AddressCity'});
$address->municipality = reset($element->children()->attributes()->{'AddressMunicipality'});
$address->suburban = reset($element->children()->attributes()->{'AddressSuburban'});
$address->village = reset($element->children()->attributes()->{'AddressVillage'});
$address->street = reset($element->children()->attributes()->{'AddressStreet'});
$address->house = reset($element->children()->attributes()->{'AddressHouse'});
$address->flatNumber = reset($element->children()->attributes()->{'AddressFlatNumber'});
$address->postalCode = reset($element->children()->attributes()->{'AddressPostalCode'});
$address->regionalDivision = reset($element->children()->attributes()->{'RegionalDivision'});
// Build school model
$school->registrationNumber = reset($element->attributes()->{'InstitutionRegistrationNumber'});
$school->name = trim(reset($element->attributes()->{'Name'}), '"');
$school->startDate = reset($element->attributes()->{'StartDate'});
$school->endDate = reset($element->attributes()->{'EndDate'});
$school->email = reset($element->attributes()->{'Email'});
$school->phone = reset($element->attributes()->{'Phone'});
$school->address = $address;
// Assign to general list
$data[] = $school;
}
return $data;
}
}
\ No newline at end of file
<?php
namespace Lu\VIISSchools\Helpers;
/**
* Class WsseAuthHeader
* Creates SOAP header object with main connection details
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @since 30.09.2020
*
* @package Lu\VIISSchools
*/
class WsseAuthHeader extends \SoapHeader
{
/**
* SOAP Structure standard
*
* @var string
*/
private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
/**
* WsseAuthHeader constructor.
*
* @param $user
* @param $pass
* @param null $ns
*/
public function __construct($user, $pass, $ns = null)
{
// Use standard ns if no other provided
if ($ns) {
$this->wss_ns = $ns;
}
// Make connection object
$auth = new \stdClass();
$auth->Username = new \SoapVar($user, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
$auth->Password = new \SoapVar($pass, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
// Make connection token
$username_token = new \stdClass();
$username_token->UsernameToken = new \SoapVar($auth, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns);
// Make security object
$security_sv = new \SoapVar(
new \SoapVar($username_token, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns),
SOAP_ENC_OBJECT, null, $this->wss_ns, 'Security', $this->wss_ns
);
// Continue to SOAP defaults
parent::__construct($this->wss_ns, 'Security', $security_sv, true);
}
}
<?php
namespace Lu\VIISSchools\Models;
/**
* Class Address
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @since 30.09.2020
*
* @package Lu\VIISSchools
*/
class Address
{
/**
* Full address string
*
* @var string
*/
public $fullAddress;
/**
* Address code
*
* @var int
*/
public $code;
/**
* Area name
*
* @var string
*/
public $area;
/**
* City name
*
* @var string
*/
public $city;
/**
* Municipality name
*
* @var string
*/
public $municipality;
/**
* Suburban name
*
* @var string
*/
public $suburban;
/**
* Village name
*
* @var string
*/
public $village;
/**
* Street name
*
* @var string
*/
public $street;
/**
* House number
*
* @var string
*/
public $house;
/**
* Flat/apartment number
*
* @var string
*/
public $flatNumber;
/**
* Postal code
*
* @var string
*/
public $postalCode;
/**
* Regional division name
*
* @var string
*/
public $regionalDivision;
}
\ No newline at end of file
<?php
namespace Lu\VIISSchools\Models;
/**
* Class ReturnType
* Set return option types
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @since 30.09.2020
*
* @package Lu\VIISSchools
*/
class ReturnType
{
/**
* Return dta as JSON
*/
public const JSON = 'json';
/**
* Return data as XML
*/
public const XML = 'xml';
/**
* Return data as php array
*/
public const ARRAY = 'array';
/**
* Return data as php array
*/
public const OBJECT = 'object';
/**
* Returns all possible options
*
* @param bool $values
*
* @return array
*/
public static function allTypesArray(bool $values = true): array
{
// Return possible return types
return $values ? [self::JSON, self::XML, self::ARRAY, self::OBJECT] : ['ReturnType::JSON', 'ReturnType::XML', 'ReturnType::ARRAY', 'ReturnType::OBJECT'];
}
}
\ No newline at end of file
<?php
namespace Lu\VIISSchools\Models;
/**
* Class School
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @since 30.09.2020
*
* @package Lu\VIISSchools
*/
class School
{
/**
* Institution official registration number
*
* @var int
*/
public $registrationNumber;
/**
* Institution full name
*
* @var string
*/
public $name;
/**
* Institution operational start date
*
* @var \DateTime
*/
public $startDate;
/**
* Institution operational end date
*
* @var \DateTime
*/
public $endDate;
/**
* Institution contact e-mail
*
* @var string
*/
public $email;
/**
* Institution phone numbers
*
* @var string
*/
public $phone;
/**
* Institution address
*
* @var \Lu\VIISSchools\Models\Address
*/
public $address;
}
<?php
namespace Lu\VIISSchools;
use Lu\VIISSchools\Helpers\Builder;
use Lu\VIISSchools\Helpers\WsseAuthHeader;
use Lu\VIISSchools\Models\ReturnType;
/**
* Class VIISSchools
*
* @author Dainis Abols <dainis.abols@lu.lv>
* @since 30.09.2020
*
* @package Lu\VIISSchools
*/
class VIISSchools
{
/**
* Define environment type
*
* @var string
*/
private $environment = "LIVE";
/**
* Active soap connection
*
* @var \SoapClient
*/
private $soapClient;
/**
* Return type
*
* @var string
*/
private $returnType = ReturnType::JSON;
/**
* Live connection url
*
* @var string
*/
private $urlLive = "https://test.viis.lv:442/Services/UniversalDataSetService.svc?wsdl";
/**
* Test connection url
*
* @var string
*/
private $urlTest = "https://test.viis.lv:442/Services/UniversalDataSetService.svc?wsdl";
/**
* Connection user
*
* @var string
*/
private $user;
/**
* Connection password
*
* @var string
*/
private $pass;
/**
* Connection ns
*
* @var string
*/
private $ns;
/**
* VIISSchools constructor.
*
* @param string $user
* @param string $pass
* @param string|null $ns
*/
public function __construct(string $user, string $pass, string $ns = null)
{
$this->user = $user;
$this->pass = $pass;
$this->ns = $ns;
}
/**
* Destroy SOAP connection
*/
public function __destruct()
{
unset($this->soapClient);
}
/**
* Set test environment
*/
public function setTestEnvironment()
{
$this->environment = "TEST";
}
/**
* Sets desired return type. Default: ReturnType::JSON
*
* @param string $returnType
*
* @throws \Exception
*/
public function setReturnType(string $returnType)
{
// check if correct return requested
if (!in_array($returnType, ReturnType::allTypesArray())) {
throw new \Exception("Return type must be a valid ReturnType object constant: ".$this->getReturnTypes());
}
$this->returnType = $returnType;
}
/**
* Retrieve environment type
*
* @return string
*/
public function getEnvironment()
{
return $this->environment;
}
/**
* Return const list of available return types
*
* @return string
*/
public function getReturnTypes()
{
return \json_encode(ReturnType::allTypesArray(false));
}
/**
* Fetches schools
*
* @return array|false|string
* @throws \SoapFault
*/
public function getSchoolList()
{
// check if correct return requested
if (!in_array($this->returnType, ReturnType::allTypesArray())) {
throw new \Exception("Return type must a valid ReturnType object constant: ".$this->getReturnTypes());
}
$schools = $this->fetchAllSchools();
return $this->returnSchools($schools);
}
/**
* Initialize SOAP client and headers
*
* @throws \SoapFault
*/
private function initSoapAndHeaders()
{
// Initialize client
$this->soapClient = new \SoapClient(
$this->environment == "LIVE" ? $this->urlLive : $this->urlTest,
[
"trace" => true,
"exceptions" => 1,
]
);
// Make headers
$wsse_header = new WsseAuthHeader($this->user, $this->pass, $this->ns);
// Prepare for execution
$this->soapClient->__setSoapHeaders([$wsse_header]);
}
/**
* Fetch schools
*
* @return mixed
* @throws \SoapFault
*/
private function fetchAllSchools()
{
// Open soap connection
$this->initSoapAndHeaders();
// Try connection
try {
$respObj = $this->soapClient->Execute([
'dataSetFilterCode' => 'Iestazu_info',
]);
} catch (\SoapFault $e) {
throw new \Exception($e);
}
// Fetch schools list
return reset($respObj)->any;
}
/**
* Prepares school data in requested format and returns
*
<