tereta/utilities

Lightweight utility helpers for the Tereta Framework: byte formatting, slug/template strings, CIDR matching, path normalization, and Factory-enabled DOMDocument/DOMXPath wrappers.

Maintainers

Package info

gitlab.com/tereta/library/utilities

Homepage

Issues

pkg:composer/tereta/utilities

Statistics

Installs: 140

Dependents: 6

Suggesters: 0

Stars: 0

1.0.11 2026-04-27 22:25 UTC

This package is auto-updated.

Last update: 2026-04-29 05:43:27 UTC


README

🌐 Русский | English

Overview

Lightweight utility module providing common helpers used across the application: math, string, network and filesystem-path primitives, plus Factory-trait wrappers around the native PHP DOM classes.

  • Services

    • Math β€” unit-conversion helpers.
      • formatBytes(int $bytes) β€” human-readable byte formatting (1048576 β†’ 1.00 MB; scales B β†’ KB β†’ MB β†’ GB β†’ TB).
    • Stringable β€” string-oriented helpers.
      • file(string $path, array $variables = []) β€” renders a .phtml template with extract()ed variables and a second {key} placeholder pass. Path is hardened via realpath() + is_file() to block path-traversal and null-byte injection.
      • identifier(string $string) β€” normalises arbitrary text into a slug-like snake_case token (lowercase, non-alphanumeric runs collapsed to _). Useful for cache keys, filenames, attribute names.
      • code(string $code) β€” prepares a heredoc-style snippet for <pre> output: trims blank edge lines, removes the common leading indent, HTML-escapes the result.
      • splitLines(string $string, int $length) β€” soft-wraps text at whitespace boundaries while preserving existing line breaks; counts length in Unicode code points.
    • Network β€” network-related helpers.
      • cidrMatch(string $ip, string $cidr) β€” tests whether an IPv4 or IPv6 address falls within a CIDR range; returns false on malformed input or family mismatch.
      • isPortOpen(string $host, int $port, float $timeout = 5.0) β€” TCP probe via fsockopen.
    • File β€” filesystem-path helpers (string-only, no I/O).
      • path(string $path, ?string $allowBase = null) β€” collapses empty segments, . and .. purely textually. When $allowBase is provided, throws RuntimeException if the resolved path falls outside that base β€” handy for sandboxing user-supplied paths.
  • DOM wrappers (Tereta\Utilities\DOMDocument, Tereta\Utilities\DOMXPath) β€” extend the native classes and add the Tereta\Core\Traits\Factory trait so they can be instantiated through the framework's DI factory pipeline.

Installation

composer require tereta/utilities

Author and License

Author: Tereta Alexander
Website: tereta.dev
License: Apache License 2.0. See LICENSE.

 www.β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
     β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β•β•β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—
        β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—  β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—     β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘
        β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•”β•β•β•  β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•     β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘
        β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—   β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘
        β•šβ•β•   β•šβ•β•β•β•β•β•β•β•šβ•β•  β•šβ•β•β•šβ•β•β•β•β•β•β•   β•šβ•β•   β•šβ•β•  β•šβ•β•
                                                      .dev

Copyright (c) 2008-2026 Tereta Alexander