PHPackages                             tomphp/transform - 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. tomphp/transform

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

tomphp/transform
================

A set of tools to make array\_map and array\_filter more friendly.

v0.2.4(10y ago)145065[1 issues](https://github.com/tomphp/php-transform/issues)MITPHPPHP &gt;=5.6

Since Jan 10Pushed 10y ago1 watchersCompare

[ Source](https://github.com/tomphp/php-transform)[ Packagist](https://packagist.org/packages/tomphp/transform)[ Docs](https://github.com/tomphp/php-transform)[ RSS](/packages/tomphp-transform/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (4)Versions (14)Used By (0)

Transform
=========

[](#transform)

[![Build Status](https://camo.githubusercontent.com/f3dec257cbf8b0d9d5d3b559f8947519e798500636186cc0c42c42cb3f8a0481/68747470733a2f2f7472617669732d63692e6f72672f746f6d7068702f7068702d7472616e73666f726d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/tomphp/php-transform)

Transform is a simple library which aims to make using PHP's `array_map`function a more pleasant experience - and resulting in cleaner code.

Transform is a collection of helper functions for common transform actions.

For a great companion library of predicates to make your `array_filter` code also look great, see [Pentothal](https://github.com/Giuseppe-Mazzapica/Pentothal).

Namespace
---------

[](#namespace)

From this point on, assume the `TomPHP\Transform` namespace is used like so:

```
use TomPHP\Transform as T;
```

Example
-------

[](#example)

Take this code:

```
$names = array_map(
    function ($user) {
        return $user->getName();
    },
    $allUsers
);
```

Using Transform this looks like this:

```
$names = array_map(T\callMethod('getName'), $allUsers);
```

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

[](#installation)

Using composer:

`composer require tomphp/transform`

Chaining
--------

[](#chaining)

Multiple transformations can be composed using the `chain` function:

```
T\chain(T\getProperty('user'), T\getElement('name'));

// Is equivalent to:

function ($object) {
    return $object->user['name'];
}
```

The Traversal Builder
---------------------

[](#the-traversal-builder)

If you want to chain together a collection of `callMethod`, `getProperty` and `getElement` calls, the `__()` function provides a builder to write this in a more elegant way.

Consider:

```
$dobs = array_map(
    function (User $user) {
        return $user->getMetaData()['dob']->format('Y-m-d');
    },
    $users
);
```

With the builder you can simply write:

```
use function TomPHP\Transform\__;

$dobs = array_map(__()->getMetaData()['dob']->format('Y-m-d'), $users);
```

Transformations
---------------

[](#transformations)

### Object Transformations

[](#object-transformations)

#### T\\callMethod(string $methodName, mixed ...$args)

[](#tcallmethodstring-methodname-mixed-args)

```
T\classMethod('getName');

// Is equivalent to:

function ($object) {
    return $object->getName();
}
```

```
T\classMethod('format', 'Y-m-d');

// Is equivalent to:

function ($object) {
    return $object->format('Y-m-d');
}
```

#### T\\callStatic(string $methodName, mixed ...$args)

[](#tcallstaticstring-methodname-mixed-args)

```
T\callStatic('getSomethingWith', 'param1', 'param2');

// Is equivalent to:

function ($object) {
    return $object::getSomethingWith('param1', 'param2');
}

// or to:
function ($classAsString) {
    return $classAsString::getSomethingWith('param1', 'param2');
}
```

#### T\\getProperty(string $name)

[](#tgetpropertystring-name)

```
T\getProperty('name');

// Is equivalent to:

function ($object) {
    return $object->name;
}
```

### Array Transformations

[](#array-transformations)

#### T\\getElement(string|int $name)

[](#tgetelementstringint-name)

```
T\getElement('name');

// Is equivalent to:

function ($array) {
    return $array['name'];
}
```

```
T\getElement(['user', 'name']);

// Is equivalent to:

function ($array) {
    return $array['user']['name'];
}
```

### String Transformations

[](#string-transformations)

#### T\\prepend(string $prefix)

[](#tprependstring-prefix)

```
T\prepend('prefix: ');

// Is equivalent to:

function ($value) {
    return 'prefix: ' . $value;
}
```

#### T\\append(string $suffix)

[](#tappendstring-suffix)

```
T\append(' - suffix');

// Is equivalent to:

function ($value) {
    return $value . ' - suffix';
}
```

#### T\\concat(...$arguments)

[](#tconcatarguments)

Use `__` as the placeholder for where you want the value inserted:

```
use const TomPHP\Transform\__;

T\concat('Hello ', __, '!');

// Is equivilent to:

function ($value) {
    return 'Hello ' . $value . '!';
}
```

### Generic Transformations

[](#generic-transformations)

#### T\\argumentTo(callable $callable, array $argments = \[\_\_\])

[](#targumenttocallable-callable-array-argments--__)

```
T\argumentTo('strtolower');

// Is equivalent to:

function ($value) {
    return strtolower($value);
}
```

You can also provide a list of arguments using `__` as the placeholder for where you want the value inserted:

```
use const TomPHP\Transform\__;

T\argumentTo('strpos', ['Tom: My name is Tom', __, 4]);

// Is equivalent to:

function ($value) {
    return strpos('Tom: My name is Tom', $value, 4);
}
```

#### T\\newInstance(string $className, array $arguments = \[\_\_\])

[](#tnewinstancestring-classname-array-arguments--__)

```
T\newInstance(Widget::class);

// Is equivalent to:

function ($value) {
    return new Widget($value);
}
```

You can also provide a list of arguments using `__` as the placeholder for where you want the value inserted:

```
use const TomPHP\Transform\__;

T\newInstance(Widget, ['first', __, 'last']);

// Is equivalent to:

function ($value) {
    return new Widget('first', $value, 'last');
}
```

#### T\\valueOrDefault(mixed $default, callable $predicate = null)

[](#tvalueordefaultmixed-default-callable-predicate--null)

```
T\valueOrDefault('default')

// Is equivalent to:

function ($value) {
    return $value ?: 'default';
}
```

A predicate function can also be supplied:

```
T\valueOrDefault('negative number', function ($value) {
    return $value >= 0;
})

// Is equivalent to:

function ($value) {
    return $value >= 0 ? $value : 'negative number';
}
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.5% 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

Every ~6 days

Recently: every ~12 days

Total

13

Last Release

3701d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1746ea71ef8c7feaef0a3f79a01ee55dca368191b4eaf0327e5e80c7bfba7441?d=identicon)[tomphp](/maintainers/tomphp)

---

Top Contributors

[![tomphp](https://avatars.githubusercontent.com/u/1959183?v=4)](https://github.com/tomphp "tomphp (49 commits)")[![gmazzap](https://avatars.githubusercontent.com/u/2208282?v=4)](https://github.com/gmazzap "gmazzap (7 commits)")[![danizord](https://avatars.githubusercontent.com/u/1850941?v=4)](https://github.com/danizord "danizord (1 commits)")[![kerro](https://avatars.githubusercontent.com/u/14033920?v=4)](https://github.com/kerro "kerro (1 commits)")

---

Tags

mapfunctiontransformfunctional-programmingarray\_map

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/tomphp-transform/health.svg)

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

###  Alternatives

[phpcollection/phpcollection

General-Purpose Collection Library for PHP

1.0k64.0M34](/packages/phpcollection-phpcollection)[dasprid/enum

PHP 7.1 enum implementation

379146.0M11](/packages/dasprid-enum)[marc-mabe/php-enum

Simple and fast implementation of enumerations with native PHP

49444.8M97](/packages/marc-mabe-php-enum)[aimeos/map

Easy and elegant handling of PHP arrays as array-like collection objects similar to jQuery and Laravel Collections

4.2k412.9k11](/packages/aimeos-map)[spatie/geocoder

Geocoding addresses to coordinates

8404.8M15](/packages/spatie-geocoder)[dusank/knapsack

Collection library for PHP

5351.0M25](/packages/dusank-knapsack)

PHPackages © 2026

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