Skip to content

Commit 1b0a624

Browse files
committed
Documentation improvements
1 parent cc993c8 commit 1b0a624

3 files changed

Lines changed: 99 additions & 59 deletions

File tree

README.md

Lines changed: 86 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ It handles all the weird exceptions with how we talk about scripture, including:
1212

1313
- Books with only a single chapter won't list the chapter. (e.g. 3 John 11)
1414
- Psalm is singular and Psalms is plural. (e.g. Psalm 23 and Psalms 101-102) Same for Song(s) of Solomon. This behavior
15-
can be changed with the 'plurl' option. (The misspelling is deliberate--all options are five letters.)
15+
can be changed with the 'plurl' option. (The misspelling is deliberate--most options are five letters.)
1616

1717
## Installation
1818

@@ -53,7 +53,7 @@ From a technical perspective, having a closed cannon is *vital* because verses a
5353
Should insertions or omissions be made, numbering would be askew for the rest of that book.
5454

5555
The author of this library is protestant, and holds to a closed canon consisting of the 66
56-
books [listed as western protestantism here](https://en.wikipedia.org/wiki/Biblical_canon#Western_Church). Changing the versification of scripture would be disastrous for any
56+
books [listed as Western Protestantism here](https://en.wikipedia.org/wiki/Biblical_canon#Western_Church). Changing the versification of scripture would be disastrous for any
5757
integer ScripturNums that are saved.
5858

5959
## Usage
@@ -64,67 +64,75 @@ The two most common ways of referring to a passage of scripture are, probably, h
6464
database. The constructor takes both of these forms.
6565

6666
For instance:
67-
68-
$s = new ScripturNum('Romans 1-8');
69-
var_dump($s);
70-
echo $s;
67+
```php
68+
$s = new ScripturNum('Romans 1-8');
69+
var_dump($s);
70+
echo $s;
71+
```
7172

7273
Produces the result:
73-
74-
object(ScripturNum\ScripturNum)
75-
protected 'int' => int 738197728
76-
protected 'book' => int 45
77-
protected 'startCh' => int 1
78-
protected 'startV' => int 1
79-
protected 'endCh' => int 8
80-
protected 'endV' => int 39
81-
Romans 1-8
74+
```
75+
object(ScripturNum\ScripturNum)
76+
protected 'int' => int 738197728
77+
protected 'book' => int 45
78+
protected 'startCh' => int 1
79+
protected 'startV' => int 1
80+
protected 'endCh' => int 8
81+
protected 'endV' => int 39
82+
Romans 1-8
83+
```
8284

8385
Or, start with a number:
84-
85-
$s = new ScripturNum(739119536);
86-
var_dump($s);
87-
echo $s;
86+
```php
87+
$s = new ScripturNum(739119536);
88+
var_dump($s);
89+
echo $s;
90+
```
8891

8992
Produces the output:
90-
91-
object(ScripturNum\ScripturNum)
92-
protected 'int' => int 739119536
93-
protected 'book' => int 45
94-
protected 'startCh' => int 9
95-
protected 'startV' => int 1
96-
protected 'endCh' => int 16
97-
protected 'endV' => int 27
98-
Romans 9-16
93+
```
94+
object(ScripturNum\ScripturNum)
95+
protected 'int' => int 739119536
96+
protected 'book' => int 45
97+
protected 'startCh' => int 9
98+
protected 'startV' => int 1
99+
protected 'endCh' => int 16
100+
protected 'endV' => int 27
101+
Romans 9-16
102+
```
99103

100104
### Starting with Other Numerical References
101105

102106
If you've already parsed a string input and have broken apart the numerical portions, you may need a different means.
103107

104108
(Matthew is the 40th book.)
105-
106-
// ScripturNum::newFromInts($book, $startCh, $startV = 1, $endCh = null, $endV = null)
107-
$n = ScripturNum::newFromInts(40, 8, null, 9);
108-
var_dump($n);
109-
echo $n;
109+
```php
110+
// ScripturNum::newFromInts($book, $startCh, $startV = 1, $endCh = null, $endV = null)
111+
$n = ScripturNum::newFromInts(40, 8, null, 9);
112+
var_dump($n);
113+
echo $n;
114+
```
110115

111116
Produces the output:
112-
113-
object(ScripturNum\ScripturNum)[3]
114-
protected 'int' => int 655134992
115-
protected 'book' => int 40
116-
protected 'startCh' => int 8
117-
protected 'startV' => int 1
118-
protected 'endCh' => int 9
119-
protected 'endV' => int 38
120-
Matthew 8-9
117+
```
118+
object(ScripturNum\ScripturNum)[3]
119+
protected 'int' => int 655134992
120+
protected 'book' => int 40
121+
protected 'startCh' => int 8
122+
protected 'startV' => int 1
123+
protected 'endCh' => int 9
124+
protected 'endV' => int 38
125+
Matthew 8-9
126+
```
121127

122128
### Getting Strings Out: Full Name
123129

124130
The object supports direct conversion to strings. Doing so will produce the same output as the `getLongString` function.
125131
For example:
126132

127-
echo new ScripturNum('1Jo1:9')
133+
```php
134+
echo new ScripturNum('1Jo1:9')
135+
```
128136

129137
Produces the output:
130138

@@ -137,8 +145,9 @@ each book in the array of names in the Bible class.
137145

138146
Abbreviated references are available through the `getAbbrev` function. By default, these are intended for use in a URL.
139147
Thus, they use a period in place of a colon to avoid the need for escaping.
140-
141-
echo (new ScripturNum('John 3:16'))->getAbbrev();
148+
```php
149+
echo (new ScripturNum('John 3:16'))->getAbbrev();
150+
```
142151

143152
Produces the output:
144153

@@ -147,6 +156,24 @@ Produces the output:
147156
By default, abbreviations use the numerical ordinal (2, as opposed to II or Second), and the *second* name given for
148157
each book in the array of names in the Bible class.
149158

159+
### Getting Strings Out: Links
160+
161+
You can provide a callback function to generate links or other special formatting for scripture references in a
162+
`callback` option. The callback will be given two parameters: the string to print, and the ScripturNum object that can
163+
be used to generate the link. For example:
164+
```php
165+
$a = new ScripturNumArray(['John 3:16']);
166+
$f = function (string $s, ScripturNum $sn) {
167+
$sc = $sn->getWholeChapters();
168+
return "<a href=\"https://www.esv.org/" . strtolower($sc->toString('abbrev')) . "\">$s</a>";
169+
};
170+
echo $a->toString(['callback' => $f]);
171+
```
172+
Results in:
173+
```html
174+
<a href="https://www.esv.org/jn3">John 3:16</a>
175+
```
176+
150177
### Database Querying
151178

152179
If you save ScripturNum integers to a given column in a database, you can easily and (relatively) efficiently query them
@@ -156,21 +183,22 @@ entirely contained within the range. Inclusive will, instead, include all resul
156183
even if it isn't entirely contained within the range.
157184

158185
For example, for a column named "Scripture", we could use this:
159-
160-
$s = new ScripturNum('Romans 8');
161-
$wheres = $s->toSqlInclusive('Scripture');
162-
186+
```php
187+
$s = new ScripturNum('Romans 8');
188+
$wheres = $s->toSqlInclusive('Scripture');
189+
```
163190
At this point, `$wheres` has a value of:
164-
165-
( (Scripture & 4278190080) = 738197504 AND (Scripture & 16773120) <= 917504 AND (Scripture & 4095) >= 186 )
166-
191+
```sql
192+
( (Scripture & 4278190080) = 738197504 AND (Scripture & 16773120) <= 917504 AND (Scripture & 4095) >= 186 )
193+
```
167194
So, we can insert this into a query:
168-
169-
$queryString = "SELECT * FROM scriptureData WHERE $wheres";
170-
195+
```php
196+
$queryString = "SELECT * FROM scriptureData WHERE $wheres";
197+
```
171198
`$queryString` now has the value:
172-
173-
SELECT * FROM scriptureData WHERE ( (Scripture & 4278190080) = 738197504 AND (Scripture & 16773120) <= 917504 AND (Scripture & 4095) >= 186 )
199+
```sql
200+
SELECT * FROM scriptureData WHERE ( (Scripture & 4278190080) = 738197504 AND (Scripture & 16773120) <= 917504 AND (Scripture & 4095) >= 186 )
201+
```
174202

175203
This can easily be used in most flavors of SQL. Two notes of caution:
176204
- If you want to use prepared statements, beware that you won't be able to re-use prepared statements with different
@@ -180,7 +208,7 @@ This can easily be used in most flavors of SQL. Two notes of caution:
180208

181209
## Sorting
182210

183-
In general, sorting by ScrupturNum integer value will result in the passages being ordered in the same sequential order
211+
In general, sorting by ScripturNum integer value will result in the passages being ordered in the same sequential order
184212
as in the Bible. Books take first priority, followed by start chapter, followed by start verse, followed by end
185213
chapter, followed by end verse.
186214

src/ScripturNumArray.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ public function getString($options = []): string
344344
}
345345

346346
/**
347-
* Return a human-readable string representation of the contained passages.
347+
* Return a human-readable string representation of the contained passages. Throws an exception on error.
348+
* To get an empty string on error instead, use getString() instead of this method.
348349
*
349350
* @param string|array $options The setting set to use, or an array of options.
350351
*

tests/ReadmeTests.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\TestCase;
66
use ScripturNum\ScripturNum;
7+
use ScripturNum\ScripturNumArray;
78

89
/**
910
* This test suite should mirror the README to validate that all examples in the README are true.
@@ -58,4 +59,14 @@ public function test_zeroIndex() {
5859

5960
$this->expectOutputString("Genesis 1:1");
6061
}
62+
63+
public function test_linkExample()
64+
{
65+
$a = new ScripturNumArray(['John 3:16']);
66+
$f = function (string $s, ScripturNum $sn) {
67+
$sc = $sn->getWholeChapters();
68+
return "<a href=\"https://www.esv.org/" . strtolower($sc->toString('abbrev')) . "\">$s</a>";
69+
};
70+
$this->assertEquals('<a href="https://www.esv.org/jn3">John 3:16</a>', $a->toString(['callback' => $f]));
71+
}
6172
}

0 commit comments

Comments
 (0)