spinitron/precis
Composer 安装命令:
composer require spinitron/precis
包简介
Implements PRECIS Framework, IDNA Bidi Rule, Unicode Bidi properties
README 文档
README
Summary
PRECIS, "Preparation, Enforcement, and Comparison of Internationalized Strings", is a framework for handling Unicode strings representing things like network addresses, identifiers and passwords, and nicknames.
In PRECIS terms, a client (e.g. web browser) prepares a string (e.g. username, password) before sending it over the network. A server (e.g. a PHP web app) enforces specific rules (e.g. checking for disallowed characters, case mapping, etc.) on the string before accepting and using it. Enforcement involves an accept/reject decision and may involve changing the string using a PRECIS profile-specific kind of normalization. When comparing two strings, both are enforced before comparion. If a hash function is used in the comparison (e.g. for a password), enforcement comes before hashing and comparison.
The PRECIS RFCs standardize such preparation and enforcement (obsoleting older standards) and this package provides a PHP implementation.
Functionality
The Precis class provides static methods to:
-
test strings against the strings classes defined in PRECIS Framework RFC 8264
- IdentifierClass
- FreeformClass
-
prepare and enforce strings according to the profiles
- UsernameCaseMapped RFC 8265 Section 3.3
- UsernameCasePreserved RFC 8265 Section 3.4
- OpaqueString (used for passwords etc.) RFC 8265 Section 4.2
- Nickname RFC 8266
In order to implement these, certain Unicode functionality is required that was absent in PHP before PHP 7.0 introduced IntlChar. The package has another class as a stop-gap.
-
Bidiprovides static methods for:- Getting the Unicode Bidi property for a character
- Applying the IDNA Bidi Rule
(Note: The CaseFold class in v0.1.1 of this package was removed after PRECIS removed it in revised RFCs.)
Methods
All public methods are static.
// test for PRECIS string classes public static function isIdentifier(string $string): bool public static function isFreeform(string $string): bool // prepare for PRECIS profiles public static function prepareUsernameCaseMapped(string $string): string|bool public static function prepareUsernameCasePreserved(string $string): string|bool public static function prepareOpaqueString(string $string): string|bool public static function prepareNickname(string $string): string|bool // enforce PRECIS profiles public static function enforceUsernameCaseMapped(string $string): string|bool public static function enforceUsernameCasePreserved(string $string): string|bool public static function enforceOpaqueString(string $string): string|bool public static function enforceNickname(string $string): string|bool
This package also has some general Unicode utilities that were required before PHP 7 introduced the IntlChar class. These remain as we maintain PHP 5.4 compat for now. And we contunue to use mbstring for the Unicode toLowerCase() operation.
// general UTF-8 character utilities public static function utf8chr(int $ord): string|null public static function utf8ord(string $string, int $pos = 0): int public static function codePoint2utf8(string $codePoint): string|null|false public static function utf82CodePoint(string $string, int $pos = 0, string $style = 'U+'): string // Unicode Bidi class and IDNA Bidi rule Bidi::getClass(string $char): string public static function rule(string $string): bool
Data
The Bidi class has its own "data trait" that contains
the relevant data from the Unicode Character Database.
They are PHP classes in order to exploit opcode caches.
They are traits not because I plan
to reuse them elsewhere but for convenience. First, because they are automatically
generated from the UCD and, second, because it's easier to work on the corresponding
methods without the big array literals in the same editor.
The generators for the data traits are in the data directory. They download the
relevant UCD files and write the PHP trait classes to standard output. So you can
regenerate them, for example, as follows (assuming php is in your path):
cd path/to/precis/project/dir
php data/generateBidi.php > BidiDataTrait.php
php data/generateCaseFold.php > CaseFoldDataTrait.php
Unit tests
The Precis tests are incomplete because I could not find test vectors for PRECIS string
classes or profiles or for the Bidi Rule.
The following are relatively well tested:
- UTF-8 utility functions are tested on every Unicode code point
Bidi::getClass(). NOTE: this test downloads UnicideData.txt from UCD (1.5 MB)- The CONTEXTO and CONTEXTJ rules
Precis::getPrecisProperty()
The DerivedPropertyValueTest uses the table of derived property values from IANA that's stuck on Unicode 6.3.0. Hence we ignore tests where the IANA table says UNASSIGNED and Precis says something else.
Copyright and license
- Copyright (c) 2015-2018 Spinitron LLC
- ISC license https://opensource.org/licenses/ISC
spinitron/precis 适用场景与选型建议
spinitron/precis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.38k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「i18n」 「password」 「username」 「unicode」 「idn」 「precis」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spinitron/precis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spinitron/precis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spinitron/precis 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
A custom URL rule class for Yii 2 which allows to create translated URL rules
The PHP class PasswordGenerator serves as a password generator to create memorable passwords like the keychain of macOS ≤ 10.14 did.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Adds username authentication and username or email login to a silverstripe install
High-level cryptographic primitives and security utilities for Maatify systems including password hashing, reversible encryption, HKDF key derivation, and key rotation.
统计信息
- 总下载量: 1.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 31
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: ISC
- 更新时间: 2015-09-19