PHP-Helpers

Documentation

Packages

Application

Namespaces

macwinnie

Table of Contents

REGEX_DELIMITER  = '/'
regex delimiter that can be used in combination with functions by this helper file
format2regex()  : string
function to translate format string to regex to retrieve entry values
delimiter_preg_quote()  : string
helper function to quote regular delimiter / in RegEx String
getRegexOccurences()  : array<string|int, mixed>
function to fetch RegEx occurences from string / template
getArrayValue()  : mixed
function to retrieve a specific value out of a given (nested) array by dot-joined following keys.
extractArrayValue()  : mixed
extended function `getArrayValue`: will remove value (and key) from array if found
rmValueByKeyTree()  : void
function to remove a value specified by the key-tree from an array
randomString()  : string
function to generate a random string of a given length
env()  : mixed
Gets the value of an environment variable. Supports boolean, empty and null.
val2boolEmptyNull()  : mixed
helper function for env function to translate (string) values into boolean values, empty string or null
rm_recursive()  : bool
Remove a directory and all its content
trimIfString()  : mixed
trim value if is a string
chunkString()  : array<string|int, mixed>
helper function for chunking strings by a bunch of separator characters
decamelize()  : array<string|int, mixed>|string
Invert the camelCase or PascalCase of a string into its parts. Case of words will not be changed!
camelize()  : string
function to camleize a string
pascalize()  : string
function to turn a string into PascalCase
snakify()  : string
snakify a string
str2bool()  : bool
function to convert a boolean string to real boolean
startsWith()  : bool|string
function to check if a string begins with string sequence
endsWith()  : bool|string
function to check if a string ends with string sequence
in_array_recursive()  : bool
recursive in_array function
compactWith()  : array<string|int, mixed>
Basic functionality like `compact` function – but based on an object

Constants

REGEX_DELIMITER

regex delimiter that can be used in combination with functions by this helper file

public string REGEX_DELIMITER = '/'

Functions

format2regex()

function to translate format string to regex to retrieve entry values

format2regex(string $format[, string|null $match = null ][, bool $fullMatch = false ]) : string

sscanf should be sufficient in most situations – but especially if the format string doesn't contain spaces after conversion specifications, it fails. Especially that's the case with LDAP DN definitions, which is why this function was coded.

Usage could be e.g.

<?php

$rGroups  = [
    'DN'  => [ 0 ],
    'uid' => [ 1 ],
    'dc1' => [ 2 ],
    'dc2' => [ 3 ],
    'dc3' => [ 4 ],
];

$dnFormat = 'uid=%s,ou=people,dc=%s,dc=%s,dc=%s';
$regex    = format2regex( $dnFormat, null, true );

$mappings = getRegexOccurences( $regex, 'uid=jdoe,ou=people,dc=compartment,dc=example,dc=com', $rGroups );

echo( json_encode( $mappings, JSON_PRETTY_PRINT ) );

// [
//     {
//         "full": "uid=jdoe,ou=people,dc=compartment,dc=example,dc=com",
//         "length": 51,
//         "offset": 0,
//         "DN": "uid=jdoe,ou=people,dc=compartment,dc=example,dc=com",
//         "uid": "jdoe",
//         "dc1": "compartment",
//         "dc2": "example",
//         "dc3": "com"
//     }
// ]

Parameters
$format : string

format string to be analyzed

$match : string|null = null

RegEx string to replace format parts – defaults to (.*?)

$fullMatch : bool = false

set true for a full match

Tags
todo

handling of '%%' within format string what equals single '%' within a string

Return values
string

RegEx to be used further

delimiter_preg_quote()

helper function to quote regular delimiter / in RegEx String

delimiter_preg_quote(string $string[, string|null $delimiter = null ]) : string
Parameters
$string : string

string to be quoted

$delimiter : string|null = null

delimiter to be used for quoting – defaults to null to use REGEX_DELIMITER defined by this helper file

Return values
string

quoted string

getRegexOccurences()

function to fetch RegEx occurences from string / template

getRegexOccurences(mixed $regex, string $template[, mixed $group_attributes = null ]) : array<string|int, mixed>
Parameters
$regex : mixed
$template : string

the template string

$group_attributes : mixed = null
Return values
array<string|int, mixed>

fetch additional group elements and put it into key; if value is [ 'name' => [ 1,2,3 ] ], the tool will return the first non-empty group out of 1, 2 or 3

getArrayValue()

function to retrieve a specific value out of a given (nested) array by dot-joined following keys.

getArrayValue(array<string|int, mixed> $array, string $dotted_key[, mixed $default = NULL ][, bool $returnKeyTree = False ]) : mixed
Parameters
$array : array<string|int, mixed>

array to be searched

$dotted_key : string

dot-joined key-tree to retrieve the value

$default : mixed = NULL

default value if key-tree not found; defaults to NULL

$returnKeyTree : bool = False

if set True, the return value is an array with two values: keyTree and value. NULL if default has to be returned .

Return values
mixed

extractArrayValue()

extended function `getArrayValue`: will remove value (and key) from array if found

extractArrayValue(array<string|int, mixed> &$array, string $dotted_key[, mixed $default = NULL ]) : mixed
Parameters
$array : array<string|int, mixed>

array to be searched

$dotted_key : string

dot-joined key-tree to retrieve the value

$default : mixed = NULL

default value if key-tree not found; defaults to NULL

Return values
mixed

rmValueByKeyTree()

function to remove a value specified by the key-tree from an array

rmValueByKeyTree(array<string|int, mixed> &$array[, array<string|int, mixed> $keytree = [] ]) : void
Parameters
$array : array<string|int, mixed>

array, the value should be removed from

$keytree : array<string|int, mixed> = []

ordered list of array keys

Return values
void

randomString()

function to generate a random string of a given length

randomString([int $length = 16 ][, mixed $alphabets = NULL ]) : string
Parameters
$length : int = 16

length, the string should have

$alphabets : mixed = NULL

default is NULL, so the random string will consist out of 0-9a-f; if of String type, the random string will be generated out of all characters of that string; if it is a List / Array of strings, the randomized string will – as length is at least as high as the count of given alphabents – contain at least one char out of all those alphabets.

Return values
string

random string

env()

Gets the value of an environment variable. Supports boolean, empty and null.

env(string $key[, mixed $default = NULL ]) : mixed

Will override existing env function if environmental variable MACWINNIE_ENV is true.

Parameters
$key : string
$default : mixed = NULL
Return values
mixed

val2boolEmptyNull()

helper function for env function to translate (string) values into boolean values, empty string or null

val2boolEmptyNull(string $value) : mixed
Parameters
$value : string

value to be transformed

Return values
mixed

transformed value

rm_recursive()

Remove a directory and all its content

rm_recursive(string $path) : bool
Parameters
$path : string

directory path

Return values
bool

true on success, false on error

trimIfString()

trim value if is a string

trimIfString(mixed $var[, string $chars = NULL ]) : mixed
Parameters
$var : mixed

value to be trimmed if string

$chars : string = NULL

defaults to NULL so default of trim function is used

Return values
mixed

trimmed string if string was given – or untouched value

chunkString()

helper function for chunking strings by a bunch of separator characters

chunkString(string $value[, string $chars = ' ' ][, string $normalizeLocale = 'de_DE' ]) : array<string|int, mixed>
Parameters
$value : string

string to be chunked

$chars : string = ' '

characters to chunk by, defaults to space

$normalizeLocale : string = 'de_DE'

defaults to de_DE – have a look on to iconv

Return values
array<string|int, mixed>

list of chunks of string

decamelize()

Invert the camelCase or PascalCase of a string into its parts. Case of words will not be changed!

decamelize(string $camel[, mixed|string $delimiterImplode = ' ' ]) : array<string|int, mixed>|string
Parameters
$camel : string

the camelCase / PascalCase to be split

$delimiterImplode : mixed|string = ' '

defaults to a normal space – if set to NULL, the list of elements, the given string consists of, will be returned instead of a imploded string.

Return values
array<string|int, mixed>|string

if $delimiterImplode is set to NULL, an array of strings is returned – if the value of $delimiterImplode allows to implode the string parts of the camelCase / PascalCase string, that imploded string is returned.

camelize()

function to camleize a string

camelize(string $value[, string $chars = ' ' ][, bool $keepCamel = False ][, string $normalizeLocale = 'de_DE' ]) : string

camelCase is a naming convention in which the first letter of each word in a compound word is capitalized, except for the first word.

Parameters
$value : string

to convert to camelCase

$chars : string = ' '

string containing all characters, to separate the string at

$keepCamel : bool = False

keep camels – defaults to false

$normalizeLocale : string = 'de_DE'

defaults to de_DE – have a look on to iconv documentation since that is relevant for translating umlauts like Ä into AE ...

Return values
string

pascalize()

function to turn a string into PascalCase

pascalize(string $value[, string $chars = ' ' ][, bool $keepCamel = False ][, string $normalizeLocale = 'de_DE' ]) : string

PascalCase is a naming convention in which the first letter of every word in a compound word is capitalized. The first letter is the only thing different to camelCase.

Parameters
$value : string

to convert to PascalCase

$chars : string = ' '

string containing all characters, to separate the string at

$keepCamel : bool = False

keep camels – defaults to false

$normalizeLocale : string = 'de_DE'

defaults to de_DE – have a look on to iconv documentation since that is relevant for translating umlauts like Ä into AE ...

Return values
string

snakify()

snakify a string

snakify(string $value[, string $chars = ' ' ][, string $normalizeLocale = 'de_DE' ]) : string
Parameters
$value : string

to convert to snake_case

$chars : string = ' '

string containing all characters, to separate the string at

$normalizeLocale : string = 'de_DE'

defaults to de_DE – have a look on to iconv documentation since that is relevant for translating umlauts like Ä into AE ...

Return values
string

str2bool()

function to convert a boolean string to real boolean

str2bool(string $check[, mixed $trueStrings = ['1'] ]) : bool
Parameters
$check : string

string to be checked

$trueStrings : mixed = ['1']
Return values
bool

the converted value – true or false

startsWith()

function to check if a string begins with string sequence

startsWith(string $haystack, string $needle[, bool $trim = false ]) : bool|string
Parameters
$haystack : string

the haystack to search in

$needle : string

the string to search for in $haystack

$trim : bool = false

defaults to false

Return values
bool|string

By default, the boolean check result value will be returned. If $trim is set true, the trimmed string will be returned.

endsWith()

function to check if a string ends with string sequence

endsWith(string $haystack, string $needle[, bool $trim = false ]) : bool|string
Parameters
$haystack : string

the haystack to search in

$needle : string

the string to search for in $haystack

$trim : bool = false

defaults to false

Return values
bool|string

By default, the boolean check result value will be returned. If $trim is set true, the trimmed string will be returned.

in_array_recursive()

recursive in_array function

in_array_recursive(mixed $needle, array<string|int, mixed> $haystack[, bool $strict = false ]) : bool
Parameters
$needle : mixed

element to be searched within $haystack

$haystack : array<string|int, mixed>

haystack to be searched in

$strict : bool = false

if set true, a strict check by === is performed instead of the simple check ==

Return values
bool

value found or not

compactWith()

Basic functionality like `compact` function – but based on an object

compactWith(mixed $object, mixed ...$properties) : array<string|int, mixed>
Parameters
$object : mixed

The object, the properties will be fetched from

$properties : mixed

Properties to be fetched

Return values
array<string|int, mixed>

Associative array of property names to property values

Search results