berlioz / helpers
Many PHP functions used in the Berlioz framework, which you can use in your developments.
Requires
- php: ^7.3 || ^8.0
- ext-intl: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^9.3
Suggests
- ext-gd: Allows to use image helpers functions
- ext-gmp: Allows to convert IPv6 addresses to/from their numeric representation
- ext-simplexml: Allows to use XML functions
Conflicts
README
Many PHP functions used in the Berlioz framework, which you can use in your developments.
Array
All array path methods support the following path formats:
-
Dot notation:
foo.bar.baz -
Bracket notation:
foo[bar][baz] -
JSON Pointer (RFC 6901):
/foo/bar/baz -
b_array_is_list(array $array): boolIs sequential array?
-
b_array_column(array $array, int|string|\Closure|null $column_key, int|string|\Closure|null $index_key = null): arrayGet values from a single column in the input array.
Difference between native array_column() and b_array_column() is that b_array_column() accept a \Closure in keys arguments.
-
b_array_merge_recursive(array $arraySrc, array ...$arrays): arrayMerge two or more arrays recursively.
Difference between native array_merge_recursive() is that b_array_merge_recursive() do not merge strings values into an array.
-
b_array_traverse_exists(&$mixed, string $path): boolTraverse array with path and return if path exists.
-
b_array_traverse_get(&$mixed, string $path, $default = null): mixed|nullTraverse array with path and get value.
-
b_array_traverse_set(&$mixed, string $path, $value): boolTraverse array with path and set value.
-
b_array_traverse_unset(&$mixed, string $path): boolTraverse array with path and unset value.
-
b_array_only(array $array, array $keys): arrayGet a subset of the array containing only the given keys. Missing keys are ignored and the source order is preserved.
-
b_array_except(array $array, array $keys): arrayGet a subset of the array excluding the given keys. Missing keys are ignored and the source order is preserved.
-
b_array_simple(array $array, ?string $prefix = null): arraySimplify a multidimensional array to simple.
-
b_array_nested(array $array): arrayTransform a simple array with dot/bracket notation keys into a multidimensional array.
File
-
b_human_file_size($size, int $precision = 2): stringGet a human see file size.
-
b_size_from_ini(string $size): intGet size in bytes from ini conf file.
-
b_resolve_absolute_path(string $srcPath, string $dstPath): ?stringResolve absolute path from another.
-
b_resolve_relative_path(string $srcPath, string $dstPath): stringResolve relative path from another.
-
b_file_normalize_path(string $path): stringNormalize a path: uniformize directory separators (backslashes to slashes) and collapse consecutive slashes into a single one.
-
b_fwritei(resource $resource, string $str, ?int $length = null, ?int $offset = null): int|falseFile write in insertion mode.
-
b_ftruncate(resource $resource, int $size, ?int $offset = null): boolTruncate a part of file and shift rest of data.
Network
-
b_net_validate_ip(string $ip): boolIs valid IP (v4 or v6)?
-
b_net_validate_ipv4(string $ip): boolIs valid IP v4?
-
b_net_validate_ipv6(string $ip): boolIs valid IP v6?
-
b_net_ip_version(string $ip): ?intGet IP version (4, 6 or null if not a valid IP).
-
b_net_is_private_ip(string $ip): boolIs a private (non-public) IP address? Returns
truefor private and reserved ranges (e.g.10.0.0.0/8,192.168.0.0/16,fc00::/7, loopback, link-local, ...). -
b_net_is_public_ip(string $ip): boolIs a publicly routable IP address?
-
b_net_ip_in_range(string $ip, string $start, string $end): boolIs IP within the inclusive range
[start, end]? Works for IPv4 and IPv6 (same family required); bounds may be given in any order. -
b_net_expand_ipv6(string $ip): ?stringExpand an IPv6 address to its full form (e.g.
2001:db8::1->2001:0db8:0000:0000:0000:0000:0000:0001), ornullif not a valid IPv6. -
b_net_compress_ipv6(string $ip): ?stringCompress an IPv6 address to its shortest canonical form (e.g.
2001:0db8:0000:0000:0000:0000:0000:0001->2001:db8::1), ornullif not a valid IPv6. -
b_net_validate_netmask(string $mask, ?int $version = null): boolIs valid netmask? Accepts a dotted netmask (e.g.
255.255.255.0) or a CIDR prefix length (e.g.24). A valid netmask must be a contiguous sequence of high bits. -
b_net_validate_cidr(string $cidr): boolIs valid CIDR notation (e.g.
192.168.1.0/24,2001:db8::/32)? -
b_net_ip_in_network(string $ip, string $network): boolIs IP in network? The network can be expressed in CIDR notation (
192.168.1.0/24) or asip mask(192.168.1.0 255.255.255.0). -
b_net_range(string $network): arrayGet network range. Returns an array with keys:
version,prefix,network,netmask,first,last,broadcast(IPv4 only,nullfor IPv6) andcount(int, or numeric string for large IPv6 ranges). -
b_net_forwarded_for_parse(string $header): arraySplit and trim an
X-Forwarded-Forheader value into a list of IP addresses, stripping optional ports (including the[ipv6]:portform) and discarding invalid entries. The list keeps header order (left-most = claimed client, right-most = closest proxy). -
b_net_client_ip(array $trustedProxies = [], ?array $server = null, string $header = 'X-Forwarded-For'): ?stringDetermine the real client IP from server parameters (defaults to
$_SERVER). ReturnsREMOTE_ADDRunless the request comes from a trusted proxy (exact IP or CIDR range), in which case the forwarded header chain is walked from right to left and the first non-trusted hop is returned. IfREMOTE_ADDRis not trusted, the forwarded header is ignored. -
b_net_is_trusted_proxy(string $ip, array $trustedProxies): boolIs the given IP a trusted proxy? Matches an exact IP address, a CIDR range (e.g.
10.0.0.0/8) or a case-insensitive symbolic alias:private(any private/reserved IP),public(any publicly routable IP),loopback(127.0.0.0/8or::1) and the catch-all*,any,0.0.0.0/0,::/0(any valid IP, both families). Invalid or unknown entries are ignored.Security note: broad aliases such as
*,anyorpublicmark (nearly) any peer as trusted, allowing a client to spoof its address via the forwarded header. Use them only in controlled environments. -
b_net_ip_to_long(string $ip): int|string|nullConvert an IP address to its numeric representation. IPv4 addresses are returned as an unsigned integer, IPv6 addresses as a decimal numeric string (requires the
gmpextension). Returnsnullif not a valid IP. -
b_net_long_to_ip(int|string $value, ?int $version = null): ?stringConvert a numeric representation back to an IP address. The version (
4or6) can be forced or detected automatically. IPv6 requires thegmpextension. Returnsnullif the value is out of range or invalid.
Object
-
b_get_property_value($object, string $property, &$exists = null): mixedGet property value with getter method.
-
b_object_has_property(object $object, string $property): boolHas readable property? Uses the same detection logic as
b_get_property_value()(getter method, public property or magic methods). -
b_set_property_value($object, string $property, $value): boolSet property value with setter method.
String
-
b_str_random(int $length = 12, int $options = B_STR_RANDOM_NUMBER | B_STR_RANDOM_SPECIAL_CHARACTERS | B_STR_RANDOM_NEED_ALL): stringGenerate an random string.
-
b_nl2p(string $str): stringSurrounds paragraphs with "P" HTML tag and inserts HTML line breaks before all newlines; in a string.
-
b_str_remove_accents(string $str): stringRemove accents.
-
b_str_to_uri(string $str): stringString to URI string.
-
b_minify_html(string $str): stringMinify HTML string.
-
b_str_truncate(string $str, int $nbCharacters = 128, int $where = B_TRUNCATE_RIGHT, string $separator = '...'): stringTruncate string.
-
b_parse_str(string $str, bool $keepDots = true): arrayParses the string into variables.
-
b_pascal_case(string $str): stringGet pascal case convention of string.
-
b_camel_case(string $str): stringGet camel case convention of string.
-
b_snake_case(string $str): stringGet snake case convention of string.
-
b_spinal_case(string $str): stringGet spinal case convention of string.