PHP Class Tools\Utility\Utility

Author: Mark Scherer
Show file Open project: dereuromark/cakephp-tools Class Usage Examples

Protected Properties

Property Type Description
$_counterStartTime integer

Public Methods

Method Description
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.

Protected Methods

Method Description
_arrayFlatten ( array $a, array $f = [] ) : array Force-flattens an array and preserves the keys.

Method Details

_arrayFlatten() protected static method

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
return array

arrayFlatten() public static method

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
return array

arrayShiftKeys() public static method

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
return string key

autoPrefixUrl() public static method

Add protocol prefix if necessary (and possible)
public static autoPrefixUrl ( string $url, string | null $prefix = null ) : string
$url string
$prefix string | null
return string

calcElapsedTime() public static method

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
return float

cleanUrl() public static method

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
return string Cleaned Url

countDim() public static method

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
return integer The number of dimensions in $array

deep() public static method

Main deep method
public static deep ( callable $function, mixed $value ) : array | string
$function callable
$value mixed
return array | string

expandList() public static method

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
return array

fileExists() public static method

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
return boolean Success

flattenList() public static method

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
return array

getClientIp() public static method

Get the current IP address.
public static getClientIp ( boolean $safe = true ) : string
$safe boolean
return string IP address

getHeaderFromUrl() public static method

Parse headers from a specific URL content.
public static getHeaderFromUrl ( string $url ) : mixed
$url string
return mixed array of headers or FALSE on failure

getReferer() public static method

Get the current referrer if available.
public static getReferer ( boolean $full = false ) : string
$full boolean (defaults to false and leaves the url untouched)
return string referer (local or foreign)

inArray() public static method

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
return boolean Success

isValidSaveAll() public static method

So we need to call this method inside the modified saveAll() method to return the expected single bool there, too.
Deprecation: Not sure this is useful for CakePHP 3.0
public static isValidSaveAll ( array $array ) : boolean
$array array
return boolean

logicalAnd() public static method

Returns true only if all values are true.
public static logicalAnd ( array $array ) : boolean
$array array
return boolean Result

logicalOr() public static method

Returns true if at least one value is true.
public static logicalOr ( array $array ) : boolean
$array array
return boolean Result

microtime() public static method

Returns microtime as float value (to be subtracted right away)
public static microtime ( integer $precision = 8 ) : float
$precision integer
return float

notEmpty() public static method

More sane !empty() method to not false positive '0' (0 as string) as empty.
public static notEmpty ( mixed $value ) : boolean
$value mixed
return boolean

pregMatch() public static method

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
return array Result

pregMatchAll() public static method

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
return array Result

prettyJson() public static method

Returns pretty JSON
Deprecation: 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
return string

returnElapsedTime() public static method

public static returnElapsedTime ( integer $precision = 8, boolean $restartClock = false ) : float
$precision integer
$restartClock boolean
return float

specialcharsDeep() public static method

Applies h() recursively
public static specialcharsDeep ( mixed $value ) : array | string
$value mixed
return array | string

startClock() public static method

public static startClock ( ) : void
return void

strSplit() public static method

By default this works properly with UTF8 strings.
public static strSplit ( string $str, integer $length = 1 ) : array
$str string
$length integer
return array Result

tokenize() public static method

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
return array

trimDeep() public static method

Trim recursively
public static trimDeep ( mixed $value ) : array | string
$value mixed
return array | string

typeCast() public static method

Convenience function for automatic casting in form methods etc.
public static typeCast ( mixed $value, string $type ) : mixed
$value mixed
$type string
return mixed Safe value for DB query, or NULL if type was not a valid one

urlExists() public static method

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
return boolean Success

Property Details

$_counterStartTime protected static property

protected static int $_counterStartTime
return integer