Collection of useful PHP functions, mini-classes, and snippets for every day.

Overview

JBZoo / Utils

Coverage Status Psalm Coverage PHP Strict Types
Stable Version Latest Unstable Version Dependents GitHub Issues Total Downloads GitHub License

Collection of PHP functions, mini classes and snippets for everyday developer's routine life.

Install

composer require jbzoo/utils

Usage

Smart functions

use function JBZoo\Utils\alias;
use function JBZoo\Utils\alpha;
use function JBZoo\Utils\alphanum;
use function JBZoo\Utils\bool;
use function JBZoo\Utils\digits;
use function JBZoo\Utils\float;
use function JBZoo\Utils\int;

int(' 10.0 ') === 10;
float(' 10.0 ') === 10.0;

bool(' yes ') === true;
bool(' no ') === false;
bool('1') === true;
bool('0') === false;

alias('Qwer ty') === 'qwer-ty';
digits('Qwer 1 ty2') === '12';
alpha('Qwer 1 ty2') === 'Qwerty';
alphanum(' #$% Qwer 1 ty2') === 'Qwer1ty2';

JBZoo\Utils\Arr

Arr::addEachKey(array $array, string $prefix): array; // Add some prefix to each key

Arr::clean(array $haystack): array; // Clean array by custom rule

Arr::cleanBeforeJson(array $array): array; // Clean array before serialize to JSON

Arr::first(array $array); // Returns the first element in an array.

Arr::firstKey(array $array); // Returns the first key in an array.

// Flatten a multi-dimensional array into a one dimensional array.
//                            overwrite keys from shallow nested arrays
Arr::flat(array $array, bool $preserveKeys = true): array;

Arr::getField(array $arrayList, string $fieldName = 'id'): array; // Get one field from array of arrays (array of objects)

Arr::groupByKey(array $arrayList, string $key = 'id'): array; // Group array by key

Arr::implode(string $glue, array $array): string; // Array imploding for nested array

Arr::in($value, array $array, bool $returnKey = false); // Check is value exists in the array

Arr::isAssoc(array $array): bool; // Check is array is type assoc

Arr::key($key, array $array, bool $returnValue = false); // Check if key exists

Arr::last(array $array); // Returns the last element in an array.

Arr::lastKey(array $array); // Returns the last key in an array.

Arr::map(Closure $function, array $array): array; // Recursive array mapping

// Returns an array containing all the elements of arr1 after applying
// the callback function to each one.
//                             (Objects, resources, etc)
Arr::mapDeep(array $array, callable $callback, bool $onNoScalar = false): array;

// Searches for a given value in an array of arrays, objects and scalar values. You can optionally specify
// a field of the nested arrays and objects to search in.
Arr::search(array $array, $search, ?string $field = null);

Arr::sortByArray(array $array, array $orderArray): array; // Sort an array by keys based on another array

Arr::toComment(array $data): string; // Convert assoc array to comment style

Arr::unique(array $array, bool $keepKeys = false): array; // Remove the duplicates from an array.

Arr::unshiftAssoc(array $array, $key, $value): array; // Add cell to the start of assoc array

// Wraps its argument in an array unless it is already an array
//   Arr.wrap(null)      # => []
//   Arr.wrap([1, 2, 3]) # => [1, 2, 3]
//   Arr.wrap(0)         # => [0]
Arr::wrap($object): array;

JBZoo\Utils\Cli

Cli::build(string $command, array $args = []): string; // Build params for cli options

Cli::check(): bool; // Is command line mode

Cli::err(string $message, bool $addEol = true): bool; // Print line to std error

Cli::exec(string $command, array $args = [], ?string $cwd = null, bool $verbose = false): string; // Execute cli commands

Cli::getNumberOfColumns(): int; // Returns the number of columns of the terminal.

// Returns true if STDOUT supports colorization.
// This code has been copied and adapted from
// Symfony\Component\Console\Output\OutputStream.
Cli::hasColorSupport(): bool;

Cli::isInteractive($fileDescriptor = 1): bool; // Returns if the file descriptor is an interactive terminal or not.

Cli::out(string $message, bool $addEol = true): bool; // Print line to std out

JBZoo\Utils\Csv

Csv::parse(string $csvFile, string $delimiter = ';', string $enclosure = '"', bool $hasHeader = true): array; // Simple parser for CSV files

JBZoo\Utils\Dates

Dates::factory($time = null, $timeZone = null): DateTime; // Build PHP \DateTime object from mixed input

Dates::formatTime(float $seconds): string; // Convert seconds to human readable format "H:i:s"

Dates::human($date, string $format = 'd M Y H:i'): string; // Convert date string ot unix timestamp to human readable date format

Dates::is(?string $date): bool; // Check if string is date

Dates::isThisMonth($time): bool; // Returns true if date passed is within this month.

Dates::isThisWeek($time): bool; // Returns true if date passed is within this week.

Dates::isThisYear($time): bool; // Returns true if date passed is within this year.

Dates::isToday($time): bool; // Returns true if date passed is today.

Dates::isTomorrow($time): bool; // Returns true if date passed is tomorrow.

Dates::isYesterday($time): bool; // Returns true if date passed was yesterday.

Dates::sql($time = null): string; // Convert time for sql format

Dates::timezone($timezone = null): DateTimeZone; // Returns a DateTimeZone object based on the current timezone.

Dates::toStamp($time = null, bool $currentIsDefault = true): int; // Convert to timestamp

JBZoo\Utils\Email

Email::check($emails): array; // Check if email(s) is(are) valid. You can send one or an array of emails.

// Check for DNS MX records of the email domain. Notice that a
// (temporary) DNS error will have the same result as no records
// were found. Code coverage ignored because this method requires
// DNS requests that could not be reliable.
Email::checkDns(string $email): bool;

// Get domains from email addresses. The not valid email addresses
// will be skipped.
Email::getDomain($emails): array;

Email::getDomainSorted(array $emails): array; // Get domains from email addresses in alphabetical order.

Email::getGravatarBuiltInImages(): array; // Returns gravatar supported placeholders

// Generates an Gravatar URL.
// Size of the image:
// * The default size is 32px, and it can be anywhere between 1px up to 2048px.
// * If requested any value above the allowed range, then the maximum is applied.
// * If requested any value bellow the minimum, then the default is applied.
// Default image:
// * It can be an URL to an image.
// * Or one of built in options that Gravatar has. See Email::getGravatarBuiltInImages().
// * If none is defined then a built in default is used. See Email::getGravatarBuiltInDefaultImage().
Email::getGravatarUrl(string $email, int $size = 32, string $defaultImage = 'identicon'): ?string;

Email::isValid(?string $email): bool; // Returns true if string has valid email format

Email::random(int $userNameLength = 10): string; // Create random email

JBZoo\Utils\Env

Env::bool(string $envVarName, bool $default = false): bool; // Convert value of environment variable to strict bool value

Env::convert(?string $value, int $options = 16); // Converts the type of values like "true", "false", "null" or "123".

Env::float(string $envVarName, float $default = 0): float; // Convert value of environment variable to strict float value

Env::get(string $envVarName, $default = null, int $options = 16); // Returns an environment variable.

Env::int(string $envVarName, int $default = 0): int; // Convert value of environment variable to strict integer value

Env::isExists(string $envVarName): bool; // Returns true if environment variable exists

Env::string(string $envVarName, string $default = ''): string; // Convert value of environment variable to clean string

JBZoo\Utils\FS

FS::base(?string $path): string; // Returns name of file with ext from FS pathname

FS::clean(?string $path, string $dirSep = '/'): string; // Function to strip trailing / or \ in a pathname

FS::dirName(?string $path): string; // Returns name for directory from FS pathname

FS::dirSize(string $dir): int; // Returns size of a given directory in bytes.

FS::executable(string $filename, bool $executable = true): bool; // Set the executable bit on a file to the minimum value that allows the user running PHP to read to it.

FS::ext(?string $path): string; // Returns

FS::filename(?string $path): string; // Returns filename without ext from FS pathname

FS::firstLine(string $filepath): ?string; // Quickest way for getting first file line

FS::format(int $bytes, int $decimals = 2): string; // Nice formatting for computer sizes (Bytes).

FS::getRelative(string $path, ?string $rootPath = null, string $forceDS = '/'): string; // Find relative path of file (remove root part)

FS::isDir(string $path): bool; // Check is current path directory

FS::isFile(string $path): bool; // Check is current path regular file

FS::isReal(?string $path): bool; // Returns clean realpath if file or directory exists

FS::ls(string $dir): array; // Returns all paths inside a directory.

FS::openFile(string $filepath): ?string; // Binary safe to open file

FS::perms(string $file, ?int $perms = null): string; // Returns the file permissions as a nice string, like -rw-r--r-- or false if the file is not found.

FS::readable(string $filename, bool $readable = true): bool; // Set the readable bit on a file to the minimum value that allows the user running PHP to read to it.

FS::real(?string $path): ?string; // Returns realpath (smart analog of PHP \realpath())

// Removes a directory (and its contents) recursively.
// Contributed by Askar (ARACOOL) <https://github.com/ARACOOOL>
FS::rmDir(string $dir, bool $traverseSymlinks = true): bool;

FS::stripExt(string $path): string; // Strip off the extension if it exists.

FS::writable(string $filename, bool $writable = true): bool; // Set the writable bit on a file to the minimum value that allows the user running PHP to write to it.

JBZoo\Utils\Filter

Filter::_($value, $filters = 'raw'); // Apply custom filter to variable

Filter::alias(string $string): string; // Get safe string

Filter::alpha(?string $value): string; // Returns only alpha chars

Filter::alphanum(?string $value): string; // Returns only alpha and digits chars

Filter::arr($value, $filter = null): array; // Cleanup array

Filter::base64(string $value): string; // Returns only chars for base64

Filter::bool($variable): bool; // Converts many english words that equate to true or false to boolean.

Filter::className(string $input): string; // Convert words to PHP Class name

Filter::clean(string $string): string; // Alias of "Str::clean($string, true, true)"

Filter::cmd(string $value): string; // Cleanup system command

Filter::data($data): JBZoo\Data\Data; // Returns JSON object from array

Filter::digits(?string $value): string; // Returns only digits chars

Filter::esc(string $string): string; // Alias of "Str::esc($string)"

Filter::float($value, int $round = 10): float; // Smart converter string to float

Filter::html(string $string): string; // Alias of "Str::htmlEnt($string)"

Filter::int($value): int; // Smart convert any string to int

Filter::low(string $string): string; // String to lower and trim

Filter::parseLines($input): array; // Parse lines to assoc list

Filter::path(string $value): string; // Remove whitespaces

Filter::raw($string); // RAW placeholder

Filter::strip(string $string): string; // Get safe string

Filter::stripQuotes(string $value): string; // Strip quotes.

Filter::stripSpace(string $string): string; // Strip spaces

Filter::trim(string $value): string; // Remove whitespaces

Filter::trimExtend(string $value): string; // Remove whitespaces

Filter::ucFirst(string $input): string; // First char to upper, other to lower

Filter::up(string $string): string; // String to upper and trim

Filter::xml(string $string): string; // Alias of "Xml::escape($string)"

JBZoo\Utils\Http

// Transmit headers that force a browser to display the download file dialog.
// Cross browser compatible. Only fires if headers have not already been sent.
Http::download(string $filename): bool;

Http::getHeaders(): array; // Get all HTTP headers

// Sets the headers to prevent caching for the different browsers.
// Different browsers support different nocache headers, so several
// headers must be sent so that all of them get the point that no caching should occur
Http::nocache(): bool;

Http::utf8(string $contentType = 'text/html'): bool; // Transmit UTF-8 content headers if the headers haven't already been sent.

JBZoo\Utils\IP

IP::getNetMask(string $ipAddress): string; // Return network mask. For example, '192.0.0.0' => '255.255.255.0'

// Returns the IP address of the client.
//                         ONLY use if your server is behind a proxy that sets these values
IP::getRemote(bool $trustProxy = false): string;

IP::v4InRange(string $ipAddress, string $range): bool; // Check if a given ip is in a network

JBZoo\Utils\Image

Image::addAlpha($image, bool $isBlend = true): void; // Add alpha chanel to image resource

Image::alpha(float $color): int; // Returns valid value of alpha-channel

Image::blur(float $blur): int; // Return valid value to blur image (1-10)

Image::brightness(float $brightness): int; // Returns valid value to make image bright (-255..255)

Image::checkGD(bool $throwException = true): bool; // Require GD library

Image::color(float $color): int; // Returns valid value to change color segment of a image (0..255)

Image::colorize(float $colorize): int; // Returns valid value to change color segment of a image (-255..255)

Image::contrast(float $contrast): int; // Returns valid value to change contrast of a image (-100..100)

Image::direction(string $direction): string; // Returns valid value of image direction: 'x', 'y', 'xy', 'yx'

Image::getInnerCoords(string $position, array $canvas, array $box, array $offset = []): ?array; // Determine position

Image::imageCopyMergeAlpha($dstImg, $srcImg, array $dist, array $src, array $srcSizes, int $opacity): void; // Same as PHP's imagecopymerge() function, except preserves alpha-transparency in 24-bit PNGs

Image::isGdRes($image): bool; // Check is var image GD resource

Image::isGif(?string $format = null): bool; // Checks if image has GIF format

Image::isJpeg(?string $format = null): bool; // Checks if image has JPEG/JPG format

Image::isPng(?string $format = null): bool; // Checks if image has PNG format

Image::isSupportedFormat(string $format): bool; // Check is format supported by lib

Image::isWebp(?string $format = null): bool; // Checks if image has WEBP format

// Converts a hex color value to its RGB equivalent
//                                Where red, green, blue - integers 0-255, alpha - integer 0-127
Image::normalizeColor($origColor): array;

Image::opacity(float $opacity): int; // Check opacity value

Image::opacity2Alpha(float $opacity): int; // Convert opacity value to alpha

Image::percent(float $percent): int; // Return valid value of percent (0-100)

Image::position(string $position): string; // Check position name

Image::quality(float $percent): int; // Returns valid value of image quality (0..100)

Image::rotate(float $color): int; // Returns valid value of image rotation (-360..360)

Image::smooth(float $smooth): int; // Returns valid value to change smoothness of a image (1..10)

Image::strToBin(string $imageString): string; // Convert string to binary data

JBZoo\Utils\PhpDocs

// Simple parse of PHPDocs.
// Example or return value
//  [
//      'description' => 'Simple parse of PHPDocs. Example or return value',
//      'params'      => [
//          'param'  => ['string $phpDoc'],
//          'return' => ['array']
//      ]
//  ]
PhpDocs::parse(string $phpDoc): array;

JBZoo\Utils\Ser

// UnSerializes partially-corrupted arrays that occur sometimes. Addresses
// specifically the `unserialize(): Error at offset xxx of yyy bytes` error.
// NOTE: This error can *frequently* occur with mismatched character sets and higher-than-ASCII characters.
// Contributed by Theodore R. Smith of PHP Experts, Inc. <http://www.phpexperts.pro/>
Ser::fix(string $brokenSerializedData): string;

// Check value to find if it was serialized.
// If $data is not an string, then returned value will always be false. Serialized data is always a string.
Ser::is($data): bool;

Ser::maybe($data); // Serialize data, if needed.

Ser::maybeUn(string $data); // Unserialize value only if it is serialized.

JBZoo\Utils\Slug

// Transliterates characters to their ASCII equivalents.
// Part of the URLify.php Project <https://github.com/jbroadway/urlify/>
Slug::downCode(string $text, string $language = ''): string;

// Converts any accent characters to their equivalent normal characters and converts any other non-alphanumeric
// characters to dashes, then converts any sequence of two or more dashes to a single dash. This function generates
// slugs safe for use as URLs, and if you pass true as the second parameter, it will create strings safe for
// use as CSS classes or IDs.
Slug::filter(?string $string, string $separator = '-', bool $cssMode = false): string;

// Converts all accent characters to ASCII characters.
// If there are no accent characters, then the string given is just returned.
Slug::removeAccents(string $string, string $language = ''): string;

// Checks to see if a string is utf8 encoded.
// NOTE: This function checks for 5-Byte sequences, UTF8 has Bytes Sequences with a maximum length of 4.
// Written by Tony Ferrara <http://blog.ircmaxwell.com>
Slug::seemsUTF8(string $string): bool;

JBZoo\Utils\Stats

// Generate a histogram.
// Note this is not a great function, and should not be relied upon
// for serious use.
// For a better implementation copy:
//   http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.histogram.html
Stats::histogram(array $values, int $steps = 10, ?float $lowerBound = null, ?float $upperBound = null): array;

Stats::linSpace(float $min, float $max, int $num = 50, bool $endpoint = true): array; // Returns an array populated with $num numbers from $min to $max.

Stats::mean(?array $values): float; // Returns the mean (average) value of the given values.

Stats::renderAverage(array $values, int $rounding = 3): string; // Render human readable string of average value and system error

Stats::stdDev(array $values, bool $sample = false): float; // Returns the standard deviation of a given population.

Stats::variance(array $values, bool $sample = false): float; // Returns the variance for a given population.

JBZoo\Utils\Str

// Make string safe
// - Remove UTF-8 chars
// - Remove all tags
// - Trim
// - Add Slashes (opt)
// - To lower (opt)
Str::clean(string $string, bool $toLower = false, bool $addSlashes = false, bool $removeAccents = true): string;

Str::esc(string $string): string; // Escape UTF-8 strings

// Escape string before save it as xml content.
// The function is moved. Please, use \JBZoo\Utils\Xml::escape($string). It'll be deprecated soon.
Str::escXml(string $string): string;

Str::getClassName($object, bool $toLower = false): string; // Get class name without namespace

Str::htmlEnt(string $string, bool $encodedEntities = false): string; // Convert >, <, ', " and & to html entities, but preserves entities that are already encoded.

Str::iPos(string $haystack, string $needle, int $offset = 0): ?int; // Finds position of first occurrence of a string within another, case insensitive

Str::iStr(string $haystack, string $needle, bool $beforeNeedle = false): string; // Finds first occurrence of a string within another, case insensitive

// Increments a trailing number in a string.
// Used to easily create distinct labels when copying objects. The method has the following styles:
//  - default: "Label" becomes "Label (2)"
//  - dash:    "Label" becomes "Label-2"
Str::inc(string $string, string $style = 'default', int $next = 0): string;

Str::isEnd(string $haystack, string $needle, bool $caseSensitive = false): bool; // Checks if the $haystack ends with the text in the $needle. Case sensitive.

Str::isMBString(): bool; // Check is mbstring loaded

Str::isOverload(): bool; // Check is mbstring overload standard functions

Str::isStart(string $haystack, string $needle, bool $caseSensitive = false): bool; // Checks if the $haystack starts with the text in the $needle.

Str::len(string $string): int; // Get string length

Str::like(string $pattern, string $haystack, bool $caseSensitive = true): bool; // Check if a given string matches a given pattern.

Str::limitChars(string $string, int $limit = 100, string $append = '...'): string; // Truncate the string to given length of characters.

Str::limitWords(string $string, int $limit = 100, string $append = '...'): string; // Truncate the string to given length of words.

Str::listToDescription(array $data, bool $alignByKeys = false): ?string; // Convert array of strings to list as pretty print description

Str::low($string): string; // Make a string lowercase

Str::parseLines(string $text, bool $toAssoc = true): array; // Parse text by lines

Str::pos(string $haystack, string $needle, int $offset = 0): ?int; // Find position of first occurrence of string in a string

Str::rChr(string $haystack, string $needle, bool $part = false): string; // Finds the last occurrence of a character in a string within another

Str::rPos(string $haystack, string $needle, int $offset = 0): ?int; // Find position of last occurrence of a string in a string

Str::random(int $length = 10, bool $isReadable = true): string; // Generate readable random string

Str::slug(string $text = '', bool $isCache = false): string; // Converts any accent characters to their equivalent normal characters

Str::splitCamelCase(string $input, string $separator = '_', bool $toLower = true): string; // Convert camel case to human readable format

// Splits a string of multiple queries into an array of individual queries.
// Single line or line end comments and multi line comments are stripped off.
Str::splitSql(string $sql): array;

Str::strStr(string $haystack, string $needle, bool $beforeNeedle = false): string; // Finds first occurrence of a string within another

Str::stripSpace(string $string): string; // Strip all whitespaces from the given string.

Str::sub(string $string, int $start, int $length = 0): string; // Get part of string

Str::subCount(string $haystack, string $needle): int; // Count the number of substring occurrences

Str::testName2Human(string $input): string; // Convert test name to human readable string

Str::trim(string $value, bool $extendMode = false): string; // Trim whitespaces and other special chars

Str::truncateSafe(string $string, int $length, string $append = '...'): string; // Truncate a string to a specified length without cutting a word off.

Str::unique(string $prefix = 'unique'): string; // Get unique string

Str::up($string): string; // Make a string uppercase

// Generates a universally unique identifier (UUID v4) according to RFC 4122
// Version 4 UUIDs are pseudo-random!
// Returns Version 4 UUID format: xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx where x is
// any random hex digit and Y is a random choice from 8, 9, a, or b.
Str::uuid(): string;

Str::zeroPad(string $number, int $length): string; // Pads a given string with zeroes on the left.

JBZoo\Utils\Sys

// Returns true when Xdebug is supported or
// the runtime used is PHPDBG (PHP >= 7.0).
Sys::canCollectCodeCoverage(): bool;

// Returns the path to the binary of the current runtime.
// Appends ' --php' to the path when the runtime is HHVM.
Sys::getBinary(): string;

Sys::getDocRoot(): ?string; // Returns current document root

Sys::getHome(): ?string; // Returns a home directory of current user.

Sys::getMemory(bool $isPeak = true): string; // Get usage memory

Sys::getName(): string; // Returns type of PHP

Sys::getNameWithVersion(): string; // Return type and version of current PHP

Sys::getUserName(): ?string; // Returns current linux user who runs script

Sys::getVendorUrl(): string; // Return URL of PHP official web-site. It depends of PHP vendor.

Sys::getVersion(): ?string; // Returns current PHP version

// Returns true when the runtime used is PHP with the PHPDBG SAPI
// and the phpdbg_*_oplog() functions are available (PHP >= 7.0).
Sys::hasPHPDBGCodeCoverage(): bool;

Sys::hasXdebug(): bool; // Returns true when the runtime used is PHP and Xdebug is loaded.

Sys::iniGet(string $varName): string; // Alias fo ini_get function

Sys::iniSet(string $phpIniKey, string $newValue): bool; // Alias fo ini_set function

Sys::isFunc($funcName): bool; // Checks if function exists and callable

Sys::isHHVM(): bool; // Returns true when the runtime used is HHVM.

Sys::isPHP(string $version, string $current = '7.2.31'): bool; // Compares PHP versions

Sys::isPHPDBG(): bool; // Returns true when the runtime used is PHP with the PHPDBG SAPI.

Sys::isRealPHP(): bool; // Returns true when the runtime used is PHP without the PHPDBG SAPI.

Sys::isRoot(): bool; // Check is current user ROOT

Sys::isWin(): bool; // Check is current OS Windows

Sys::setMemory(string $newLimit = '256M'): void; // Set new memory limit

Sys::setTime(int $newLimit = 0): void; // Set PHP execution time limit (doesn't work in safe mode)

JBZoo\Utils\Timer

Timer::format(float $milliSeconds): string; // Formats the elapsed time as a string.

Timer::formatMS(float $seconds): string; // Formats the elapsed time as a string.

Timer::getRequestTime(): float; // Get request time

Timer::timeSinceStart(): float; // Formats the elapsed time since the start of the request as a string.

JBZoo\Utils\Url

Url::addArg(array $newParams, ?string $uri = null): string; // Add or remove query arguments to the URL.

Url::build(array $queryParams): string; // Builds HTTP query from array

// Build a URL. The parts of the second URL will be merged into the first according to the flags argument.
//                                or associative array like parse_url() returns
//                                would return
Url::buildAll($sourceUrl, $destParts = [], int $flags = 1, array $newUrl = []): string;

Url::create(array $parts = []): string; // Create URL from array params

Url::current(bool $addAuth = false): ?string; // Returns the current URL.

Url::delArg($keys, ?string $uri = null): string; // Removes an item or list from the query string.

Url::getAuth(): ?string; // Get current auth info

Url::isAbsolute(string $path): bool; // Is absolute url

Url::isHttps(bool $trustProxyHeaders = false): bool; // Checks to see if the page is being server over SSL or not

// Turns all of the links in a string into HTML links.
// Part of the LinkifyURL Project <https://github.com/jmrware/LinkifyURL>
Url::parseLink(string $text): string;

Url::path(): ?string; // Returns the current path

Url::pathToRel(string $path): string; // Convert file path to relative URL

Url::pathToUrl(string $path): string; // Convert file path to absolute URL

Url::root(bool $addAuth = false): ?string; // Returns current root URL

JBZoo\Utils\Vars

Vars::isEven(int $number): bool; // Is the current value even?

Vars::isIn(float $number, float $min, float $max): bool; // Returns true if the number is within the min and max.

Vars::isNegative(float $number): bool; // Is the current value negative; less than zero.

Vars::isOdd(int $number): bool; // Is the current value odd?

Vars::isPositive(float $number, bool $zero = true): bool; // Is the current value positive; greater than or equal to zero.

Vars::limit(float $number, float $min, float $max): int; // Limits the number between two bounds.

Vars::max(float $number, float $max): int; // Decrease the number to the maximum if above threshold.

Vars::min(float $number, float $min): int; // Increase the number to the minimum if below threshold.

Vars::out(float $number, float $min, float $max): bool; // Returns true if the number is outside the min and max.

// Ensures $value is always within $min and $max range.
// If lower, $min is returned. If higher, $max is returned.
Vars::range(float $value, float $min, float $max): int;

Vars::relativePercent(float $normal, float $current): string; // Get relative percent

JBZoo\Utils\Xml

// Convert array to PHP DOMDocument object.
// Format of input array
// $source = [
//     '_node'     => '#document',
//     '_text'     => null,
//     '_cdata'    => null,
//     '_attrs'    => [],
//     '_children' => [
//         [
//             '_node'     => 'parent',
//             '_text'     => "Content of parent tag",
//             '_cdata'    => null,
//             '_attrs'    => ['parent-attribute' => 'value'],
//             '_children' => [
//                 [
//                     '_node'     => 'child',
//                     '_text'     => "Content of child tag",
//                     '_cdata'    => null,
//                     '_attrs'    => [],
//                     '_children' => [],
//                 ],
//             ]
//         ]
//     ]
// ];
// Format of output
//     <?xml version="1.0" encoding="UTF-8"?>
//     <parent parent-attribute="value">Content of parent tag<child>Content of child tag</child></parent>
Xml::array2Dom(array $xmlAsArray, ?DOMElement $domElement = null, ?DOMDocument $document = null): DOMDocument;

Xml::createFromString(?string $source = null, bool $preserveWhiteSpace = false): DOMDocument; // Create DOMDocument object from XML-string

// Convert PHP \DOMDocument or \DOMNode object to simple array
// Format of input XML (as string)
//     <?xml version="1.0" encoding="UTF-8"?>
//     <parent parent-attribute="value">Content of parent tag<child>Content of child tag</child></parent>
// Format of output array
// $result = [
//     '_node'     => '#document',
//     '_text'     => null,
//     '_cdata'    => null,
//     '_attrs'    => [],
//     '_children' => [
//         [
//             '_node'     => 'parent',
//             '_text'     => "Content of parent tag",
//             '_cdata'    => null,
//             '_attrs'    => ['parent-attribute' => 'value'],
//             '_children' => [
//                 [
//                     '_node'     => 'child',
//                     '_text'     => "Content of child tag",
//                     '_cdata'    => null,
//                     '_attrs'    => [],
//                     '_children' => [],
//                 ],
//             ]
//         ]
//     ]
// ];
Xml::dom2Array(DOMNode $element): array;

Xml::escape($rawXmlContent): string; // Escape string before save it as xml content

Links (ideas and some functions)

Unit tests and check code style

make
make test-all

License

MIT

See Also

  • CI-Report-Converter - Converting different error reports for deep compatibility with popular CI systems.
  • Composer-Diff - See what packages have changed after composer update.
  • Composer-Graph - Dependency graph visualization of composer.json based on mermaid-js.
  • Mermaid-PHP - Generate diagrams and flowcharts with the help of the mermaid script language.
  • Image - Package provides object-oriented way to manipulate with images as simple as possible.
  • Data - Extended implementation of ArrayObject. Use files as config/array.
  • Retry - Tiny PHP library providing retry/backoff functionality with multiple backoff strategies and jitter support.
  • SimpleTypes - Converting any values and measures - money, weight, exchange rates, length, ...
Comments
  • Add Arr::wrap function.

    Add Arr::wrap function.

    Hi! @SmetDenis

    I'd like to add Arr::wrap function :airplane:

    example:

    Arr.wrap(null)      # => []
    Arr.wrap([1, 2, 3]) # => [1, 2, 3]
    Arr.wrap(0)         # => [0]
    
    opened by hikouki 5
  • Can be removed the need for private methods start with underscore?

    Can be removed the need for private methods start with underscore?

    Hi there,

    Don't think that starting the function name of private methods with an underscore can be useful. Can this be removed from the ruleset?

    Thanks!

    opened by lfbn 5
  • undefined function JBZoo\Data\data()

    undefined function JBZoo\Data\data()

    Hi, I'm getting this error when i try to use Url::current()

    Fatal error: Uncaught Error: Call to undefined function JBZoo\Data\data() in /Applications/MAMP/htdocs/inc/JBZoo/Utils/Url.php:162 Stack trace: #0 /Applications/MAMP/htdocs/inc/JBZoo/Utils/Url.php(115): JBZoo\Utils\Url::root(false) #1 /Applications/MAMP/htdocs/a/index.php(11): JBZoo\Utils\Url::current() #2 {main} thrown in /Applications/MAMP/htdocs/inc/JBZoo/Utils/Url.php on line 162

    Also throws an error on Url::buildAll() at Url.php on line 243

    Screen Shot 2019-06-22 at 14 59 58

    opened by canc 4
  • Is ext-gd required for common library usage?

    Is ext-gd required for common library usage?

    My question is, why this package requires to have installed an ext-gd (extension for image manipulations, ref [1]) always, since util class for images can be not used (as it is not the main package responsibility)?

    [1] - https://github.com/JBZoo/Utils/blob/bfece7c54b9c8b93b248e26f5b2a92d7615132f3/composer.json#L32

    Background: I'm using composer-diff (https://github.com/JBZoo/Composer-Diff) package to track composer dependencies changes which require this package, but use only Sys and Cli helpers.

    question 
    opened by andrew-demb 2
  • Arr::key() is inconsistent with string parameters.

    Arr::key() is inconsistent with string parameters.

    Arr::key() returns inconsistent results if the passed $array parameter is a string.

    // expect '01' but returns '0'
    Arr::key( 0, '01', true );
    
    // returns null as expected
    // but should return '1' to be consistent with the previous example
    Arr::key( 1, '01', true );
    

    The inconsistency is due to Arr::key() casting the $array parameter for array_key_exists() but it returns the value of the uncasted parameter.

    opened by jmwebservices 1
  • Dependabot can't resolve your PHP dependency files

    Dependabot can't resolve your PHP dependency files

    Dependabot can't resolve your PHP dependency files.

    As a result, Dependabot couldn't update your dependencies.

    The error Dependabot encountered was:

    Your requirements could not be resolved to an installable set of packages.
      Problem 1
        - jbzoo/toolbox-dev 2.x-dev is an alias of jbzoo/toolbox-dev dev-master and thus requires it to be installed too.
        - jbzoo/toolbox-dev[dev-master, 2.8.0, ..., 2.9.1] require jbzoo/composer-graph ^1.0.4 -> satisfiable by jbzoo/composer-graph[1.0.4, 1.1.0, 1.1.1, 1.x-dev (alias of dev-master)].
        - jbzoo/composer-graph 1.x-dev is an alias of jbzoo/composer-graph dev-master and thus requires it to be installed too.
        - jbzoo/composer-graph[dev-master, 1.0.4, ..., 1.1.1] require jbzoo/utils ^4.2.3 -> satisfiable by jbzoo/utils[4.2.3] from composer repo (https://repo.packagist.org) but jbzoo/utils[1.0.0+no-version-set] from root package repo has higher repository priority. The packages with higher priority do not match your constraint and are therefore not installable. See https://getcomposer.org/repoprio for details and assistance.
        - Root composer.json requires jbzoo/toolbox-dev ^2.8.0 -> satisfiable by jbzoo/toolbox-dev[2.8.0, 2.9.0, 2.9.1, 2.x-dev (alias of dev-master)].
    

    If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

    View the update logs.

    opened by dependabot-preview[bot] 0
  • Dependabot can't resolve your PHP dependency files

    Dependabot can't resolve your PHP dependency files

    Dependabot can't resolve your PHP dependency files.

    As a result, Dependabot couldn't update your dependencies.

    The error Dependabot encountered was:

    Your requirements could not be resolved to an installable set of packages.
      Problem 1
        - jbzoo/toolbox-dev 2.x-dev is an alias of jbzoo/toolbox-dev dev-master and thus requires it to be installed too.
        - jbzoo/toolbox-dev[dev-master, 2.8.0, ..., 2.9.0] require jbzoo/composer-graph ^1.0.4 -> satisfiable by jbzoo/composer-graph[1.0.4, 1.x-dev (alias of dev-master)].
        - jbzoo/composer-graph 1.x-dev is an alias of jbzoo/composer-graph dev-master and thus requires it to be installed too.
        - jbzoo/composer-graph[dev-master, 1.0.4] require jbzoo/utils ^4.2.3 -> satisfiable by jbzoo/utils[4.2.3] from composer repo (https://repo.packagist.org) but jbzoo/utils[1.0.0+no-version-set] from root package repo has higher repository priority. The packages with higher priority do not match your constraint and are therefore not installable. See https://getcomposer.org/repoprio for details and assistance.
        - Root composer.json requires jbzoo/toolbox-dev ^2.8.0 -> satisfiable by jbzoo/toolbox-dev[2.8.0, 2.9.0, 2.x-dev (alias of dev-master)].
    

    If you think the above is an error on Dependabot's side please don't hesitate to get in touch - we'll do whatever we can to fix it.

    View the update logs.

    opened by dependabot-preview[bot] 0
Releases(5.0.0)
Owner
JBZoo Toolbox
Awesome developer tools and libs
JBZoo Toolbox
A widget that displays a Bible verse every day in the Statamic dashboard

Statamic Widget: Verse of the Day What is it A widget that displays a Bible verse every day in the Statamic dashboard. How to install it Install via c

Michael 2 Jan 27, 2022
This Kirby V3 Plugin brings snippets and blueprints together in one place. It includes useful tools that completely changing the way you work with Kirby: Fast and well organized.

Kirby Components Overview Do you love to make awesome projects with Kirby CMS? Do you also find it difficult to switch between snippets and blueprints

Roman Gsponer 6 May 31, 2023
This component provides a collection of functions/classes using the symfony/intl package when the Intl extension is not installed.

Symfony Polyfill / Intl: ICU This package provides fallback implementations when the Intl extension is not installed. It is limited to the "en" locale

Symfony 2.4k Jan 6, 2023
⚡ Php snippets, random stuff, demos, functions, fast message system, agnostic and framework free - 100% compactible ;) ⚡

⚡ Php8 FPM Nginx Fast, Scripts, Pearls & Treasures ?? Want to run and test asap ? docker-compose up -d phpgit_php8;ip=$(docker-machine ip default);ech

Benjamin FONTAINE 0 Mar 20, 2022
This library provides a collection of native enum utilities (traits) which you almost always need in every PHP project.

This library provides a collection of native enum utilities (traits) which you almost always need in every PHP project.

DIVE 20 Nov 11, 2022
🎓 Collection of useful PHP frequently asked questions, articles and best practices

PHP.earth documentation These files are available online at PHP.earth. Contributing and license We are always looking forward to see your contribution

PHP.earth 278 Dec 27, 2022
A collection of useful codes and utilities for WordPress plugin development..

WordPress Utils A collection of useful codes and utilities for WordPress plugin development. These simplifies common tasks and promote code reusabilit

weDevs 5 Jun 9, 2023
Here is the top 100 PHP functions: it is the list of the most often used PHP native functions

Here is the top 100 PHP functions: it is the list of the most often used PHP native functions. If you are a PHP developer, you must know the Top 100 PHP Functions deeply.

Max Base 16 Dec 11, 2022
This library can be used, among other things, to retrieve the classes, interfaces, traits, enums, functions and constants declared in a file

marijnvanwezel/reflection-file Library that allows reflection of files. This library can be used, among other things, to retrieve the classes, interfa

Marijn van Wezel 5 Apr 17, 2022
Magento-Functions - A Resource of Magento Functions

Magento-Functions A Resource of Magento Functions Table of Contents Category Product User Cart Checkout General Account [Working w/ URL's] (#urls) Cat

Bryan Littlefield 28 Apr 19, 2021
The slides and demo files for my Alpine Day 2021 talk

Building a Better Dialog Austen Cameron - @austencam This repository contains the slides and demos for my talk from Alpine Day 2021. Below, you'll fin

Austen Cameron 9 Nov 16, 2021
A collection of functions to clean up WordPress

Clean WordPress Admin A collection of functions to clean up WordPress front and back-end to make it easier for editors to work and for you to look at

Vincent Orback 411 Dec 28, 2022
TeamCal Pro is a legacy web application of a day-based calendar

TeamCal Pro is a legacy web application of a day-based calendar. It's generic purpose is the absence management of project teams, more precisely of their members.

George Lewe 1 Jan 7, 2022
Adds support for quickly adding a "snow day" banner at the top of a website.

❄️ Snow Day Plugin ?? Requires OctoberCMS 2.0 ✨ What does this plugin do? Provides the ability to quickly add a cross-site banner using a component. ❓

Albright Labs 4 Nov 7, 2022
Easily create and work with code snippets from PHP

code-snippets Easily create and work with code snippets from source code files of any type in PHP. The original code this package is based on was borr

Permafrost Software 8 Sep 4, 2022
Demo serverless applications, examples code snippets and resources for PHP

The Serverless LAMP stack Examples Code example Description AWS blog link 0.1-SimplePhpFunction A very simple implementation of a PHP Lambda function.

AWS Samples 303 Dec 20, 2022
Sanitize and escape every values in your PHP Application

PHP Sanitizer Sanitize and escape every values in your PHP Application. This solution will make PHP developer life easy, very easy and developers woul

Maniruzzaman Akash 10 Oct 2, 2022
A library of powerful code snippets to help you get the job done with Gravity Forms and Gravity Perks.

Gravity Wiz Snippet Library Gravity Wiz is creating the most comprehensive library of snippets for Gravity Forms ever. We'll be consistently moving ou

Gravity Wiz 151 Dec 27, 2022
Custom code snippets and examples for SkyVerge-built WooCommerce extensions

SkyVerge WooCommerce Plugins Welcome to the wc-plugins-snippets repository! This repository stores code snippets related to SkyVerge WooCommerce plugi

SkyVerge 255 Nov 16, 2022