Derafu: Support
A collection of essential PHP utility classes for the common work of an application: strings, arrays, dates, files, CSV, objects, IP addresses and URL paths.
Every class is final and has only static methods. They keep no state: what a call does depends on its arguments (and on the clock or the file system, in the classes whose job is dates or files), so it gives the same answer anywhere and is easy to test.
The classes
| Class | What it is for | Page |
|---|---|---|
Str |
Placeholders, case conversion, slugs, multibyte padding, UUIDs and random strings. | Strings |
Caster |
Turns a text into the scalar that it says ("42" is 42). |
Strings |
Encoding |
UTF-8 and ISO-8859-1, also inside arrays and objects. | Strings |
Arr |
Dot notation, casting, trees, tables and subsets. | Arrays |
Date |
Spanish formatting, periods (YYYYMM), working days and units of time. |
Dates |
File |
Atomic writes, MIME types, ZIP and downloads. | Files and CSV |
Csv |
Reading, writing, generating and sending CSV. | Files and CSV |
Obj, Factory, Hydrator |
Create objects from arrays and look at them. | Objects |
Serialization, JsonSerializer |
Is it serializable, and objects to JSON. | Objects |
Debug |
Information about a variable and where it was inspected. | Objects |
Ip |
IPv4 and IPv6: validation, normalization, ranges and networks. | Ip |
Url |
The canonical form of the paths of URLs, and prefixes by segments. | Url |
Why Derafu\Support?
It solves needs of business and data processing that the standard library leaves out:
- Business dates: working days, holidays, periods (
YYYYMM) and ranges that skip weekends. - CSV across systems: separators, enclosures and encodings.
- Data transformations: trees, tables and lists, without losing the data.
- Safe files: atomic writes, errors that say what failed, and the MIME type of a file.
- Addresses that are handled right: IPv6, ranges and networks, with the same canonical form for the same address.
Install
composer require derafu/support
Only derafu/translation is required (see Errors). The libraries that some classes use are not required, because a project that does not use those classes does not need them. Install what you use:
| Package | Needed by |
|---|---|
nesbot/carbon |
Date (all of it). |
league/csv (9.27 or later) |
Csv (all of it). |
symfony/mime |
File::mimetype(), and File::send() for the type of the file it sends. |
symfony/filesystem |
File::rmdir(), which does not follow symbolic links. |
maennchen/zipstream-php |
File::zip() and File::compress(). |
ext-zip |
File::unzip(). |
The string classes (Str, Encoding) use the functions of mbstring.
Errors
The errors that the classes throw are translatable: they are the standard exceptions of PHP (InvalidArgumentException, RuntimeException, LogicException) in the translatable version of derafu/translation, so catching the standard class works, and the message is also available as a message id with its parameters.
The Spanish translations ship with the package. To have them in your translator, import its services (or add SupportTranslationResourceProvider to the resource providers of the translator):
imports:
- { resource: '../vendor/derafu/support/resources/config/support-services.yaml' }
derafu/foundation already does it.