univ-pau-ics/build/service/Config.php

208 lines
5.2 KiB
PHP
Raw Normal View History

<?php
namespace service;
class Config{
/* [1] Attributes
=========================================================*/
private $diplomes = null;
private $periods = null;
private static function monthAsso(){
return [
'janvier' => '01',
'février' => '02',
'mars' => '03',
'avril' => '04',
'mai' => '05',
'juin' => '06',
'juillet' => '07',
'août' => '08',
'septembre' => '09',
'octobre' => '10',
'novembre' => '11',
'décembre' => '12'
];
}
/* (1) Constructs a filled config object
*
* @d_list<Array> List of the diplomes ( ID => name )
* @p_list<Array> List of the periods ( ID => first day's date )
*
* @return this<Config> New instance
*
---------------------------------------------------------*/
private function __construct($d_list=null, $p_list=null){
/* [1] Check arguments (type: array)
=========================================================*/
if( !is_array($d_list) || !is_array($p_list) )
throw new \Exception("Config.__construct(<Array>, <Array) expected but Config.__construct(<".gettype($d_list).">, <".gettype($d_list).">) received");
/* [2] Basic setter
=========================================================*/
/* (1) Set the diplomes' list */
$this->diplomes = $d_list;
/* (2) Set the periods' list */
$this->periods = $p_list;
}
/* (2) Loads the configuration from URL
*
* @d_script<String> URL of the script containing the diplomes list
* @p_script<String> URL of the script containing the periods list
*
* @return instance<Config> The created instance
* NULL on error
*
---------------------------------------------------------*/
public static function load($d_script=null, $p_script=null){
/* [1] Check arguments (type + validity)
=========================================================*/ {
/* (1) Check arguments' types (string, string) */
if( !is_string($d_script) || !is_string($p_script) )
throw new \Exception("Config::load(<Array>, <Array) expected but Config::load(<".gettype($d_script).">, <".gettype($d_script).">) received");
/* (2) Check @d_script URL validity */
if( !($d_h=@get_headers($d_script)) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
/* (3) Check @p_script URL validity */
if( !($p_h=@get_headers($p_script)) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
/* (4) Check image exists @dscript */
if( !preg_match('@HTTP.+200@', $d_h[0]) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
/* (5) Check image exists @dscript */
if( !preg_match('@HTTP.+200@', $p_h[0]) )
throw new \Exception("Config::load(<1>, <2>) received but cannot reach <1>");
}
/* [2] Get file cursors
=========================================================*/ {
/* (1) Try to get file cursors */
try{
$d_cur = new \SplFileObject($d_script, "r");
$p_cur = new \SplFileObject($p_script, "r");
/* (2) Raise on failure */
}catch(\Exception $e){
throw new \Exception("Config::load(<1>, <2>) cannot get a cursor on a one of both files");
}
}
/* [3] Filter and register content
=========================================================*/{
/* (1) Manage diplomes
---------------------------------------------------------*/ {
/* (1) Initialize diplomes' list */
$d_list = [];
/* (2) Fetch each line and extract data */
while( !$d_cur->eof() ){
// {1} If does not match -> go to next line //
if( !preg_match('@"grDiplome","(?:&lt;)?(.+)(?:&gt;)?","T(.+)"@i', $d_cur->fgets(), $m) )
continue;
// {2} Register into diplome list //
$d_list[ "T${m[2]}" ] = html_entity_decode($m[1]);
}
/* (2) Close file cursor */
$d_cur = null;
}
/* (2) Manage periods
---------------------------------------------------------*/{
/* (1) Initialize periods' list */
$p_list = [];
/* (2) Fetch each line and extract data */
while( !$p_cur->eof() ){
// {1} If does not match -> go to next line //
if( !preg_match('@"T\d+","du \d+ au (\d+) ([a-z]+) (\d+)","T\d+S(.+)"@i', $p_cur->fgets(), $m) )
continue;
// {2} Get numeric month number //
$month = self::monthAsso()[ $m[2] ];
// {3} Get monday date //
$mon = date( 'd-m-Y', strtotime("${m[1]}-$month-${m[3]} - 5 days") );
// {4} Register into the list //
$p_list[ "S${m[4]}" ] = $mon;
}
/* (2) Close file cursor */
$p_cur = null;
}
}
/* [4] Return the instance
=========================================================*/
return new Config($d_list, $p_list);
}
/* (3) Getter diplomes
*
* @return diplomes<Array> Diplome list
*
---------------------------------------------------------*/
public function getDiplomes(){
return $this->diplomes;
}
/* (4) Getter periods
*
* @return periods<Array> Period list
*
---------------------------------------------------------*/
public function getPeriods(){
return $this->periods;
}
}