PHP Class lithium\util\String

Datei anzeigen Open project: unionofrad/lithium Class Usage Examples

Protected Properties

Property Type Description
$_source Closure A closure which, given a number of bytes, returns that amount of random bytes.

Public Methods

Method Description
clean ( string $str, array $options = [] ) : string Cleans up a String::insert() formatted string with given $options depending on the 'clean' option. The goal of this function is to replace all whitespace and unneeded mark-up around place-holders that did not get replaced by String::insert().
compare ( string $known, string $user ) : boolean Compares two strings in constant time to prevent timing attacks.
extract ( string $regex, string $str, integer $index ) : mixed Extract a part of a string based on a regular expression $regex.
hash ( string $string, array $options = [] ) : string Uses PHP's hashing functions to create a hash of the string provided, using the options specified. The default hash algorithm is SHA-512.
insert ( string $str, array $data, array $options = [] ) : string Replaces variable placeholders inside a string with any given data. Each key in the $data array corresponds to a variable placeholder name in $str.
random ( integer $bytes, array $options = [] ) : string Generates random bytes for use in UUIDs and password salts, using (when available) a cryptographically strong random number generator.
tokenize ( string $data, array $options = [] ) : array Tokenizes a string using $options['separator'], ignoring any instances of $options['separator'] that appear between $options['leftBound'] and $options['rightBound'].
uuid ( ) : string Generates an RFC 4122-compliant version 4 UUID.

Protected Methods

Method Description
_source ( ) : Closure Initializes String::$_source using the best available random number generator.

Method Details

_source() protected static method

When available, /dev/urandom and COM gets used on *nix and Windows systems, respectively. If all else fails, a Mersenne Twister gets used. (Strictly speaking, this fallback is inadequate, but good enough.) Note: Users restricting path access through the open_basedir INI setting, will need to include /dev/urandom into the list of allowed paths, as this method might read from /dev/urandom.
See also: lithium\util\String::$_source
protected static _source ( ) : Closure
return Closure Returns a closure containing a random number generator.

clean() public static method

Cleans up a String::insert() formatted string with given $options depending on the 'clean' option. The goal of this function is to replace all whitespace and unneeded mark-up around place-holders that did not get replaced by String::insert().
public static clean ( string $str, array $options = [] ) : string
$str string The string to clean.
$options array Available options are: - `'after'`: characters marking the end of targeted substring. - `'andText'`: (defaults to `true`). - `'before'`: characters marking the start of targeted substring. - `'clean'`: `true` or an array of clean options: - `'gap'`: Regular expression matching gaps. - `'method'`: Either `'text'` or `'html'` (defaults to `'text'`). - `'replacement'`: String to use for cleaned substrings (defaults to `''`). - `'word'`: Regular expression matching words.
return string The cleaned string.

compare() public static method

To successfully mitigate timing attacks and not leak the actual length of the known string, it is important that _both provided strings have the same length_ and that the _user-supplied string is passed as a second parameter_ rather than first. This function has the same signature and behavior as the native hash_equals() function and will use that function if available (PHP >= 5.6). An E_USER_WARNING will be emitted when either of the supplied parameters is not a string.
public static compare ( string $known, string $user ) : boolean
$known string The string of known length to compare against.
$user string The user-supplied string.
return boolean Returns a boolean indicating whether the two strings are equal.

extract() public static method

Extract a part of a string based on a regular expression $regex.
public static extract ( string $regex, string $str, integer $index ) : mixed
$regex string The regular expression to use.
$str string The string to run the extraction on.
$index integer The number of the part to return based on the regex.
return mixed

hash() public static method

Uses PHP's hashing functions to create a hash of the string provided, using the options specified. The default hash algorithm is SHA-512.
public static hash ( string $string, array $options = [] ) : string
$string string The string to hash.
$options array Supported options: - `'type'` _string_: Any valid hashing algorithm. See the `hash_algos()` function to determine which are available on your system. - `'salt'` _string_: A _salt_ value which, if specified, will be prepended to the string. - `'key'` _string_: If specified `hash_hmac()` will be used to hash the string, instead of `hash()`, with `'key'` being used as the message key. - `'raw'` _boolean_: If `true`, outputs the raw binary result of the hash operation. Defaults to `false`.
return string Returns a hashed string.

insert() public static method

Usage: String::insert( 'My name is {:name} and I am {:age} years old.', array('name' => 'Bob', 'age' => '65') ); // returns 'My name is Bob and I am 65 years old.' Please note that optimization have applied to this method and parts of the code may look like it can refactored or removed but in fact this is part of the applied optimization. Please check the history for this section of code before refactoring
public static insert ( string $str, array $data, array $options = [] ) : string
$str string A string containing variable place-holders.
$data array A key, value array where each key stands for a place-holder variable name to be replaced with value.
$options array Available options are: - `'after'`: The character or string after the name of the variable place-holder (defaults to `}`). - `'before'`: The character or string in front of the name of the variable place-holder (defaults to `'{:'`). - `'clean'`: A boolean or array with instructions for `String::clean()`. - `'escape'`: The character or string used to escape the before character or string (defaults to `'\'`). - `'format'`: A regular expression to use for matching variable place-holders (defaults to `'/(?
return string

random() public static method

$bits = String::random(8); // 64 bits $hex = bin2hex($bits); // [0-9a-f]+ Optionally base64-encodes the resulting random string per the following. The alphabet used by base64_encode() is different than the one we should be using. When considering the meaty part of the resulting string, however, a bijection allows to go the from one to another. Given that we're working on random bytes, we can use safely use base64_encode() without losing any entropy.
public static random ( integer $bytes, array $options = [] ) : string
$bytes integer The number of random bytes to generate.
$options array The options used when generating random bytes: - `'encode'` _integer_: If specified, and set to `String::ENCODE_BASE_64`, the resulting value will be base64-encoded, per the notes above.
return string Returns a string of random bytes.

tokenize() public static method

Tokenizes a string using $options['separator'], ignoring any instances of $options['separator'] that appear between $options['leftBound'] and $options['rightBound'].
public static tokenize ( string $data, array $options = [] ) : array
$data string The data to tokenize.
$options array Options to use when tokenizing: -`'separator'` _string_: The token to split the data on. -`'leftBound'` _string_: Left scope-enclosing boundary. -`'rightBound'` _string_: Right scope-enclosing boundary.
return array Returns an array of tokens.

uuid() public static method

Generates an RFC 4122-compliant version 4 UUID.
public static uuid ( ) : string
return string The string representation of an RFC 4122-compliant, version 4 UUID.

Property Details

$_source protected_oe static_oe property

A closure which, given a number of bytes, returns that amount of random bytes.
protected static Closure $_source
return Closure