thoemo/traits (1.5.4)

Published 2026-10-25 15:48:52 +00:00 by elnebuloso

Installation

{
	"repositories": [{
			"type": "composer",
			"url": ""
		}
	]
}
composer config repositories.thoemo composer 
composer require thoemo/traits:1.5.4

About this package

Helpful traits

thoemo/traits

A collection of small PHP traits for everyday array, string, sorting and formatting tasks. Each trait adds one or a few methods to the class that uses it.

Requirements

  • PHP >= 8.2
  • PHP extensions ext-yaml and ext-zlib
  • Composer dependencies (installed automatically): chrisullyott/php-filesize, cocur/slugify, jasny/dotkey

Installation

The package is served by the Forgejo Composer registry, which needs no credentials. Add the repository to your composer.json:

"repositories": {
    "packages.link.thoemo.net/thoemo": {
        "type": "composer",
        "url": "https://packages.link.thoemo.net/api/packages/thoemo/composer"
    }
}

Then require the package:

composer require thoemo/traits

The namespace is elnebuloso\Traits, not thoemo\....

Overview

All traits live in src/Traits/. Methods are protected unless noted otherwise.

Arrays

Trait Method(s) What it does
ArrayAssocFlattenTrait arrayAssocFlatten(array $array): array Flattens a nested array into ["a.b.c" => value] for all scalar leaves.
ArrayKeyPathsToScalarValuesTrait arrayKeyPathsToScalarValues(array $array): array Returns the dot paths ("a.b.c") of all scalar leaves.
ArrayFlattenTrait arrayFlatten(array $array): array Flattens a nested array into a list of its leaf values; keys are dropped.
ArrayFilterAllowListTrait arrayFilterAllowList(array $data, array $allowList): array Keeps only the keys listed in $allowList.
ArrayFilterBlockListTrait arrayFilterBlockList(array $data, array $blockList): array Removes the keys listed in $blockList.
ArrayFilterHiddenTrait arrayFilterHidden(array $data, array $hidden): array Removes keys that start with one of the prefixes in $hidden (e.g. _, #).
ArrayFilterRecursiveTrait arrayFilterRecursive(array $values): array Recursively removes empty values (null, "", []); keeps 0 and false.
ArrayRecursiveRegexReplaceTrait arrayRecursiveRegexReplace(array &$array, string $pattern, string $replacement): void Runs preg_replace on every string in a nested array, in place.
TransformToArrayOfArraysTrait transformToArrayOfArrays(mixed $data, string $key, array $defaults = []): array Turns a string, a list of strings or a list of arrays into a list of arrays merged with $defaults.
TransformToArrayOfStringsTrait transformToArrayOfStrings(array $values, bool $slugify): array Trims, lowercases, de-duplicates and optionally slugifies values (not e-mail addresses), then sorts like arraySortPrioritized.

Sorting

Trait Method(s) What it does
ArraySortByListTrait arraySortByList(array $arrayToSort, array $orderList): array Moves the keys in $orderList to the front in that order; the other keys keep their order.
SortArrayByPriorityKeysTrait sortArrayByPriorityKeys(array $array, array $priorityKeys): array Moves the keys in $priorityKeys to the front; the other keys follow, sorted alphabetically.
SortArrayRecursiveByKeysTrait sortArrayRecursiveByKeys(array $array): array Sorts an array and all nested arrays by key (ksort).
ArraySortPrioritizedTrait arraySortPrioritized(array $values): array Lowercases and de-duplicates strings; returns e-mail addresses first, then the rest, each sorted.

Strings

Trait Method(s) What it does
StringOrNullTrait stringOrNull(?string $value): ?string Trims whitespace and /; returns null for an empty result.
LowerCaseStringOrNullTrait lowerCaseStringOrNull(?string $value): ?string (public) Like stringOrNull, and lowercases the result.
SlugifyTrait slugify(string $value): string Turns a string into a URL slug (via cocur/slugify).
ImplodeUniqueTrait implodeUnique(array $strings, string $glue = ", "): string, implodeUniqueSorted(...) Joins trimmed, unique, non-empty strings; the sorted variant puts longer strings first.
RenderTemplateTrait renderTemplate(string $template, array $data): string Replaces {{ key }} placeholders with values from $data; unknown placeholders stay.
TruncateLeftTrait truncateLeft(string $input, int $maxLength = 36): string Cuts a string from the left to $maxLength characters, prefixed with ... .
StringMatchesStringsTrait stringMatchesStrings(string $target, array $strings, string $modifier = "i"): bool true if one of $strings occurs as whole word(s) in $target; special characters such as / or + are matched literally, an empty list gives false.
StringMatchesTwigTrait stringMatchesTwig(string $content): bool true if the string contains a Twig tag ({{ }}, {% %} or {# #}).
ExtractVersionTrait extractVersion(?string $input): ?string Returns the first version number in a string (7,1 becomes 7.1), or null.
ExtractSemverTrait extractSemverTrait(?string $input): ?string Returns the first dotted number in a string, or null.

Encoding and validation

Trait Method(s) What it does
Base64UrlTrait base64UrlEncode(string $input): string, base64UrlDecode(string $input): string URL-safe Base64 without padding.
EncodeDecodeStringTrait encodeFileContent / decodeFileContent, gzipEncodeString / gzipDecodeString, zlibEncodeString / zlibDecodeString Base64 only; Base64 then gzcompress; Base64 then gzdeflate (and the reverse). All take and return string.
IsValidYamlTrait isValidYaml(string $content): bool true if every ----separated document parses to a mapping, a list or nothing; empty input and plain scalars are invalid.

Formatting and miscellaneous

Trait Method(s) What it does
HumanReadableSizeTrait humanReadableSize(int $size, string $format = "Byte"): string (private) Formats a size, e.g. 1048576 as 1 MB.
HumanReadableTimeTrait humanReadableTime(float $microtime, int $decimals = 3): string (private) Formats seconds as 612ms, 1.612s, 1m1s or 1h1m1s.
InterpolateImportanceTrait `interpolateImportance(int $value, int $min, int $max,

Dependencies

Dependencies

ID Version
chrisullyott/php-filesize ^4.2.1
cocur/slugify ^4.3.0
ext-yaml *
ext-zlib *
jasny/dotkey ^2.1.0
php >=8.2
Details
Composer
2026-10-25 15:48:52 +00:00
0
proprietary
19 KiB
Assets (1)
Versions (5) View all
1.5.5 2026-10-25
1.5.4 2026-10-25
1.5.3 2026-10-25
1.4.3 2026-10-25
1.5.2 2026-10-25