-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferenceParsingController.php
116 lines (90 loc) · 3.66 KB
/
ReferenceParsingController.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
<?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\Source as Source;
use UrbanMonastics\SourceTextLibrary\Models\Reference as theReference;
class ReferenceParsing{
/* -- Reference Variables -- */
private $Reference;
private $Source;
public function __construct( string $ReferenceString, Source $Source = null){
$this->Reference = new theReference();
$this->Source = $Source;
// Copy some Source values into the Reference
$this->Reference->setSegments( $this->Source->getSegments() );
$this->Reference->setVerses( $this->Source->getVerses() );
$tmp = $this->parse( $ReferenceString, $this->Reference->getSegments() == 'Chapters', $this->Reference->getVerses() );
var_dump( $ReferenceString, $tmp, $this->Reference );
return $this->Reference;
}
private function parse( string $Reference, bool $WithChapters = false, bool $WithVerses = false ){
$Response = array();
if( ($WithVerses === true && $WithChapters === false) ){
// This only has Verrses
if( strpos( $Reference, ',' ) !== false ){
$segments = explode( ',', $Reference );
}else{
$segments = array( $Reference );
}
foreach( $segments as $s ){
$s = trim( $s );
if( strpos( $s, '-') ){
$tmp = explode( '-', $s );
for ($i = $tmp[0]; $i <= $tmp[1]; $i++) {
$Response[] = (int) $i;
}
}else{
$Response[] = (int) $s;
}
}
asort( $Response );
}else if( ($WithChapters === true && $WithVerses === false) ){
// This only has chapters
$tmp = $this->parse( $Reference, false, true ); // Same as verses, but we just need to set the response as the keys
$Response = array_fill_keys( $tmp, array() );
}else if( $WithVerses === true && $WithChapters === true ){
// There are Chapters and Verses
$FirstBreak = strpos( $Reference, ':' );
if( $FirstBreak === false && strpos( $Reference, '-' ) === false && strpos( $Reference, ',' ) === false ){
// This is just simply a chapter number
$Response[$Reference] = array();
}
else if( $FirstBreak === false ){
// This is a list of chapters to include without any verse references
$Response = $this->parse( $Reference, true, false );
ksort( $Response );
}
else if( $FirstBreak !== false && substr_count( $Reference, ':') == 1 ){
// A Single chapter with verses
$tmp = explode( ':', $Reference );
$Response[$tmp[0]] = $this->parse( $tmp[1], false, true );
}else{
// These have verses multiple chapters with verses
// Split the request in half at the `-CHAPTER:` mark
$segments = preg_split("/(-[0-9]*:)/i", $Reference, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
$StartTmp = explode( ':', $segments[0], 2 );
$FirstVerses = $StartTmp[1] . '-300'; // Up to 300 verses, since I do not know of any chapters/segments beyond 180 verses. This 2/3rds buffer should be sufficant.
$EndTmp = substr( $segments[1], 1, -1 );
$LastVerses = '1-' . $segments[2];
// Fill the response keys with all the chapters
$Response = array_fill( $StartTmp[0], ((int)$EndTmp - (int)$StartTmp[0] + 1), array());
// Ensure we only get the portion of the first chapter that is requested
$Response[$StartTmp[0]] = $this->parse( $FirstVerses, false, true );
// Ensure we only get the portion of the last chapter that is requested
$Response[$EndTmp] = $this->parse( $LastVerses, false, true );
}
}
return $Response;
}
}
?>