PHPackages                             alexthekiwi/tx - 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. alexthekiwi/tx

ActiveLibrary

alexthekiwi/tx
==============

A fluent casting library for PHP.

01PHP

Since Apr 9Pushed 1y ago1 watchersCompare

[ Source](https://github.com/alexthekiwi/tx)[ Packagist](https://packagist.org/packages/alexthekiwi/tx)[ RSS](/packages/alexthekiwi-tx/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Tx - Laravel Type Transformation Package
========================================

[](#tx---laravel-type-transformation-package)

A fluent, chainable transformation and validation utility for Laravel applications. Tx makes it easy to transform, cast, and validate values in your Laravel applications, with built-in support for dates and money transformations.

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

[](#installation)

You can install the package via composer:

```
composer require alexthekiwi/tx
```

Then publish the configuration file:

```
php artisan vendor:publish --provider="Alexthekiwi\Tx\TxServiceProvider" --tag="config"
```

Features
--------

[](#features)

- Fluent, chainable API
- Strong type conversions
- Date transformations using Carbon
- Money/Price transformations using brick/money
- Fallbacks and error handling
- Extensible with custom transformers

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

```
use function Alexthekiwi\Tx\tx;

// Basic type conversions
$intValue = tx('42')->toInt(); // 42
$floatValue = tx('3.14')->toFloat(); // 3.14
$boolValue = tx(1)->toBool(); // true
```

### Working with Dates

[](#working-with-dates)

```
// Parse a date
$date = tx('2023-01-15')->toDate(); // Returns Carbon instance

// Parse a date with a specific format
$date = tx('15/01/2023')->fromFormat('d/m/Y')->toDate();

// Chain Carbon methods
$formattedDate = tx('2023-01-15')->toDate()->format('F j, Y'); // January 15, 2023
$nextMonth = tx('2023-01-15')->toDate()->addMonth()->format('Y-m-d'); // 2023-02-15
```

### Working with Money

[](#working-with-money)

```
// Create a Money instance from a numeric value
$money = tx(100)->toPrice(); // $100 USD

// Format money values
$formattedPrice = tx(100)->toPrice()->getAmount(); // "100.00"

// Format with currency
$formattedWithCurrency = tx(100)->toPrice()->formatTo('en_US'); // "$100.00"

// Perform calculations
$discountedPrice = tx(100)->toPrice()->multipliedBy('0.9'); // $90 USD
```

### Error Handling

[](#error-handling)

```
// Using fallbacks
$safeInt = tx('not-a-number')->fallback(0)->toInt(); // 0

// Throwing exceptions
try {
    $value = tx('not-a-number')->throw()->toInt();
} catch (\Alexthekiwi\Tx\Exceptions\CannotTxException $e) {
    // Handle exception
}

// Using custom error handling
$customHandled = tx('not-a-number')
    ->failed(function($exception, $originalValue) {
        // Log the error
        Log::error("Failed to cast value: {$originalValue}", [
            'exception' => $exception,
        ]);

        return 0; // Return a default value
    })
    ->toInt();
```

### Advanced Usage

[](#advanced-usage)

```
// Chaining multiple transformations
$result = tx('{"date": "2023-01-15", "amount": 100}')
    ->toCollection()
    ->map(function ($item, $key) {
        if ($key === 'date') {
            return tx($item)->toDate();
        }

        if ($key === 'amount') {
            return tx($item)->toPrice();
        }

        return $item;
    });

// Custom transformers
$result = tx($value)
    ->using(new MyCustomTransformer())
    ->toCustomFormat();
```

Configuration
-------------

[](#configuration)

You can customize the default behavior by modifying the `config/tx.php` file:

```
return [
    // Default currency for money transformations
    'currency' => env('TX_DEFAULT_CURRENCY', 'USD'),

    'casters' => [
        'date' => [
            'class' => \Alexthekiwi\Tx\Transformers\TxCarbon::class,
            'default_format' => 'Y-m-d H:i:s',
        ],
        'price' => [
            'class' => \Alexthekiwi\Tx\Transformers\TxBrickMoney::class,
        ],
    ],
];
```

Extending Tx
------------

[](#extending-tx)

You can create your own transformers by implementing the appropriate interface:

```
use Alexthekiwi\Tx\Contracts\TxDateContract;
use Carbon\Carbon;

class MyCustomDateTransformer implements TxDateContract
{
    public function toDate(mixed $value, ?string $format = null): ?Carbon
    {
        // Custom implementation
    }
}
```

Then update your config to use your custom transformer:

```
'casters' => [
    'date' => [
        'class' => \App\Transformers\MyCustomDateTransformer::class,
    ],
],
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity15

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/02e06d939be026746151b711568e4397d5f9f4d1b6ba54cab3db12f14ccca04c?d=identicon)[alexclark-nz](/maintainers/alexclark-nz)

---

Top Contributors

[![alexthekiwi](https://avatars.githubusercontent.com/u/38124722?v=4)](https://github.com/alexthekiwi "alexthekiwi (1 commits)")

### Embed Badge

![Health badge](/badges/alexthekiwi-tx/health.svg)

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

PHPackages © 2026

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