bacarin / bacatools
Uma coleção leve e nativa de ferramentas utilitárias para PHP.
Requires
- php: ^8.2
Requires (Dev)
- phpunit/phpunit: ^10.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-09-04 14:39:54 UTC
README
A simple toolkit featuring the most frequently used functions in system development (or at least in my workflow).
This package brings together core utilities—such as base32Encode and base32Decode—alongside country-specific features. For instance, Brazil::getDocumentType($str) handles standard validation rules and formatting for common Brazilian user data.
Feel free to submit issues or pull requests to add support for other countries or new features!
Installation
It's possible to install via composer using this comand below:
composer require bacarin/bacatools
Features: Toolkit
| Function Call | Description | Parameters |
|---|---|---|
Bacatools\Toolkit::base32Encode($data) |
Encodes a plain text string into Base32 format. | • string $data: The plain text string to encode. |
Bacatools\Toolkit::base32Decode($data) |
Decodes a Base32 encoded string back into plain text. | • string $data: The Base32 string to decode. |
Bacatools\Toolkit::generateTotpSecret() |
Generates a cryptographically secure random secret key for TOTP authentication, encoded in Base32. | None |
Bacatools\Toolkit::generateTotp($secret, $interval, $digits, $timeSlice) |
Generates a TOTP (Time-Based One-Time Password) code based on a Base32 secret and the specified time. | • string $secret: The Base32 encoded secret key.• int $interval (optional): Token validity period in seconds (default: 30).• int $digits (optional): Length of the code (default: 6).• ?int $timeSlice (optional): Specific timestamp to evaluate, or null for current time (default: null). |
Bacatools\Toolkit::verifyTotp($secret, $code, $discrepancy, $interval, $digits) |
Verifies a TOTP code against a given secret, supporting time drift tolerance. | • string $secret: The Base32 secret key.• string $code: The TOTP code provided by the user.• int $discrepancy (optional): Allowed clock drift window tolerance (default: 1).• int $interval (optional): Token validity period in seconds (default: 30).• int $digits (optional): Expected code length (default: 6). |
Bacatools\Toolkit::getTotpProvisioningUri($name, $secret, $issuer) |
Generates an otpauth:// provisioning URI for QR code generation in authenticator apps. |
• string $name: Account identifier/name (e.g., user email).• string $secret: The Base32 encoded secret key.• string $issuer: Service provider or app name. |
Bacatools\Toolkit::generateRecoveryCodes($amount, $blocks, $blockSize) |
Generates a set of cryptographically secure two-factor authentication recovery codes. | • int $amount (optional): Number of codes to generate (default: 8).• int $blocks (optional): Number of character blocks per code (default: 2).• int $blockSize (optional): Length of each character block (default: 4). |
Bacatools\Toolkit::strSlug($str, $separator) |
Generates a URL-friendly slug from a given text, removing accents and special characters. | • string $str: The input string to slugify.• string $separator (optional): Word separator character (default: '-'). |
Bacatools\Toolkit::strMask($str, $visible, $mask, $size) |
Masks a portion of a string while keeping a specified number of characters visible at the end. | • string $str: The input string to mask.• int $visible (optional): Visible characters left at the end (default: 2).• string $mask (optional): Masking character (default: '*').• int $size (optional): Maximum length evaluated from the end (default: 15). |
Bacatools\Toolkit::strTruncate($str, $length, $ending) |
Truncates a string to a specified length without breaking words, appending a suffix. | • string $str: The string to truncate.• int $length (optional): Maximum length including suffix (default: 25).• string $ending (optional): Suffix appended if truncated (default: '...'). |
Features: Support/Base32
Base32 encode and decode functions.
| Function Call | Description | Parameters |
|---|---|---|
Bacatools\Support\Base32::encode($str) |
Encodes a plain text string into Base32 format. | • string $str: The plain text string to encode. |
Bacatools\Support\Base32::decode($str) |
Decodes a Base32 encoded string back into plain text. | • string $str: The Base32 string to decode. |
Features: Support/CreditCard
Credit card helper functions.
| Function Call | Description | Parameters |
|---|---|---|
Bacatools\Support\CreditCard::validate($number) |
Validates a credit card number using the Luhn algorithm (Modulus 10). | • string $number: The credit card number (can include spaces or dashes). |
Bacatools\Support\CreditCard::getBrand($number) |
Identifies the credit card brand/network based on its number prefix and length. | • string $number: The credit card number to evaluate. |
Features: Support/RecoveryCodes
Recovery Code generation function.
| Function Call | Description | Parameters |
|---|---|---|
Bacatools\Support\RecoveryCodes::generate($amount, $blocks, $blockSize) |
Generates a set of cryptographically secure two-factor authentication recovery codes. | • int $amount (optional): Number of recovery codes to generate (default: 8).• int $blocks (optional): Number of character blocks per code (default: 2).• int $blockSize (optional): Length of each character block (default: 4). |
Features: Support/StringTreatment
String treatment functions.
| Function Call | Description | Parameters |
|---|---|---|
Bacatools\Support\StringTreatment::mask($value, $visible, $mask, $size) |
Masks a portion of a string while keeping a specified number of characters visible at the end. | • string $value: The input string to mask.• int $visible (optional): Visible characters left at the end (default: 2).• string $mask (optional): Masking character (default: '*').• int $size (optional): Maximum length evaluated from the end (default: 15). |
Bacatools\Support\StringTreatment::slug($text, $separator) |
Generates a URL-friendly slug from a given text, removing accents and special characters. | • string $text: The input string to slugify.• string $separator (optional): Word separator character (default: '-'). |
Bacatools\Support\StringTreatment::toNumbers($value) |
Extracts only the numeric digits from a given string, stripping non-digit characters. | • string $value: The input string containing numbers and other characters. |
Bacatools\Support\StringTreatment::truncate($text, $length, $ending) |
Truncates a string to a specified length without breaking words, appending a suffix if truncated. | • string $text: The string to truncate.• int $length (optional): Maximum length including suffix (default: 100).• string $ending (optional): Suffix appended if truncated (default: '...'). |
Features: Support/Totp
TOTP (Time-Based One Time Password) functions.
| Function Call | Description | Parameters |
|---|---|---|
Bacatools\Support\Totp::generateSecret($length) |
Generates a cryptographically secure random secret key for TOTP authentication, encoded in Base32. | • int $length (optional): Number of random binary bytes to generate before Base32 encoding (default: 16). |
Bacatools\Support\Totp::generate($secret, $interval, $digits, $timeSlice) |
Generates a TOTP code based on a Base32 secret and the specified time window following RFC 6238. | • string $secret: The Base32 encoded secret key.• int $interval (optional): Token validity period in seconds (default: 30).• int $digits (optional): Length of the code (default: 6).• ?int $timeSlice (optional): Specific time slice to evaluate, or null for current time (default: null). |
Bacatools\Support\Totp::verify($secret, $code, $discrepancy, $interval, $digits) |
Verifies a TOTP code against a given secret, supporting clock drift tolerance. | • string $secret: The Base32 secret key.• string $code: The TOTP code provided by the user.• int $discrepancy (optional): Allowed window tolerance for clock drift (default: 1).• int $interval (optional): Token validity period in seconds (default: 30).• int $digits (optional): Expected code length (default: 6). |
Bacatools\Support\Totp::getProvisioningUri($name, $secret, $issuer) |
Generates an otpauth:// provisioning URI for QR code generation in authenticator apps. |
• string $name: Account identifier/name (e.g., user email).• string $secret: The Base32 encoded secret key.• string $issuer: Service provider or app name. |
Features: Countries
Brazil
| Function Call | Description | Parameters |
|---|---|---|
Bacatools\Support\Countries\Brazil::getDocumentType($doc) |
Identifies the document type based on its length and structural validation. | • string $doc: The document string to analyze. |
Bacatools\Support\Countries\Brazil::maskCpf($doc) |
Formats and masks a CPF number (e.g., ***.456.789-**). |
• string $doc: The CPF string (only digits are extracted). |
Bacatools\Support\Countries\Brazil::maskCnpj($cnpj) |
Formats and masks a CNPJ number (e.g., **.345.678/0001-**). |
• string $cnpj: The CNPJ string (only digits are extracted). |
Bacatools\Support\Countries\Brazil::maskDocument($value) |
Automatically identifies whether the input is a CPF or CNPJ and applies the respective mask. | • string $value: The CPF or CNPJ document string. |
Bacatools\Support\Countries\Brazil::validateCPF($cpf) |
Validates a Brazilian CPF (Tax ID) number based on its verification digits. | • string $cpf: The CPF number (can include formatting characters). |
Bacatools\Support\Countries\Brazil::validateCnh($cnh) |
Validates a Brazilian CNH (Driver's License) number based on its verification digits. | • string $cnh: The CNH number (can include formatting characters). |
Bacatools\Support\Countries\Brazil::validateCNPJ($cnpj) |
Validates a Brazilian CNPJ (Company Tax ID) number. Supports numeric and alphanumeric formats. | • string $cnpj: The CNPJ number (can include formatting characters). |
Bacatools\Support\Countries\Brazil::validatePis($pis) |
Validates a Brazilian PIS/PASEP (or NIS) number based on its verification digit. | • string $pis: The PIS/PASEP number (can include formatting characters). |
Bacatools\Support\Countries\Brazil::validateRenavam($renavam) |
Validates a Brazilian RENAVAM (Vehicle Registry) number. Supports 11-digit and legacy 9-digit codes. | • string $renavam: The RENAVAM number (can include formatting characters). |
Bacatools\Support\Countries\Brazil::validateCarPlate($plate) |
Validates a Brazilian license plate, supporting both traditional (ABC1234) and Mercosul (ABC1D23) formats. |
• string $plate: The vehicle license plate to validate. |