Download the PHP package nick-jones/php-ucd without Composer
On this page you can find all versions of the php package nick-jones/php-ucd. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nick-jones/php-ucd
More information about nick-jones/php-ucd
Files in nick-jones/php-ucd
Package php-ucd
Short Description Interface into the Unicode Character Database
License MIT
Informations about the package php-ucd
PHP UCD
This project aims to present a PHP interface into the Unicode Character Database (UCD). It provides a means to lookup, filter, and interrogate the metadata & properties of unicode characters.
Installation
You can install this library via composer:
composer require nick-jones/php-ucd
Usage
The primary interface to utilise is UCD\Database
. This provides a number of methods to interrogate
"codepoint assigned" entities (i.e. Character
, NonCharacter
, and Surrogate
instances) that reside within the UCD:
Database::getByCodepoint(Codepoint $codepoint)
- resolves a codepoint assigned entityDatabase::getCharacterByCodepoint(Codepoint $codepoint)
- as above, but will only returnCharacter
instancesDatabase::getByCodepoints(Codepoint\Collection $codepoints)
- resolves multiple codepoint assigned entitiesDatabase::getCodepointsByBlock(Block $block)
- resolves codepoints residing in the supplied blockDatabase::getByBlock(Block $block)
- resolves codepoint assigned entities residing in the supplied blockDatabase::getCodepointsByCategory(GeneralCategory $category)
- resolves codepoints residing in the supplied categoryDatabase::getByCategory(GeneralCategory $category)
- resolves codepoint assigned entities residing in the supplied categoryDatabase::getCodepointsByScript(Script $script)
- resolves codepoints residing in the supplied scriptDatabase::getByScript(Script $script)
- resolves codepoint assigned entities residing in the supplied scriptDatabase::all()
- returns aCollection
instance containing everything assigned a codepoint within the databaseDatabase::onlyCharacters()
- returns aCollection
instance containing onlyCharacter
instancesDatabase::onlyNonCharacters()
- returns aCollection
instance containing onlyNonCharacter
instancesDatabase::onlySurrogates()
- returns aCollection
instance containing onlySurrogate
instances
The UCD\Unicode\Character\Collection
class, returned by a number of methods, provides methods for filtering,
traversal, codepoint extractions, amongst other things.
It is likely that you will want to leverage the default Character\Repository
for resolution of characters, etc, in
which case, calling UCD\Collection::fromDisk()
will give you an instance backed by FileRepository
. You can,
of course, leverage a different Character\Repository
implementation, if you so wish, by providing it to the
constructor of UCD\Database
.
Because this project makes good use of generators, the memory footprint of interrogating the dataset is fairly nominal.
Caveats
As of Unicode 8.0 there are > 260,000 items assigned codepoints. Reading, filtering and traversing all of these will take a few seconds. As such, if your intention is to identify items by filtering rules, you would be well advised to cache the output in some suitable form (e.g. build a regex, or PHP array of codepoints) which can then be interrogated, rather than always returning to filter and traverse the database. If your intention is to perform lookup by codepoint, then it is no problem to call into this library when and as required, as this is an efficient operation.
Examples
Manual Filtering + Traversal
Say you wish to dump all characters that hold a numeric property and reside outside of the Basic Latin (ASCII) block.
You could simply leverage the Collection::filterWith(callable $filter)
method to interrogate the properties of each
Character
instance. You could then perhaps dump their latin equivalent representation by calling ::getNumber()
on
the Numericity
property. For example:
Codepoint Lookup
Locating an individual character by its codepoint value is trivial:
It is just as trivial to interrogate multiple codepoints. For example, you could print the name of every codepoint residing within a string:
Factory methods are available on the Codepoint
and Codepoint\Collection
classes to construct instances based on UTF-8,
UTF-16BE, UTF-16LE, UTF-32BE and UTF-32LE encoded character(s).
Regex Building
The library provides a means to build regular expression characters classes based codepoints that have been extracted or aggregated from a collection of characters. For example, if you wanted to produce a regular expression that matched numeric flavour bengali characters, then you could use something along the lines of:
Map Building
This library can be used for building maps for various purposes. One such example is building a lowercase → uppercase character map. This is relatively simple to achieve; interrogate the properties of each character to check whether a mapping to a different character exists - if one does, print it out in PHP syntax:
This can then be leveraged as follow:
Executable
The primary intention of this project is to act as a library, however a small utility command is available for testing
and database generation/manipulation purposes. bin/ucd search <codepoint>
will dump character information, and
bin/ucd repository-transfer <from> <to>
will transfer characters from one repository implementation to another.
Please run bin/ucd
for more detailed help.
Properties
The intention of the most interesting of the available character properties, as described in Unicode Standard Annex #44, Unicode Character Database - Properties, available for interrogation. There are, however, a good quantity of them, so this remains work in progress. The following are currently covered:
- [x] Name
- [x] Block
- [x] Age
- [x] General Category
- [x] Numeric Value
- [x] Numeric Type
- [x] Normalization
- [x] Canonical Combining Class
- [x] Decomposition Mapping
- [x] Decomposition Type
- [x] Join Control
- [x] Joining Group
- [x] Joining Type
- [x] Bidi Class
- [x] Bidi Control
- [x] Bidi Mirrored
- [x] Bidi Mirroring Glyph
- [x] Bidi Paired Bracket
- [x] Bidi Paired Bracket Type
Tests
PhpSpec class specifications and PHPUnit backed integration tests are
provided. The easiest way to run them is via the Makefile; simply run make test
.
All versions of php-ucd with dependencies
ext-iconv Version *
ext-zlib Version *
psr/log Version ^1.0
judev/php-intervaltree Version ^2.0