Utils
Collection of useful PHP functions, mini-classes, and snippets for every day.
Install / Use
/learn @JBZoo/UtilsREADME
JBZoo / Utils
<!--ts-->- Install
- Usage
- Smart functions
- JBZoo\Utils\Arr
- JBZoo\Utils\Cli
- JBZoo\Utils\Csv
- JBZoo\Utils\Dates
- JBZoo\Utilsmail
- JBZoo\Utilsnv
- JBZoo\Utils\FS
- JBZoo\Utils\Filter
- JBZoo\Utils\Http
- JBZoo\Utils\IP
- JBZoo\Utils\Image
- JBZoo\Utils\PhpDocs
- JBZoo\Utils\Ser
- JBZoo\Utils\Slug
- JBZoo\Utils\Stats
- JBZoo\Utils\Str
- JBZoo\Utils\Sys
- JBZoo\Utils\Timer
- JBZoo\Utils\Url
- JBZoo\Utils\Vars
- JBZoo\Utils\Xml
- Links (ideas and some functions)
- Unit tests and check code style
- License
- See Also
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): ?mixed; // Returns the first element in an array.
Arr::firstKey(array $array): ?string|int|null; // 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::getSchema(array $array): array; // Returns type of variables as array schema.
Arr::groupByKey(array $arrayList, string $key = 'id'): array; // Group array by key and return list of grouped values.
Arr::implode(string $glue, array $array): string; // Array imploding for nested array.
Arr::in(?mixed $value, array $array, bool $returnKey = false): ?string|int|bool|null; // Check is value exists in the array.
Arr::isAssoc(array $array): bool; // Check is array is type assoc.
Arr::key(?mixed $key, array $array, bool $returnValue = false): ?mixed; // Check if key exists.
Arr::last(array $array): ?mixed; // Returns the last element in an array.
Arr::lastKey(array $array): ?string|int|null; // 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;
Arr::removeByValue(array $array, ?string|int|float|bool|null $value): array; // Remove all items from array by value.
// 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, ?string|int|float|bool|null $search, ??string $field = null): string|bool;
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, string|int $key, ?mixed $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(?mixed $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(?mixed $time = null, ?DateTimeZone|string|null $timeZone = null): DateTime; // Build PHP \DateTime object from mixed input.
Dates::formatTime(float $seconds, int $minValuableSeconds = 2): string; // Convert seconds to human-readable format "H:i:s".
Dates::human(string|int $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(string|int $time): bool; // Returns true if date passed is within this month.
Dates::isThisWeek(string|int $time): bool; // Returns true if date passed is within this week.
Dates::isThisYear(string|int $time): bool; // Returns true if date passed is within this year.
Dates::isToday(string|int $time): bool; // Returns true if date passed is today.
Dates::isTomorrow(string|int $time): bool; // Returns true if date passed is tomorrow.
Dates::isYesterday(string|int $time): bool; // Returns true if date passed was yesterday.
Dates::sql(?string|int|null $time = null): string; // Convert time for sql format.
Dates::timezone(?DateTimeZone|string|null $timezone = null): DateTimeZone; // Returns a DateTimeZone object based on the current timezone.
Dates::toStamp(?DateTime|string|int|null $time = null, bool $currentIsDefault = true): int; // Convert to timestamp.
JBZoo\Utils\Email
Email::check(?mixed $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;
Email::getDomain(?mixed $emails): array; // Get domains from email addresses. The not valid email addresses will be skipped.
Email::getDomainSorted(array $emails): array; // Get domains from email addresses in alphabetical order.
Email::getGravatarBuiltInImages(): array; // Returns gravatar supported placeholders.
// Generates a 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 a 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; // C
