PHP Класс Tools\Utility\Utility

Автор: Mark Scherer
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$_counterStartTime integer

Открытые методы

Метод Описание
arrayFlatten ( array $array, boolean $preserveKeys = false ) : array Force-flattens an array.
arrayShiftKeys ( array &$array ) : string Similar to array_shift but on the keys of the array like array_shift() only for keys and not values
autoPrefixUrl ( string $url, string | null $prefix = null ) : string Add protocol prefix if necessary (and possible)
calcElapsedTime ( integer $start, integer $end, integer $precision = 8 ) : float Returns microtime as float value (to be subtracted right away)
cleanUrl ( string $url, boolean $headerRedirect = false ) : string Remove unnessary stuff + add http:// for external urls TODO: protocol to lower!
countDim ( array $array, boolean $all = false, integer $count ) : integer Counts the dimensions of an array. If $all is set to false (which is the default) it will only consider the dimension of the first element in the array.
deep ( callable $function, mixed $value ) : array | string Main deep method
expandList ( array $data, string $separator = '.', string | null $undefinedKey = null ) : array Expands the values of an array of strings into a deep array.
fileExists ( string $file, string $pattern = '~^https?://~i' ) : boolean A more robust wrapper around for file_exists() which easily fails to return true for existent remote files.
flattenList ( array $data, string $separator = '.' ) : array Flattens a deep array into an array of strings.
getClientIp ( boolean $safe = true ) : string Get the current IP address.
getHeaderFromUrl ( string $url ) : mixed Parse headers from a specific URL content.
getReferer ( boolean $full = false ) : string Get the current referrer if available.
inArray ( mixed $needle, array $haystack ) : boolean Clean implementation of inArray to avoid false positives.
isValidSaveAll ( array $array ) : boolean On non-transaction db connections it will return a deep array of bools instead of bool.
logicalAnd ( array $array ) : boolean Returns true only if all values are true.
logicalOr ( array $array ) : boolean Returns true if at least one value is true.
microtime ( integer $precision = 8 ) : float Returns microtime as float value (to be subtracted right away)
notEmpty ( mixed $value ) : boolean More sane !empty() method to not false positive '0' (0 as string) as empty.
pregMatch ( string $pattern, string $subject, integer | null $flags = null, integer | null $offset = null ) : array Multibyte analogue of preg_match() function. Only that this returns the result.
pregMatchAll ( string $pattern, string $subject, integer $flags = PREG_SET_ORDER, integer | null $offset = null ) : array Multibyte analogue of preg_match_all() function. Only that this returns the result.
prettyJson ( string $json, string $indString = " " ) : string Returns pretty JSON
returnElapsedTime ( integer $precision = 8, boolean $restartClock = false ) : float
specialcharsDeep ( mixed $value ) : array | string Applies h() recursively
startClock ( ) : void
strSplit ( string $str, integer $length = 1 ) : array Multibyte analogue of str_split() function.
tokenize ( string $data, string $separator = ',', array $options = [] ) : array Tokenizes a string using $separator.
trimDeep ( mixed $value ) : array | string Trim recursively
typeCast ( mixed $value, string $type ) : mixed Convenience function for automatic casting in form methods etc.
urlExists ( string $url ) : boolean file_exists() does not always work with URLs.

Защищенные методы

Метод Описание
_arrayFlatten ( array $a, array $f = [] ) : array Force-flattens an array and preserves the keys.

Описание методов

_arrayFlatten() защищенный статический Метод

Careful with this method. It can lose information. The keys will not be changed, thus possibly overwrite each other. TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
protected static _arrayFlatten ( array $a, array $f = [] ) : array
$a array
$f array
Результат array

arrayFlatten() публичный статический Метод

Careful with this method. It can lose information. The keys will not be changed, thus possibly overwrite each other. TODO: check if it can be replace by Hash::flatten() or Utility::flatten().
public static arrayFlatten ( array $array, boolean $preserveKeys = false ) : array
$array array Array to flatten
$preserveKeys boolean
Результат array

arrayShiftKeys() публичный статический Метод

Similar to array_shift but on the keys of the array like array_shift() only for keys and not values
public static arrayShiftKeys ( array &$array ) : string
$array array keyValuePairs
Результат string key

autoPrefixUrl() публичный статический Метод

Add protocol prefix if necessary (and possible)
public static autoPrefixUrl ( string $url, string | null $prefix = null ) : string
$url string
$prefix string | null
Результат string

calcElapsedTime() публичный статический Метод

Returns microtime as float value (to be subtracted right away)
public static calcElapsedTime ( integer $start, integer $end, integer $precision = 8 ) : float
$start integer
$end integer
$precision integer
Результат float

cleanUrl() публичный статический Метод

Remove unnessary stuff + add http:// for external urls TODO: protocol to lower!
public static cleanUrl ( string $url, boolean $headerRedirect = false ) : string
$url string
$headerRedirect boolean
Результат string Cleaned Url

countDim() публичный статический Метод

Counts the dimensions of an array. If $all is set to false (which is the default) it will only consider the dimension of the first element in the array.
public static countDim ( array $array, boolean $all = false, integer $count ) : integer
$array array Array to count dimensions on
$all boolean Set to true to count the dimension considering all elements in array
$count integer Start the dimension count at this number
Результат integer The number of dimensions in $array

deep() публичный статический Метод

Main deep method
public static deep ( callable $function, mixed $value ) : array | string
$function callable
$value mixed
Результат array | string

expandList() публичный статический Метод

Opposite of flattenList(). It needs at least a single separator to be present in all values as the key would otherwise be undefined. If data can contain such key-less rows, use $undefinedKey to avoid an exception being thrown. But it will effectivly collide with other values in that same key then. So Some.Deep.Value becomes array('Some' => array('Deep' => array('Value'))).
public static expandList ( array $data, string $separator = '.', string | null $undefinedKey = null ) : array
$data array
$separator string
$undefinedKey string | null
Результат array

fileExists() публичный статический Метод

Per default it allows http/https images to be looked up via urlExists() for a better result.
public static fileExists ( string $file, string $pattern = '~^https?://~i' ) : boolean
$file string File
$pattern string
Результат boolean Success

flattenList() публичный статический Метод

Opposite of expandList(). So array('Some' => array('Deep' => array('Value'))) becomes Some.Deep.Value. Note that primarily only string should be used. However, boolean values are casted to int and thus both boolean and integer values also supported.
public static flattenList ( array $data, string $separator = '.' ) : array
$data array
$separator string
Результат array

getClientIp() публичный статический Метод

Get the current IP address.
public static getClientIp ( boolean $safe = true ) : string
$safe boolean
Результат string IP address

getHeaderFromUrl() публичный статический Метод

Parse headers from a specific URL content.
public static getHeaderFromUrl ( string $url ) : mixed
$url string
Результат mixed array of headers or FALSE on failure

getReferer() публичный статический Метод

Get the current referrer if available.
public static getReferer ( boolean $full = false ) : string
$full boolean (defaults to false and leaves the url untouched)
Результат string referer (local or foreign)

inArray() публичный статический Метод

in_array itself has some PHP flaws regarding cross-type comparison: - in_array('50x', array(40, 50, 60)) would be true! - in_array(50, array('40x', '50x', '60x')) would be true!
public static inArray ( mixed $needle, array $haystack ) : boolean
$needle mixed
$haystack array
Результат boolean Success

isValidSaveAll() публичный статический Метод

So we need to call this method inside the modified saveAll() method to return the expected single bool there, too.
Устаревший: Not sure this is useful for CakePHP 3.0
public static isValidSaveAll ( array $array ) : boolean
$array array
Результат boolean

logicalAnd() публичный статический Метод

Returns true only if all values are true.
public static logicalAnd ( array $array ) : boolean
$array array
Результат boolean Result

logicalOr() публичный статический Метод

Returns true if at least one value is true.
public static logicalOr ( array $array ) : boolean
$array array
Результат boolean Result

microtime() публичный статический Метод

Returns microtime as float value (to be subtracted right away)
public static microtime ( integer $precision = 8 ) : float
$precision integer
Результат float

notEmpty() публичный статический Метод

More sane !empty() method to not false positive '0' (0 as string) as empty.
public static notEmpty ( mixed $value ) : boolean
$value mixed
Результат boolean

pregMatch() публичный статический Метод

By default this works properly with UTF8 strings. Do not forget to use preg_quote() first on strings that could potentially contain unescaped characters. Note that you still need to add the u modifier (for UTF8) to your pattern yourself. Example: /some(.*)pattern/u
public static pregMatch ( string $pattern, string $subject, integer | null $flags = null, integer | null $offset = null ) : array
$pattern string The pattern to use.
$subject string The string to match.
$flags integer | null
$offset integer | null
Результат array Result

pregMatchAll() публичный статический Метод

By default this works properly with UTF8 strings. Do not forget to use preg_quote() first on strings that could potentially contain unescaped characters. Note that you still need to add the u modifier (for UTF8) to your pattern yourself. Example: /some(.*)pattern/u
public static pregMatchAll ( string $pattern, string $subject, integer $flags = PREG_SET_ORDER, integer | null $offset = null ) : array
$pattern string The pattern to use.
$subject string The string to match.
$flags integer
$offset integer | null
Результат array Result

prettyJson() публичный статический Метод

Returns pretty JSON
Устаревший: Now there is a JSON_PRETTY_PRINT option available on json_encode()
public static prettyJson ( string $json, string $indString = " " ) : string
$json string The original JSON string
$indString string The string to indent with
Результат string

returnElapsedTime() публичный статический Метод

public static returnElapsedTime ( integer $precision = 8, boolean $restartClock = false ) : float
$precision integer
$restartClock boolean
Результат float

specialcharsDeep() публичный статический Метод

Applies h() recursively
public static specialcharsDeep ( mixed $value ) : array | string
$value mixed
Результат array | string

startClock() публичный статический Метод

public static startClock ( ) : void
Результат void

strSplit() публичный статический Метод

By default this works properly with UTF8 strings.
public static strSplit ( string $str, integer $length = 1 ) : array
$str string
$length integer
Результат array Result

tokenize() публичный статический Метод

Options - clean: true/false (defaults to true and removes empty tokens and whitespace)
public static tokenize ( string $data, string $separator = ',', array $options = [] ) : array
$data string The data to tokenize
$separator string The token to split the data on.
$options array
Результат array

trimDeep() публичный статический Метод

Trim recursively
public static trimDeep ( mixed $value ) : array | string
$value mixed
Результат array | string

typeCast() публичный статический Метод

Convenience function for automatic casting in form methods etc.
public static typeCast ( mixed $value, string $type ) : mixed
$value mixed
$type string
Результат mixed Safe value for DB query, or NULL if type was not a valid one

urlExists() публичный статический Метод

So if you check on strpos(http) === 0 you can use this to check for URLs instead.
public static urlExists ( string $url ) : boolean
$url string Absolute URL
Результат boolean Success

Описание свойств

$_counterStartTime защищенное статическое свойство

protected static int $_counterStartTime
Результат integer