Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/decodelabs/dictum
Text formatting tools for PHP
https://github.com/decodelabs/dictum
date formatting number php text-formatting
Last synced: 17 days ago
JSON representation
Text formatting tools for PHP
- Host: GitHub
- URL: https://github.com/decodelabs/dictum
- Owner: decodelabs
- License: mit
- Created: 2021-04-13T16:45:14.000Z (almost 4 years ago)
- Default Branch: develop
- Last Pushed: 2023-12-06T22:50:50.000Z (about 1 year ago)
- Last Synced: 2024-03-26T22:13:22.308Z (10 months ago)
- Topics: date, formatting, number, php, text-formatting
- Language: PHP
- Homepage:
- Size: 157 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Dictum
[![PHP from Packagist](https://img.shields.io/packagist/php-v/decodelabs/dictum?style=flat)](https://packagist.org/packages/decodelabs/dictum)
[![Latest Version](https://img.shields.io/packagist/v/decodelabs/dictum.svg?style=flat)](https://packagist.org/packages/decodelabs/dictum)
[![Total Downloads](https://img.shields.io/packagist/dt/decodelabs/dictum.svg?style=flat)](https://packagist.org/packages/decodelabs/dictum)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/decodelabs/dictum/integrate.yml?branch=develop)](https://github.com/decodelabs/dictum/actions/workflows/integrate.yml)
[![PHPStan](https://img.shields.io/badge/PHPStan-enabled-44CC11.svg?longCache=true&style=flat)](https://github.com/phpstan/phpstan)
[![License](https://img.shields.io/packagist/l/decodelabs/dictum?style=flat)](https://packagist.org/packages/decodelabs/dictum)### Text formatting tools for PHP
Dictum provides a collection of commonly required text parsing and processing features.
_Get news and updates on the [DecodeLabs blog](https://blog.decodelabs.com)._
---
## Installation
Install via Composer:
```bash
composer require decodelabs/dictum
```## Usage
### Importing
Dictum uses [Veneer](https://github.com/decodelabs/veneer) to provide a unified frontage under
DecodeLabs\Dictum
.
You can access all the primary functionality via this static frontage without compromising testing and dependency injection.### Formatters
The main Veneer frontage of Dictum exposes a set of predictable text / key formatters which can be used to quickly prepare strings for specific actions.
```php
use DecodeLabs\Dictum;echo Dictum::name('geoff-randomName');
// Geoff Random Nameecho Dictum::firstName('geoff-randomName');
// Geoffecho Dictum::initials('geoff-randomName');
// GRNecho Dictum::initialsAndSurname('geoff-randomName');
// GR Nameecho Dictum::initialMiddleNames('geoff-randomName');
// Geoff R Nameecho Dictum::consonants('here\'s a Random-string of text');
// hr's Rndm-strng f txtecho Dictum::label('here\'s a Random-string of text');
// Here's a random string of textecho Dictum::id('here\'s a Random-string of text');
// HeresARandomStringOfTextecho Dictum::camel('here\'s a Random-string of text');
// heresARandomStringOfTextecho Dictum::constant('here\'s a Random-string of text');
// HERE_S_A_RANDOM_STRING_OF_TEXTecho Dictum::slug('here\'s a Random-string of text / other stuff');
// heres-a-random-string-of-text-other-stuffecho Dictum::pathSlug('here\'s a Random-string of text / other stuff');
// heres-a-random-string-of-text/other-stuffecho Dictum::actionSlug('here\'s a Random-string of text / other stuff');
// here's-a-random-string-of-text-/-other-stuffecho Dictum::fileName('here\'s a Random-string of text / other stuff');
// here's-a-Random-string-of-text-_-other-stuffecho Dictum::shorten('here\'s a Random-string of text / other stuff', 10);
// here's a…echo Dictum::numericToAlpha(23345452);
// aybfraecho Dictum::alphaToNumeric('aybfra')
// 23345452echo Dictum::toBoolean('yes') ? 'true' : 'false';
// true
```### Text
The formatters above predominantly use the
Text
class to process the strings provided. This class exposes a full suite of multibyte aware string manipulation functionality in an immutable collection format.For example, the above
id()
method is defined as:```php
echo (new Text($id))
->toUtf8()
->toAscii()
->regexReplace('([^ ])([A-Z])', '\\1 \\2')
->replace(['-', '.', '+'], ' ')
->regexReplace('[^a-zA-Z0-9_ ]', '')
->toTitleCase()
->replace(' ', '')
->__toString();
```Note, regexes are based off the mb_ereg functions and as such do not use delimiters in their patterns.
## Plugins
Dictum provides generic interfaces for defining locale-aware formatter plugins that can be implemented by different output generators.
Currently, Time and Number are available, defining predictable methods for formatting dates and various forms of number.
Dictum offers a plain text version of these interfaces:
```php
use DecodeLabs\Dictum;// Custom format
Dictum::$time->format('now', 'd/m/Y', 'Europe/London');// Locale format
// When timezone is true it is fetched from Cosmos::$timezone
Dictum::$time->locale('now', 'long', 'long', true);// Locale shortcuts
Dictum::$time->dateTime('tomorrow'); // medium
Dictum::$time->longTime('yesterday');
Dictum::$time->shortDate('yesterday');
// ...etc// Intervals
Dictum::$time->since('yesterday'); // 1 day ago
Dictum::$time->until('tomorrow'); // 1 day from now
Dictum::$time->sinceAbs('yesterday'); // 1 day
Dictum::$time->untilAbs('yesterday'); // -1 day
Dictum::$time->between('yesterday', 'tomorrow'); // 1 day// Numbers
Dictum::$number->format(16.5, 'px'); // 16.5 px
Dictum::$number->format(16.5, 'px', 'de'); // 16,5 px
Dictum::$number->decimal(16.534643, 2); // 16.53
Dictum::$number->currency(16.534643, 'GBP'); // £16.53
Dictum::$number->percent(16.534643, 50, 2); // 33.07%
Dictum::$number->scientific(16.534643); // 1.6534643E1
Dictum::$number->spellout(16.534643); // sixteen point five three four six four three
Dictum::$number->ordinal(16.534643); // 17th
Dictum::$number->diff(16.534643); // ⬆ 16.535
Dictum::$number->fileSize(16534643); // 15.77 MiB
Dictum::$number->fileSizeDec(16534643); // 16.53 MB
```See [Tagged](https://github.com/decodelabs/tagged) for equivalent HTML implementations of these interfaces.
## Licensing
Dictum is licensed under the MIT License. See [LICENSE](./LICENSE) for the full license text.