PHPackages                             district5/formatter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. district5/formatter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

district5/formatter
===================

District5 Formatter Library

1.0.0(2y ago)099mitPHPPHP &gt;=8.1

Since Apr 29Pushed 2y agoCompare

[ Source](https://github.com/district-5/php-formatter)[ Packagist](https://packagist.org/packages/district5/formatter)[ Docs](https://github.com/district-5/php-formatter)[ RSS](/packages/district5-formatter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

District5 PHP Core Libraries - Formatter
========================================

[](#district5-php-core-libraries---formatter)

Installation
------------

[](#installation)

Use composer to add this library as a dependency onto your project.

```
composer require district5/formatter
```

Usage
-----

[](#usage)

### Using a formatter

[](#using-a-formatter)

Calling a formatter can be done with a single line of code:

```
// single object for format
$formatted = MyFormatter::formatSingle($model);

// multiple objects for format
$formatted = MyFormatter::formatMultiple($models);
```

### Creating a formatter

[](#creating-a-formatter)

Create your own formatter by extending `FormatterAbstract` and implementing the formatSingle function. The abstract will automatically give you the `formatMultiple` functionality.

```
use District5\Formatter\FormatterAbstract;

class MyFormatter extends FormatterAbstract
{
    public static function formatSingle($item, array $options = null)
    {
        return [
            'field1' => 'someValue',
            'field2' => 5
        ];
    }
}
```

`FormatterAbstract` also includes a function to aid with more complex decisions for content inclusion:

```
use District5\Formatter\FormatterAbstract;

class MyComplexFormatter extends FormatterAbstract
{
    public static function formatSingle($item, array $options = null)
    {
        $formatted = array(
            'id' => $item->getIdStr(),
            'created' => $item->getCreatedDateMillis(),
            'title' => $item->getTitle(),
            'text' => $item->getText()
        );

        if (false !== static::getOption('includeCoverImage', $options, false)) {
            $formatted['coverImage'] = $item->getCoverImagePath();
        }

        if (false !== static::getOption('includeOwnerId', $options, false)) {
            $formatted['ownerId'] = $item->getUserId(true);
        }

        return $formatted;
    }
}
```

This could then be called with the following:

```
// single object for format
$formatted = MyComplexFormatter::formatSingle(
    $model,
    [
        'includeCoverImage' => true
    ]
);

// multiple objects for format
$formatted = MyComplexFormatter::formatMultiple(
    $models,
    [
        'includeCoverImage' => true,
        'includeOwnerId' => true
    ]
);
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

748d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5de4b9a90eadf6b595e1b5efc802683f2358da5661c616820b90c922f8aa8e05?d=identicon)[rogerthomas84](/maintainers/rogerthomas84)

![](https://www.gravatar.com/avatar/c566343bfe6b0a3602622dd1def882992f10ff114b6a95ffb000da2ba9e22578?d=identicon)[mark-morgan](/maintainers/mark-morgan)

---

Top Contributors

[![mark-morgan](https://avatars.githubusercontent.com/u/1934581?v=4)](https://github.com/mark-morgan "mark-morgan (6 commits)")

---

Tags

phpformatterdistrict5

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/district5-formatter/health.svg)

```
[![Health](https://phpackages.com/badges/district5-formatter/health.svg)](https://phpackages.com/packages/district5-formatter)
```

###  Alternatives

[kartik-v/php-date-formatter

A Javascript datetime formatting and manipulation library using PHP date-time formats.

461.5M3](/packages/kartik-v-php-date-formatter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
