-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguagesController.php
147 lines (122 loc) · 4.32 KB
/
LanguagesController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/***********
*
* Source Text Library
* https://developers.urbanmonastic.org/
*
* © Paul Prins
* https://paulprins.net https://paul.build/
*
* Licensed under MIT - For full license, view the LICENSE distributed with this source.
*
***********/
namespace UrbanMonastics\SourceTextLibrary\Controllers;
use UrbanMonastics\SourceTextLibrary\Models\Language as Language;
use UrbanMonastics\SourceTextLibrary\SourceTextLibrary;
use \Exception;
class Languages{
// Manage the location, and loading of the various files from the location
private $Languages = array();
private $Families = array();
/* -- Reference to Parent -- */
private $SourceTextLibrary;
function __construct( SourceTextLibrary &$SourceTextLibrary ){
$this->SourceTextLibrary = $SourceTextLibrary;
}
/**
* Set the Source details from a relevant source-texts source. You can overload it to simplify the steps involved.
*
* @param array $Source array of lines that need to be processed
* @param string $PathToTexts the path to the directory holding the actual texts (absolute paths preferred)
* @param array $Version array of the version being fetched
* @return this
*/
public function addLanguage( string $Abbreviation, Language $newLanguage ){
$this->Languages[$Abbreviation] = $newLanguage;
if( !array_key_exists( $newLanguage->getFamily(), $this->Families ) )
$this->Families[$newLanguage->getFamily()] = array();
/* -- Add to the family index -- */
$this->Families[$newLanguage->getFamily()][] = $newLanguage;
// Fancy sort the family by the age of the related entries with the newest at the top of the list
usort( $this->Families[$newLanguage->getFamily()], function($a, $b) {
if( is_null( $a->getEndDate() ) ){
if( $a->getEndDate() == $b->getEndDate() ){
// Both end dates are open, so which ever one started first should be listed first.
return $a->getStartDate() > $b->getStartDate();
}
return -1;
}else if( is_null( $b->getEndDate() ) )
return 1;
return -($a->getEndDate() <=> $b->getEndDate());
});
}
/**
* Retreive a specific language family
*
* @param string $FamilyName The name of the language family you are requesting
* @return array of Languages
*/
public function getFamily( string $FamilyName = NULL ){
if( is_null( $FamilyName ) || $FamilyName == ''){
throw new \Exception('Unable to getFamily as no current family name has been defined.');
}
if( !array_key_exists( $FamilyName, $this->Families ) ){
throw new \Exception('Unable to getFamily as the language family does not exist: \'' . $FamilyName . '\'' );
}
return $this->Families[$FamilyName];
}
/**
* Return all of the languages grouped by language family
*
* @return array of languages
*/
public function getFamilies(){
return $this->Families;
}
/**
* Retreive a specific language
*
* @param string $Abbreviation array of lines that need to be processed
* @return Language
*/
public function getLanguage( string $Abbreviation = NULL ){
if( is_null( $Abbreviation ) || $Abbreviation == '' ){
throw new \Exception('Unable to getLanguage as no current abbreviation has been defined.');
}
if( !array_key_exists( $Abbreviation, $this->Languages ) ){
throw new \Exception('Unable to getLanguage as language does not exist: \'' . $Abbreviation . '\'' );
}
return $this->Languages[$Abbreviation];
}
/**
* Return all of the languages that are current loaded
*
* @return array of languages
*/
public function getLanguages(){
return $this->Languages;
}
/**
* Set the Source details from a relevant source-texts source. You can overload it to simplify the steps involved.
*
* @param array $Source array of lines that need to be processed
* @param string $PathToTexts the path to the directory holding the actual texts (absolute paths preferred)
* @param array $Version array of the version being fetched
* @return this
*/
public function loadLanguages( string $LanguagesFilePath ){
try{
$LanguageList = $this->SourceTextLibrary->_loadJson( $LanguagesFilePath );
}catch( Exception $e){
echo $e->getMessage();
exit(1);
}
foreach( $LanguageList as $Family => $Varraints ){
foreach( $Varraints as $abv => $v ){
$tmpLanguage = new Language( $abv, $v, $Family );
$this->addLanguage( $abv, $tmpLanguage );
}
}
return $this;
}
}