ENCODING
ENCODING
Define a single character.
Unlike other classes of Bah library, this class is not equivalent of a PHP primitive type, but it can be very useful to deal with characters and their UTF-8 properties.
So, you can tests some characters properties, such as "is ti punctuation?"; "is it lowercase?"; "is it right to left character?" and many others…
You can get all characters from family of given one (from same unicode block), you can get unicode code point and block name for each character.
__construct(mixed $char)
Instanciates new character.
Creates new character object using 3 different ways.
You can instanciate it using:
\Malenki\Bah\N
object. In this case, given object is read to be
the UTF-8 code point of the character. So, The Bah Number must be in
[0, 0x10FFFF]
range to be valid.Examples:
// Unicode code point way
$n = new N(948);
$c = new C($n);
echo $c; //'δ'
// XML entity way
$c = new C('é');
echo $c; // 'é'
// direct way
$c = new C('z');
echo $c; // 'z'
mixed | $char | A string-like or |
If given Bah Number is not into the valid UTF-8 range.
If given HTML entity is not valid.
If given char is not valid UTF-8 char.
encodings() : \Malenki\Bah\A
Lists all available encodings.
List all available encodings, as collection of \Malenki\Bah\S
objects.
mustBeStringOrScalar(mixed $arg, string $arg_name) : void
Checks whether given value is string-like or scalar type.
Value must be of string-like type (string or object having
__toString()
method) or a basic scalar type.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeString(mixed $arg, string $arg_name) : void
Checks whether given value is string-like type.
A string-like value is string or object having __toString()
method.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeInteger(mixed $arg, string $arg_name) : void
Checks whether given value is integer-like type.
An integer-like value is integer or \Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeFloat(mixed $arg, string $arg_name) : void
Checks whether given value is float-like type.
A float-like value is float or \Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeDouble(mixed $arg, string $arg_name) : void
Checks whether given value is double-like type.
A double-like value is string or \Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeNumeric(mixed $arg, string $arg_name) : void
Checks whether given value is numeric-like type.
A numeric-like value is numeric (integer, float or double) or
\Malenki\Bah\N
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeArray(mixed $arg, string $arg_name) : void
Checks whether given value is an array-like type.
A array-like value is an array or \Malenki\Bah\A
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeHash(mixed $arg, string $arg_name) : void
Checks whether given value is a hash-like type.
A hash-like value is an array having named-index or \Malenki\Bah\H
object.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeArrayOrHash(mixed $arg, string $arg_name) : void
Checks whether given value is an array-like or hash-like type.
The aim of this method is only to raise \InvalidArgumentException
if
given value has not the good type.
mixed | $arg | The value to test. |
string | $arg_name | Optional name of the value, used into exception’s message. |
If value’s type is not valid
mustBeCallable(mixed $arg, mixed $arg_name) : void
Checks whether the given argument is callable.
This is usefull to test is an argument can be call as a function. If
this argument is not callable, then this raises an
\InvalidArgumentException
.
mixed | $arg | The value to test |
mixed | $arg_name | Optional name of the value, used into error message |
If arg is not callable
_integer() : integer
Casts current character to integer primitive type.
If current character is digit form 0 to 9, then it can be casted to integer php primitive type.
Example:
$c = new C('5');
var_dump($c->integer); // int(5)
If current character is not numeric.
_trans() : \Malenki\Bah\S
Transliterate current character.
Current character is transliterated to \Malenki\Bah\S
object.
Example:
$c = new C('ç');
echo $c->trans; // 'c'
Note: Returned object is not \Malenki\Bah\C
object, but
\Malenki\Bah\S
object, because translierated character can give more
than one new characters.
If Intl PHP extension is not available
If Intl is available, but PHP version is less than 5.4.0. Transliterating feature is available since PHP 5.4.0.
_upper() : \Malenki\Bah\C
Transforms to uppercase.
Returns new character object translated to uppercase, if any. If no uppercase exists, returns same character.
Example:
$c = new C('œ');
echo $c->upper; // 'Œ'
$c = new C('9');
echo $c->upper; // '9'
_lower() : \Malenki\Bah\C
Transforms to lowercase.
Returns new character object translated to lowercase, if any. If no lowercase exists, returns same character.
Example:
$c = new C('A');
echo $c->lower; // 'a'
$c = new C('9');
echo $c->lower; // '9'
_isPrivateUse() : boolean
Checks if character is for private use only.
Checks whether current character must be use only for private uses or not.
Some UTF-8 characters can be used as you want, for example to have custom identicon. An example of private use is FontAwesome.
Example:
$c = new C(new N(0xe62e));
var_dump($c->is_private_use); // true
_isSeparator() : boolean
Tests if character is separator.
Tests whether current character is a separator, like no-break-space, space, ideographic space and so on.
Example:
$c = new C(' ');
var_dump($c->is_separator); // true
$c = new C("\t");
var_dump($c->is_separator); // false
_isPunctuation() : boolean
Tests if character is a punctuation.
Tests whether current character is punctuation or not, like comma, dot, parentethis and so on…
Examples:
$c = new C('.');
var_dump($c->is_punctuation); // true
$c = new C(',');
var_dump($c->is_punctuation); // true
$c = new C('…');
var_dump($c->is_punctuation); // true
$c = new C('–');
var_dump($c->is_punctuation); // true
$c = new C('。');
var_dump($c->is_punctuation); // true
$c = new C('【');
var_dump($c->is_punctuation); // true
_isLowerCase() : boolean
Tests whether current character is in lower case.
This is runtime part of some magic getters to test whether current character is in lower case or not.
Example:
$c = new C('a');
var_dump($c->is_lower_case); // true
var_dump($c->is_lower); // true
$c = new C('A');
var_dump($c->is_lower_case); // false
var_dump($c->is_lower); // false
Note: Not to be confused with C::lower
! This convert current
character to another character into lower case.
_isUpperCase() : boolean
Tests whether current character is in upper case.
If current characters can be in lower or uppercase, then this tests if current character is upper case.
Example:
$c = new C('À');
var_dump($c->is_upper_case); // true
var_dump($c->is_upper); // true
$c = new C('à');
var_dump($c->is_upper_case); // false
var_dump($c->is_upper); // false
_isAscii() : boolean
Tests if current character is from ASCII set or not.
UTF-8 characters are compatible with ASCII characters. ASCII characters
are defined into the range [0x0, 0x7F]
.
Example:
$c = new C('a');
var_dump($c->is_ascii); // true
$c = new C('œ');
var_dump($c->is_ascii); // false
_block() : \Malenki\Bah\S
Gets unicode block’s name
Returns unicode block’s name of current characters as a \Malenki\Bah\S
object.
Example:
$c = new C('a');
$c->block; // 'Basic Latin'
This methods is the runtime part of magic getter \Malenki\Bah\C::$block
If current character is not into unicode block
_family() : \Malenki\Bah\A
For current character, gets all characters of its unicode block.
All characters of the same block of current character are returned into
an \Malenki\Bah\A
collection of \Malenki\Bah\C
objects.
Example:
$c = new \Malenki\Bah\C(new N(0x2191));
echo $c->family->join(',');
// print '←,↑,→,↓,↔,↕,↖,↗,↘,↙,↚,↛,↜,↝,↞,↟,↠,↡,↢,↣,↤,↥,↦,↧,↨,↩,↪,↫,
// ↬,↭,↮,↯,↰,↱,↲,↳,↴,↵,↶,↷,↸,↹,↺,↻,↼,↽,↾,↿,⇀,⇁,⇂,⇃,⇄,⇅,⇆,⇇,⇈,⇉,⇊,⇋,
//⇌,⇍,⇎,⇏,⇐,⇑,⇒,⇓,⇔,⇕,⇖,⇗,⇘,⇙,⇚,⇛,⇜,⇝,⇞,⇟,⇠,⇡,⇢,⇣,⇤
This methods is the runtime part of magic getter \Malenki\Bah\C::$family
If current character is not into unicode block
_unicode() : \Malenki\Bah\N
Get unicode code point for the current character.
Returns unicode code point of current character as \Malenki\Bah\N
object, so you can deal with different numeric systems.
This is runtime part of magic getter having same name.
Example:
$c = new C('€');
echo $c->unicode; // '8364'
echo $c->unicode->bin; // '10000010101100'
echo $c->unicode->oct; // '20254'
echo $c->unicode->hex; // '20ac'
Note: To get UTF8 bytes, use C::$bytes
instead.
$c = new C('€');
echo $c->bytes->join(', '); // 226, 130, 172
_rtl() : boolean
Checks if current character is RTL.
Checks whether current character is RTL, understand Right To Left, like you can see into Arabic or Hebrew language for examples (but other langauges exist too).
Example:
$c = new C('ش');
var_dump($c->rtl); // true
var_dump($c->is_rtl); // true
var_dump($c->is_right_to_left); // true
var_dump($c->right_to_left); // true
$c = new C('a');
var_dump($c->rtl); // false
var_dump($c->is_rtl); // false
var_dump($c->is_right_to_left); // false
var_dump($c->right_to_left); // false
_ltr() : boolean
Checks if current character is LTR.
Checks whether current character is LTR, understand Left To Right, like you can see into English, French, Dutch and many others.
This is the opposite of \Malenki\Bah\C::_rtl()
method.
This method is runtime par of some magic getters, these have the same name as RTL version, but with left in place of right and vice versa ;-)
Example:
$c = new C('ش');
var_dump($c->ltr); // false
var_dump($c->is_ltr); // false
var_dump($c->left_to_right); // false
var_dump($c->is_left_to_right); // false
$c = new C('a');
var_dump($c->ltr); // true
var_dump($c->is_ltr); // true
var_dump($c->left_to_right); // true
var_dump($c->is_left_to_right); // true