PHPackages                             tbpixel/type-adapter - 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. tbpixel/type-adapter

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

tbpixel/type-adapter
====================

A type-safe package for adapting from one data type to another.

1.1.0(7y ago)049MITPHPPHP ^7.1

Since Mar 20Pushed 7y ago1 watchersCompare

[ Source](https://github.com/TBPixel/type-adapter)[ Packagist](https://packagist.org/packages/tbpixel/type-adapter)[ RSS](/packages/tbpixel-type-adapter/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (2)Dependencies (5)Versions (5)Used By (0)

Type Adapter
============

[](#type-adapter)

[![Latest Version on Packagist](https://camo.githubusercontent.com/6443ecd0a9d1f12e4be6f38204ff3c750bac2655acd7daf8db4e0d9e712e18c9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f5442506978656c2f747970652d616461707465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tbpixel/type-adapter)[![Build Status](https://camo.githubusercontent.com/72536c94dc0721b3a4804fc6e514633cc6b98dc695cbe44aab08804abfee0e0d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f5442506978656c2f747970652d616461707465722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/TBPixel/type-adapter)

A type-safe package for adapting from one data type to another.

Contents
--------

[](#contents)

- [Installation](#installation)
- [Purpose](#purpose)
- [Examples](#examples)
- [Contributing](#contributing)
- [Changelog](#changelog)
- [Support Me](#support-me)
- [License](#license)

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

[](#installation)

Via composer:

```
composer require tbpixel/type-adapter
```

Purpose
-------

[](#purpose)

Have you ever needed to convert one instance (usually a [data transfer object](https://github.com/spatie/data-transfer-object)) from one type to another? I found myself writing static constructors like:

```
class Foo
{
    /** @var string **/
    public $name;
}

class Bar
{
    /** @var string **/
    public $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public static function fromFoo(Foo $foo): self
    {
        return new self($foo->name);
    }
}
```

In all honesty this is fine for a start, but it really creates tight coupling between objects in order to handle these constructor conversions. If I wanted to remove `Foo` entirely, I'd have to go and find every place where I called `fromFoo` on the `Bar` class! This can get even worse if the source type comes from a package; it may become critical to decouple this package from an application and yet references to it are now litered all through out *my* application.

This why I created the Type Adapter package. This package allows for loosely coupled, type safe conversions without static constructors. It attempts to reduce coupling and act as a communcation converter between the layers of your application. It is particularly useful in hexagonal architected, and by extension domain driven application, software design.

Examples
--------

[](#examples)

Taking the types from above, we could create an adapter that converts from `Foo` to `Bar` like so:

```
use TBPixel\TypeAdapter\Adaptable;

class FooToBarAdapter implements Adaptable
{
    /**
     * Adapts a Foo resource into a Bar resource.
     *
     * @param Foo $resource
     *
     * @return Bar
     */
    public function adapt($resource)
    {
        return new Bar($resource->name);
    }

    /**
     * Returns the acceptable valid resource type as either a string or an array.
     *
     * @return array|string
     */
    public function expects()
    {
        return Foo::class;
    }
}
```

You can see a couple of things going on here. For starters, we implement the `Adaptable` interface defined in this package. This interface requires us to implement an `adapt($resource)` method and a `expects()` method.

The `adapt` method accepts in the resource, which we can type hint using a doc block comment, and then returns the converted type.

The `expects` method is for type safety. It defines out a validation rule for the `adapt` method to expect. This is what allows us type safety.

The `expects` method can either return a `string` type name or an `array` of strings, for variadic types.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

### Support Me

[](#support-me)

Hi! I'm a developer living in Vancouver, BC and boy is the housing market tough. If you wanna support me consider following me on [Twitter @TBPixel](https://twitter.com/TBPixel), or maybe [buying me a coffee](https://ko-fi.com/tbpixel). Thanks!

License
-------

[](#license)

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

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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 ~0 days

Total

2

Last Release

2613d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e95171b17d11979d1512cd550d27ce6216fe493b39cf8a5853bda127ed470eb0?d=identicon)[TBPixel](/maintainers/TBPixel)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tbpixel-type-adapter/health.svg)

```
[![Health](https://phpackages.com/badges/tbpixel-type-adapter/health.svg)](https://phpackages.com/packages/tbpixel-type-adapter)
```

PHPackages © 2026

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