JBZoo / Utils

repository·master·Indexed 21 days ago

https://github.com/jbzoo/utils

A collection of PHP functions, mini classes, and snippets for routine developer tasks. It includes utilities for array manipulation (Arr), CLI interaction (Cli), CSV parsing (Csv), date handling (Dates), email validation (Email), environment variables (Env), filesystem management (FS), data filtering (Filter), HTTP headers (Http), IP networking (IP), image processing (Image), serialization (Ser), slug generation (Slug), statistical calculations (Stats), string manipulation (Str), and system environment checks (Sys).

Tokens
4.4K
Snippets
6
Records
26
Agent score
24%

What's inside jbzoo-utils

  1. Use Smart Functions for type conversion and string manipulation

    master

    JBZoo provides several standalone smart functions for quick type casting and string cleaning. These are useful for normalizing input data.

    Type Conversions:

    • int($value): Converts to integer (e.g., ' 10.0 ' becomes 10).
    • float($value): Converts to float (e.g., ' 10.0 ' becomes 10.0).
    • bool($value): Converts truthy/falsy strings like 'yes', 'no', '1', or '0' to boolean.

    String Cleaning:

    • alias($string): Creates a URL-friendly alias (e.g., 'Qwer ty' becomes 'qwer-ty').
    • digits($string): Extracts only digits.
    • alpha($string): Extracts only alphabetic characters.
    • alphanum($string): Extracts only alphanumeric characters.
    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;
    
    alias('Qwer ty') === 'qwer-ty';
    digits('Qwer 1 ty2') === '12';
    alpha('Qwer 1 ty2') === 'Qwerty';
    alphanum(' #$% Qwer 1 ty2') === 'Qwer1ty2';
  2. Perform math and range checks with JBZoo\Utils\Vars

    master

    The Vars class provides mathematical utility functions for numbers.

    Key Methods:

    • Vars::isEven(int $number) / isOdd: Checks parity.
    • Vars::isIn(float $number, float $min, float $max): Checks if a number is within a range.
    • Vars::range(float $value, float $min, float $max): Ensures a value is clamped between $min and $max.
    • Vars::limit(float $number, float $min, float $max): Limits a number between two bounds.
  3. Handle serialized data with JBZoo\Utils\Ser

    master

    The Ser class provides tools for managing PHP serialized data, including fixing corrupted strings.

    Key Methods:

    • Ser::fix(string $brokenSerializedData): Attempts to fix partially-corrupted serialized arrays (often caused by character set mismatches).
    • Ser::is(?mixed $data): Checks if the data is a serialized string.
    • Ser::maybe(?mixed $data): Serializes data only if it is not already serialized.
    • Ser::maybeUn(string $data): Unserializes a value only if it is a serialized string.
  4. Generate slugs with JBZoo\Utils\Slug

    master

    The Slug class helps create URL-safe strings.

    Key Methods:

    • Slug::filter(?string $string, string $separator = '-', bool $cssMode = false): Converts accent characters to ASCII, replaces non-alphanumeric characters with separators, and collapses multiple separators. If $cssMode is true, it creates strings safe for CSS classes/IDs.
    • Slug::removeAccents(string $string, string $language = ''): Converts accent characters to their ASCII equivalents.
    • Slug::seemsUTF8(string $string): Checks if a string is UTF-8 encoded.
  5. Manipulate HTTP headers with JBZoo\Utils\Http

    master

    The Http class provides helpers for common HTTP response tasks.

    Key Methods:

    • Http::download(string $filename): Transmits headers to force a browser download dialog.
    • Http::nocache(): Sets headers to prevent browser caching.
    • Http::utf8(string $contentType = 'text/html'): Transmits UTF-8 content headers.
    • Http::getHeaders(): Retrieves all current HTTP headers.
  6. Access environment variables with JBZoo\Utils\Env

    master

    The Env class provides type-safe access to environment variables.

    Key Methods:

    • Env::get(?string $envVarName, ?string|int|float|bool|null $default = null, int $options = 16): Retrieves an environment variable.
    • Env::bool(string $envVarName, bool $default = false): Converts an env var to a strict boolean.
    • Env::int(string $envVarName, int $default = 0): Converts an env var to a strict integer.
    • Env::float(string $envVarName, float $default = 0): Converts an env var to a strict float.
    • Env::string(string $envVarName, string $default = ''): Converts an env var to a clean string.
    • Env::isExists(string $envVarName): Checks if the variable exists.
  7. Convert arrays to XML with JBZoo\Utils\Xml

    master

    The Xml class allows for converting between PHP arrays and \DOMDocument objects.

    Key Methods:

    • Xml::array2Dom(array $xmlAsArray, ?\DOMElement $domElement = null, ?\DOMDocument $document = null): Converts a structured array into a \DOMDocument.
    • Xml::dom2Array(\DOMNode $element): Converts a \DOMNode into a simple array.
    • Xml::createFromString(?string $source = null, bool $preserveWhiteSpace = false): Creates a \DOMDocument from an XML string.
  8. Manipulate strings with JBZoo\Utils\Str

    master

    The Str class is a powerful utility for string manipulation, including case conversion, truncation, and pattern matching.

    Key Methods:

    • Str::clean(string $string, bool $toLower = false, bool $addSlashes = false, bool $removeAccents = true): Makes a string safe by removing UTF-8 chars, tags, and trimming.
    • Str::uuid(): Generates a RFC 4122 compliant UUID v4.
    • Str::limitChars(string $string, int $limit = 100, string $append = '...'): Truncates a string by character count.
    • Str::limitWords(string $string, int $limit = 100, string $append = '...'): Truncates a string by word count.
    • Str::slug(string $text = '', bool $isCache = false): Converts text to a slug.
    • Str::random(int $length = 10, bool $isReadable = true): Generates a random string.
    • Str::splitCamelCase(string $input, string $separator = '_', bool $toLower = true): Converts camelCase to human-readable format.
  9. Process images with JBZoo\Utils\Image

    master

    The Image class provides utilities for working with GD image resources and image metadata.

    Key Methods:

    • Image::checkGD(bool $throwException = true): Checks if the GD library is enabled.
    • Image::isPng(?string $format = null) / isJpeg / isGif / isWebp: Checks the image format.
    • Image::quality(float $percent): Returns a valid value for image quality (0..100).
    • Image::rotate(float $color): Returns a valid value for image rotation (-360..360).
  10. Handle dates and timestamps with JBZoo\Utils\Dates

    master

    The Dates class simplifies date creation, formatting, and relative time checks.

    Key Methods:

    • Dates::factory(?mixed $time = null, ?DateTimeZone|string|null $timeZone = null): Creates a \DateTime object from various inputs.
    • Dates::human(string|int $date, string $format = 'd M Y H:i'): Converts a date string or timestamp to a human-readable format.
    • Dates::isToday(string|int $time) / isTomorrow / isYesterday: Checks if a given time matches today, tomorrow, or yesterday.
    • Dates::isThisMonth(string|int $time) / isThisWeek / isThisYear: Checks if a date falls within the current month, week, or year.
    • Dates::sql(?string|int|null $time = null): Converts a time to a SQL-compatible format.