PHPackages                             eloquent/endec - 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. eloquent/endec

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

eloquent/endec
==============

Versatile encoding implementations for PHP.

0.2.1(11y ago)98.9k[2 issues](https://github.com/eloquent/endec/issues)MITPHPPHP &gt;=5.3

Since Mar 19Pushed 11y ago1 watchersCompare

[ Source](https://github.com/eloquent/endec)[ Packagist](https://packagist.org/packages/eloquent/endec)[ Docs](https://github.com/eloquent/endec)[ RSS](/packages/eloquent-endec/feed)WikiDiscussions develop Synced 3d ago

READMEChangelog (4)Dependencies (6)Versions (6)Used By (0)

Endec
=====

[](#endec)

*Versatile encoding implementations for PHP.*

[![The most recent stable version is 0.2.1](https://camo.githubusercontent.com/027fcc445a6e5c85d5e79cd0de653860761e589def48e0f9a4c1bea49cc9987e/687474703a2f2f696d672e736869656c64732e696f2f3a73656d7665722d302e322e312d79656c6c6f772e737667 "This project uses semantic versioning")](http://semver.org/)[![Current build status image](https://camo.githubusercontent.com/b48882d305c8416a127e1e790892a9b5c840e4b2dcfdba75fc6f659796bdde22/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f656c6f7175656e742f656e6465632f646576656c6f702e737667 "Current build status for the develop branch")](https://travis-ci.org/eloquent/endec)[![Current coverage status image](https://camo.githubusercontent.com/eab9952bfb2cb04c48a62c189e4fdb8fab84e052186fa5aef310c00a83d1e16d/687474703a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f656c6f7175656e742f656e6465632f646576656c6f702e737667 "Current test coverage for the develop branch")](https://coveralls.io/r/eloquent/endec)

Installation and documentation
------------------------------

[](#installation-and-documentation)

- Available as [Composer](http://getcomposer.org/) package [eloquent/endec](https://packagist.org/packages/eloquent/endec).
- [API documentation](http://lqnt.co/endec/artifacts/documentation/api/) available.

What is Endec?
--------------

[](#what-is-endec)

*Endec* is a general-purpose encoding library for PHP that supports encoding and decoding of streaming data in addition to regular string-based methods. *Endec*comes with a selection of common encodings, is easy to use, and is simple to extend with custom encodings if necessary.

Usage
-----

[](#usage)

### Strings

[](#strings)

*Endec* can work with strings, similar to encoding functions in the PHP standard library. All codec classes have a static `instance()` method as a convenience only (they are not singletons).

```
use Eloquent\Endec\Base32\Base32;

$codec = new Base32;
echo $codec->encode('foobar'); // outputs 'MZXW6YTBOI======'
echo $codec->decode('MZXW6YTBOI======'); // outputs 'foobar'

echo Base32::instance()->encode('foobar'); // outputs 'MZXW6YTBOI======'
echo Base32::instance()->decode('MZXW6YTBOI======'); // outputs 'foobar'
```

### Stream filters

[](#stream-filters)

PHP natively supports [stream filters](http://php.net/stream.filters). Any number of filters can be added to any stream with [stream\_filter\_append](http://php.net/stream_filter_append) or [stream\_filter\_prepend](http://php.net/stream_filter_prepend), and removed with [stream\_filter\_remove](http://php.net/stream_filter_remove). All of *Endec*'s encodings are available as [stream filters](#built-in-stream-filters).

```
use Eloquent\Endec\Endec;

Endec::registerFilters();
$path = '/path/to/file';

$stream = fopen($path, 'wb');
stream_filter_append($stream, 'endec.base32-encode');
fwrite($stream, 'fo');
fwrite($stream, 'ob');
fwrite($stream, 'ar');
fclose($stream);
echo file_get_contents($path); // outputs 'MZXW6YTBOI======'

$stream = fopen($path, 'rb');
stream_filter_append($stream, 'endec.base32-decode');
$data = fread($stream, 3);
$data .= fread($stream, 3);
$data .= fread($stream, 2);
fclose($stream);
echo $data; // outputs 'foobar'
```

### React streams

[](#react-streams)

Streams can be obtained from an [encoder, decoder, or codec](#encoders-decoders-and-codecs). *Endec*'s streams implement both [WritableStreamInterface](https://github.com/reactphp/react/blob/v0.4.0/src/Stream/WritableStreamInterface.php) and [ReadableStreamInterface](https://github.com/reactphp/react/blob/v0.4.0/src/Stream/ReadableStreamInterface.php) from the [React](http://reactphp.org/)library, and hence can be used in an asynchronous manner.

```
use Eloquent\Endec\Base32\Base32;

$codec = new Base32;
$encodeStream = $codec->createEncodeStream();
$decodeStream = $codec->createDecodeStream();

$encoded = '';
$encodeStream->on(
    'data',
    function ($data, $stream) use (&$encoded) {
        $encoded .= $data;
    }
);

$decoded = '';
$decodeStream->on(
    'data',
    function ($data, $stream) use (&$decoded) {
        $decoded .= $data;
    }
);

$encodeStream->pipe($decodeStream);

$encodeStream->write('fo');
$encodeStream->write('ob');
$encodeStream->end('ar');

echo $encoded; // outputs 'MZXW6YTBOI======'
echo $decoded; // outputs 'foobar'
```

### Handling errors

[](#handling-errors)

Handling errors when dealing with strings is as simple as catching an exception. All exceptions thrown implement [TransformExceptionInterface](http://lqnt.co/endec/artifacts/documentation/api/Eloquent/Endec/Transform/Exception/TransformExceptionInterface.html):

```
use Eloquent\Endec\Base32\Base32;
use Eloquent\Endec\Exception\EncodingExceptionInterface;

$codec = new Base32;
try {
    $codec->decode('!!!!!!!!');
} catch (EncodingExceptionInterface $e) {
    echo 'Unable to decode';
}
```

When using stream filters, error handling is difficult because PHP seems to simply [ignore errors produced by the filter](https://bugs.php.net/bug.php?id=66932). If 0 bytes are written by `fwrite()`, it's a fair indication that an error occurred:

```
use Eloquent\Endec\Endec;

Endec::registerFilters();
$path = '/path/to/file';

$stream = fopen($path, 'wb');
stream_filter_append($stream, 'endec.base32-decode');
if (!fwrite($stream, '!!!!!!!!')) {
    echo 'Unable to decode';
}
fclose($stream);
```

When using [React](http://reactphp.org/) streams, simply handle the `error` event:

```
use Eloquent\Endec\Base32\Base32;

$codec = new Base32;
$decodeStream = $codec->createDecodeStream();

$decodeStream->on(
    'error',
    function ($error, $stream) {
        echo 'Unable to decode';
    }
);

$decodeStream->end('!!!!!!!!');
```

Built-in encodings
------------------

[](#built-in-encodings)

*Endec* supports a number of common encodings out of the box. Where there is a relevant specification document, *Endec* aims to be 100% spec-conformant. Available encodings include:

- [Base64](http://tools.ietf.org/html/rfc4648#section-4) from [RFC 4648](http://tools.ietf.org/html/rfc4648)
- [Base64 for MIME message bodies](http://tools.ietf.org/html/rfc2045#section-6.8) from [RFC 2045](http://tools.ietf.org/html/rfc2045)
- [Base64 with URL and filename safe alphabet](http://tools.ietf.org/html/rfc4648#section-5) from [RFC 4648](http://tools.ietf.org/html/rfc4648)
- [Base32](http://tools.ietf.org/html/rfc4648#section-6) from [RFC 4648](http://tools.ietf.org/html/rfc4648)
- [Base32 with extended hexadecimal alphabet](http://tools.ietf.org/html/rfc4648#section-7) from [RFC 4648](http://tools.ietf.org/html/rfc4648)
- [Base16 (hexadecimal)](http://tools.ietf.org/html/rfc4648#section-8) from [RFC 4648](http://tools.ietf.org/html/rfc4648)
- [URI percent encoding](http://tools.ietf.org/html/rfc3986#section-2.1) from [RFC 3986](http://tools.ietf.org/html/rfc3986)

Built-in stream filters
-----------------------

[](#built-in-stream-filters)

All *Endec* encodings are available as stream filters. Filters must be registered globally before use by calling `Endec::registerFilters()` (it is safe to call this method multiple times). Available stream filters include:

- endec.base64-encode
- endec.base64-decode
- endec.base64mime-encode (see also PHP's [convert.base64-encode](http://php.net/filters.convert))
- endec.base64mime-decode (see also PHP's [convert.base64-decode](http://php.net/filters.convert))
- endec.base64url-encode
- endec.base64url-decode
- endec.base32-encode
- endec.base32-decode
- endec.base32hex-encode
- endec.base32hex-decode
- endec.base16-encode
- endec.base16-decode
- endec.uri-encode
- endec.uri-decode

Encoders, decoders, and codecs
------------------------------

[](#encoders-decoders-and-codecs)

Most of the functionality of *Endec* is provided through *encoders*, *decoders*, and *codecs* (codecs are simply a combination of an encoder and decoder). All of *Endec*'s built-in encodings are implemented as codecs, but it is also possible to implement a standalone encoder or decoder.

All encoders implement [EncoderInterface](http://lqnt.co/endec/artifacts/documentation/api/Eloquent/Endec/EncoderInterface.html), all decoders implement [DecoderInterface](http://lqnt.co/endec/artifacts/documentation/api/Eloquent/Endec/DecoderInterface.html), and all codecs implement [EncoderInterface](http://lqnt.co/endec/artifacts/documentation/api/Eloquent/Endec/EncoderInterface.html), [DecoderInterface](http://lqnt.co/endec/artifacts/documentation/api/Eloquent/Endec/DecoderInterface.html), and [CodecInterface](http://lqnt.co/endec/artifacts/documentation/api/Eloquent/Endec/CodecInterface.html). This allows for type hints to accurately express requirements.

Implementing a custom encoding
------------------------------

[](#implementing-a-custom-encoding)

Encoders and decoders are built upon [Confetti](https://github.com/eloquent/confetti) transforms. For details on how to implement a transform, see the Confetti documentation for [implementing a transform](https://github.com/eloquent/confetti#implementing-a-transform).

As an example, given the following transform:

```
use Eloquent\Confetti\TransformInterface;

class Rot13Transform implements TransformInterface
{
    public function transform($data, &$context, $isEnd = false)
    {
        return array(str_rot13($data), strlen($data));
    }
}
```

It is extremely simple to create a related encoder, decoder, or codec:

```
use Eloquent\Endec\Codec;
use Eloquent\Endec\Decoder;
use Eloquent\Endec\Encoder;

$transform = new Rot13Transform;

$encoder = new Encoder($transform);
echo $encoder->encode('foobar'); // outputs 'sbbone'

$decoder = new Decoder($transform);
echo $decoder->decode('foobar'); // outputs 'sbbone'

$codec = new Codec($transform, $transform);
echo $codec->decode($codec->encode('foobar')); // outputs 'foobar'
```

Note that the codec takes the same tranform for encoding and decoding only because rot13 is reciprocal. Most codecs would require a separate encode and decode transform.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity24

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.7% 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 ~20 days

Total

4

Last Release

4377d ago

PHP version history (2 changes)0.1.0PHP &gt;=5.4

0.1.1PHP &gt;=5.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/100152?v=4)[Erin](/maintainers/ezzatron)[@ezzatron](https://github.com/ezzatron)

---

Top Contributors

[![ezzatron](https://avatars.githubusercontent.com/u/100152?v=4)](https://github.com/ezzatron "ezzatron (75 commits)")[![jmalloc](https://avatars.githubusercontent.com/u/761536?v=4)](https://github.com/jmalloc "jmalloc (1 commits)")

---

Tags

streamencodingbase64streamingencodedecodebase32hexbase16decodingreactcodecbase64urlbase32hex

### Embed Badge

![Health badge](/badges/eloquent-endec/health.svg)

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

###  Alternatives

[paragonie/constant_time_encoding

Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)

901329.7M148](/packages/paragonie-constant-time-encoding)[christian-riesen/base32

Base32 encoder/decoder according to RFC 4648

13331.8M61](/packages/christian-riesen-base32)[delight-im/base64

Simple and convenient Base64 encoding and decoding for PHP

15158.1k6](/packages/delight-im-base64)[xobotyi/basen

Text and integers encoding utilities for PHP with no extensions dependencies. Base32, Base58, Base64 and much more!

1219.6k](/packages/xobotyi-basen)[skleeschulte/base32

Base32 encoding and decoding class (RFC 4648, RFC 4648 extended hex, Crockford, z-base-32/Zooko).

17314.5k9](/packages/skleeschulte-base32)[dflydev/base32-crockford

Encode/decode numbers using Douglas Crockford's Base32 Encoding

14379.1k1](/packages/dflydev-base32-crockford)

PHPackages © 2026

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