-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSourceTextLibrary.php
370 lines (289 loc) · 10.8 KB
/
SourceTextLibrary.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?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;
use UrbanMonastics\SourceTextLibrary\Controllers\Index as IndexController;
use UrbanMonastics\SourceTextLibrary\Controllers\Languages as LanguageController;
use UrbanMonastics\SourceTextLibrary\Controllers\Texts as TextController;
use UrbanMonastics\SourceTextLibrary\Controllers\Versions as VersionController;
use UrbanMonastics\SourceTextLibrary\Controllers\ReferenceParsing as ReferenceParsing;
use UrbanMonastics\SourceTextLibrary\Models\Language as Language;
use UrbanMonastics\SourceTextLibrary\Models\Reference as Reference;
use UrbanMonastics\SourceTextLibrary\Models\Source as Source;
use UrbanMonastics\SourceTextLibrary\Models\Version as Version;
use \Exception;
use UrbanMonastics\SourceTextParser\SourceTextParser;
class SourceTextLibrary{
const version = '0.1';
/* -- Variables for Source Texts -- */
protected $Source; // This is the source.json file as an Object
protected $Reference; // What portion (Chapter, Chapter/Verse, Abbv) are we parsing
protected $sourceText; // This is to store the source text before we use it.
protected $Parser; // This is the Source Text Parser
protected $allowedSourceTypes = array( 'Book', 'Letter', 'Dictionary');
protected $allowedSourceSegments = array( 'Chapters', 'Abbreviations', 'None');
/* -- Controller References -- */
protected $IndexController;
protected $LanguagesController;
protected $VersionsController;
protected $TextsController;
function __construct( array $Source = array() ){
// Create Controllers
$this->IndexController = new IndexController( $this );
$this->VersionsController = new VersionController( $this );
$this->TextsController = new TextController( $this );
$this->LanguagesController = new LanguageController( $this );
$this->Parser = new SourceTextParser(); // Create an instance of the Source Text Parser to use for outputs
if( !empty( $Source ) ){
$this->setSource( $Source );
}
}
/**
* Load a local JSON resource and respond with an associative array.
*
* @param string $PathToTexts the path to the directory holding the actual texts (absolute paths preferred)
* @return array
*/
function _loadJson( string $PathToFile ){
try{
if( !file_exists( $PathToFile ) ){
return false; // This book/text does not exist
}
$Response = json_decode( file_get_contents($PathToFile), true );
}catch( \Exception $e){
echo $e->getMessage();
exit(1);
}
return $Response;
}
/* -- Automatically load a given library of data -- */
public function addLibrary( string $DirectoryPath ){
// Look for important files
if( file_exists( $DirectoryPath . 'languages.json' ) )
$this->loadLanguages( $DirectoryPath . 'languages.json' );
if( file_exists( $DirectoryPath . 'versions.json' ) )
$this->loadVersions( $DirectoryPath . 'versions.json');
// var_dump( 'Versions Loaded', $this->VersionsController->getVersions() );
return $this;
}
/* -- LanguageController Passthrough Functions -- */
public function addLanguage( string $Abbreviation, array $RawLanguage ){
$newLanguage = new Language( $Abbreviation, $RawLanguage, $FamilyName ); // Convert the Raw language data into a language model.
$this->LanguagesController->addLanguage( $Abbreviation, $newLanguage );
return $this;
}
public function getFamily( string $FamilyName = NULL ){
return $this->LanguagesController->getFamily( $FamilyName );
}
public function getFamilies(){
return $this->LanguagesController->getFamily();
}
public function getLanguage( string $Abbreviation = NULL ){
return $this->LanguagesController->getLanguage( $Abbreviation );
}
public function getLanguages(){
return $this->LanguagesController->getLanguages();
}
public function loadLanguages( string $PathToLanguagesFile ){
$this->LanguagesController->loadLanguages( $PathToLanguagesFile );
return $this;
}
/* -- END: LanguageController Passthrough Functions -- */
/* -- TextController Passthrough Functions -- */
public function setTextsPath( String $PathToTexts, Bool $HasVersionFolders = false ){
$this->TextsController->setTextsPath( $PathToTexts, $HasVersionFolders );
return $this;
}
/* -- END: TextController Passthrough Functions -- */
/* -- VersionController Passthrough Functions -- */
public function addVersion( string $Abbreviation, array $RawVersion ){
$newVersion = new Version( $this, $RawVersion ); // Convert the Raw version data into a version model.
$this->VersionsController->addVersion( $Abbreviation, $newVersion );
return $this;
}
public function checkVersion( string $Abbreviation = NULL ){
return $this->VersionsController->checkVersion( $Abbreviation );
}
public function getVersion( string $Abbreviation = NULL ){
return $this->VersionsController->getVersion( $Abbreviation );
}
public function getVersions(){
return $this->VersionsController->getVersions();
}
public function getVersionsByLanguage( string $LanguageFamily = NULL ){
return $this->VersionsController->getByLanguageFamily( $LanguageFamily );
}
public function loadVersionLanguages(){
$this->VersionsController->loadLanguages();
return $this;
}
public function loadVersions( string $PathToVersionsFile ){
$this->VersionsController->loadVersions( $PathToVersionsFile );
return $this;
}
public function setVersion( string $VersionAbbreviation){
$this->VersionsController->setVersion( $VersionAbbreviation );
return $this;
}
/* -- END: VersionController Passthrough Functions -- */
/* -- IndexController Passthrough Functions -- */
public function indexLoadLibrary( string $libraryPath ){
return $this->IndexController->loadLibrary( $libraryPath );
}
public function indexImport( array $ExportedIndex ){
return $this->IndexController->importIndex( $ExportedIndex );
}
public function indexExport(){
// This will export out the current Index so it can be cached for later use
return $this->IndexController->exportIndex();
}
public function indexGetIssues(){
// Return the array of issues found when building the current Index
return $this->IndexController->getIssues();
}
/* -- END: IndexController Passthrough Functions -- */
public function autoloadSource(){
if( is_null( $this->TextsController->getSourceFilePath() ) ){
var_dump('Fail 1');
return false;
}
try{
$Source = $this->_loadJson( $this->TextsController->getSourceFilePath() );
var_dump( $Source );
}catch( Exception $e){
var_dump('Fail 2');
return false; // This is fine to fail.
}
$this->setSource( $Source );
return true;
}
/**
* 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 setSource( array $Source, string $PathToTexts = null, array $Version = null ){
if( is_array( $Source ) )
$this->Source = new Source( $Source );
// Are we speeding up the process
if( !is_null( $PathToTexts ) && is_string( $PathToTexts ) ){
$this->setTextsPath( $PathToTexts );
}
if( is_array( $Version ) ){
$this->Version = new Version( $this, $Version );
}
// var_dump( 'Small Caps Now:', $this->smallCapsText, $this->selahHTML, $this->Version->getLicense() );
return $this;
}
/*
* Source Text Setters
*/
public function setReference(string $Reference, bool $getReference = false){
$this->textReference = null; // Clearn any cached reference
// Before we can set a reference we must have the TextsPath, and the Source information defined so we know how to look for files in that directory
if( !is_a( $this->Source, 'UrbanMonastics\SourceTextLibrary\Models\Source' ) ){
return false; // We can only set references if we know how the source is structured
}
$Reference = trim( $Reference );
$this->Reference = new ReferenceParsing( $Reference, $this->Source );
var_dump( $this->Reference );
// if( $this->Source->getSegments() === true ){
// $Store = array( $Reference ); // This reference can really be anything since it's in segments.
// }else if( $this->Source->getChapters() === true && $this->Source->getVerses() === true ){
// // This reference is stored as a key => array(); An empty array means the whole chapter
// // We need to expect ranges here, and commas, it's gonna be a shit show
// $Store = array();
// $tmp = new ReferenceParsing( $Reference, $this->Source );
// // var_dump( $tmp );
// }else if( $this->Source->getChapters() === true ){
// $Store = array();
// }else if( $this->Source->getVerses() === true ){
// $Store = array();
// $tmp = explode( ',', $Reference );
// foreach( $tmp as $v ){
// $v = trim( $v );
// if( strpos( $v, '-' ) ){
//
// }else{
// $Store[] = $v;
// }
// }
//
// }else
// $Store = NULL;
$this->textReference = $Store;
if( $getReference === true ){
return $this->getReference( true );
}
return $this;
}
private function fetchReference(){
// Get the reference and prepare to style it
$ProcessingText = NULL;
// Title
// Chapter
// Description
// FormatAs
// InlineTitles
// Footnotes
$Filename = 'text.json';
$Filename = 'chapter-0001.json';
$Filename = 'SEGMENT.json';
// TODO: Add support for Footnotes and InlineTitles
// Text
// Verses
$this->sourceText = NULL;
}
public function getReference( bool $fetchFirst = false ){
if( $fetchFirst )
$this->fetchReference(); // to cache the text
return $this->Parser->text( $this->sourceText );
}
/**
* Get the value of the allowedSourceSegments array stored in the SourceText class
*
* @return array
*/
public function getAllowedSourceSegments(){
return $this->allowedSourceSegments;
}
/**
* Get the value of the allowedSourceTypes array stored in the SourceText class
*
* @return array
*/
public function getAllowedSourceTypes(){
return $this->allowedSourceTypes;
}
/**
* Clear out all of the source information, and update the options to match.
*
* @param array $lines array of lines that need to be processed
* @return array
*/
public function resetSource(){
$this->sourceObject = null;
$this->versionObject = null;
$this->TextsController->reset();
$this->Parser->setBreaksEnabled( false );
$this->Parser->setPreserveIndentations( false );
// These are defined when the version is set. So reset them to their default [false]
$this->Parser->setSelahHTML( false );
$this->Parser->setSmallCapsText( false );
}
/*
* END: Source Text Setters
*/
}