jkrrv/scripturnum
Composer 安装命令:
composer require jkrrv/scripturnum
包简介
Fit references of Scripture passages in just 32 bits (an unsigned integer)
关键字:
README 文档
README
This is a PHP Library intended for working with scripture references. It can parse text-based scripture references, and can express passages of scripture in only 32 bits (31, actually), which allows a scripture range to be stored in a single unsigned integer in most DBs.
It handles all the weird exceptions with how we talk about scripture, including:
- Books with only a single chapter won't list the chapter. (e.g. 3 John 11)
- Psalm is singular and Psalms is plural. (e.g. Psalm 23 and Psalms 101-102) Same for Song(s) of Solomon. This behavior can be changed with the 'plurl' option. (The misspelling is deliberate--most options are five letters.)
Installation
Use Composer. Just run:
composer require jkrrv/scripturnum
composer install --no-dev
Or, download the source files from GitHub. The code comes ready-to-run; there is no particular 'build' procedure.
Prerequisites
- PHP >7.0
A few other packages are needed if you want to run the tests, but those will easily install with Composer.
Restrictions, Limitations and Cautions
There are a few (only a few) restrictions imposed within this library, most of which have to do with just how little can really be crammed into 32 bits. Specifically:
- Ranges can only be contained within one book. So, "John 2-3" is fine. "2 John - 3 John" is not.
- Behavior is undefined when a start reference comes after the end reference. In most cases, exceptions will be thrown to prevent you from doing this, because "Ephesians 2:10-1" doesn't really make sense. This reversal is discouraged, as it makes database searching dramatically more difficult, as database searching would probably be based on bitmasking.
- The Canon and versification cannot change while values are stored in a database, except in some fairly narrow cases.
- Beware of similarly-named books, which may not quite render what you intended when abbreviated. For instance, does ' Ju' refer to Judges or Jude? We say Jude. 'Jg' refers to Judges.
This library has 100% test coverage, and issues are usually dealt with expediently. If you encounter a bug, please report it to the issue tracker.
A Note on Canon
From a technical perspective, having a closed cannon is vital because verses and books are numbered sequentially. Should insertions or omissions be made, numbering would be askew for the rest of that book.
The author of this library is protestant, and holds to a closed canon consisting of the 66 books listed as Western Protestantism here. Changing the versification of scripture would be disastrous for any integer ScripturNums that are saved.
Usage
For full API documentation, see the wiki.
The two most common ways of referring to a passage of scripture are, probably, human-readable and as stored in a database. The constructor takes both of these forms.
For instance:
$s = new ScripturNum('Romans 1-8'); var_dump($s); echo $s;
Produces the result:
object(ScripturNum\ScripturNum)
protected 'int' => int 738197728
protected 'book' => int 45
protected 'startCh' => int 1
protected 'startV' => int 1
protected 'endCh' => int 8
protected 'endV' => int 39
Romans 1-8
Or, start with a number:
$s = new ScripturNum(739119536); var_dump($s); echo $s;
Produces the output:
object(ScripturNum\ScripturNum)
protected 'int' => int 739119536
protected 'book' => int 45
protected 'startCh' => int 9
protected 'startV' => int 1
protected 'endCh' => int 16
protected 'endV' => int 27
Romans 9-16
Starting with Other Numerical References
If you've already parsed a string input and have broken apart the numerical portions, you may need a different means.
(Matthew is the 40th book.)
// ScripturNum::newFromInts($book, $startCh, $startV = 1, $endCh = null, $endV = null) $n = ScripturNum::newFromInts(40, 8, null, 9); var_dump($n); echo $n;
Produces the output:
object(ScripturNum\ScripturNum)[3]
protected 'int' => int 655134992
protected 'book' => int 40
protected 'startCh' => int 8
protected 'startV' => int 1
protected 'endCh' => int 9
protected 'endV' => int 38
Matthew 8-9
Getting Strings Out: Full Name
The object supports direct conversion to strings. Doing so will produce the same output as the getLongString function.
For example:
echo new ScripturNum('1Jo1:9')
Produces the output:
1 John 1:9
By default, full references use the numerical ordinal (2, as opposed to II or Second), and the first name given for each book in the array of names in the Bible class.
Getting Strings Out: An Abbreviation
Abbreviated references are available through the getAbbrev function. By default, these are intended for use in a URL.
Thus, they use a period in place of a colon to avoid the need for escaping.
echo (new ScripturNum('John 3:16'))->getAbbrev();
Produces the output:
Jn3.16
By default, abbreviations use the numerical ordinal (2, as opposed to II or Second), and the second name given for each book in the array of names in the Bible class.
Getting Strings Out: Links
You can provide a callback function to generate links or other special formatting for scripture references in a
callback option. The callback will be given two parameters: the string to print, and the ScripturNum object that can
be used to generate the link. For example:
$a = new ScripturNum('John 3:16'); $f = function (string $s, ScripturNum $sn) { $sc = $sn->getWholeChapters(); return "<a href=\"https://www.esv.org/" . strtolower($sc->toString('abbrev')) . "\">$s</a>"; }; echo $a->toString(['callback' => $f]);
Results in:
<a href="https://www.esv.org/jn3">John 3:16</a>
Database Querying
If you save ScripturNum integers to a given column in a database, you can easily and (relatively) efficiently query them
using bitwise operators. For your convenience, two methods exist that generate conditions that can be added to a WHERE
clause: toSqlExclusive, and toSqlInclusive. Exclusive will include only the results that are
entirely contained within the range. Inclusive will, instead, include all results that overlap with the given range at all,
even if it isn't entirely contained within the range.
For example, for a column named "Scripture", we could use this:
$s = new ScripturNum('Romans 8'); $wheres = $s->toSqlInclusive('Scripture');
At this point, $wheres has a value of:
( (Scripture & 4278190080) = 738197504 AND (Scripture & 16773120) <= 917504 AND (Scripture & 4095) >= 186 )
So, we can insert this into a query:
$queryString = "SELECT * FROM scriptureData WHERE $wheres";
$queryString now has the value:
SELECT * FROM scriptureData WHERE ( (Scripture & 4278190080) = 738197504 AND (Scripture & 16773120) <= 917504 AND (Scripture & 4095) >= 186 )
This can easily be used in most flavors of SQL. Two notes of caution:
- If you want to use prepared statements, beware that you won't be able to re-use prepared statements with different passages.
- Because you can't really prepare statements, beware of SQL injection. In my application, I'm hard-coding the column name (or, at least attaching it to a const), which eliminates this risk. But, that may not be the case for you.
Sorting
In general, sorting by ScripturNum integer value will result in the passages being ordered in the same sequential order as in the Bible. Books take first priority, followed by start chapter, followed by start verse, followed by end chapter, followed by end verse.
Bitwise Operations
The ScripturNum integer is just sequential numbers, bit-shifted to their respective positions. All individual values are zero-indexed. Genesis is book 0. Genesis 1:1 has the integer value of 0.
- The Book index << 24
- Start reference << 12
- End reference << 0
jkrrv/scripturnum 适用场景与选型建议
jkrrv/scripturnum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 124 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「bible」 「scripture」 「Passage」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jkrrv/scripturnum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jkrrv/scripturnum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jkrrv/scripturnum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Parse verse textual representation into book/chapter/verse ranges
Sign in with Passage OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A Fast Markdown Parser extended for Liturgy in PHP.
A simple way to load, read, and present texts from various source text libraries.
Fast, Efficient permission system for controlling access to your website resources
Passkey Complete for PHP - Integrate into your PHP API or service to enable a completely passwordless standalone auth solution with Passage by 1Password.
统计信息
- 总下载量: 124
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-07