Download the PHP package eloquent/endec without Composer
On this page you can find all versions of the php package eloquent/endec. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package endec
Endec
Versatile encoding implementations for PHP.
Installation and documentation
- Available as Composer package eloquent/endec.
- API documentation available.
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
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).
Stream filters
PHP natively supports stream filters. Any number of filters can be added to any stream with stream_filter_append or stream_filter_prepend, and removed with stream_filter_remove. All of Endec's encodings are available as stream filters.
React streams
Streams can be obtained from an encoder, decoder, or codec. Endec's streams implement both WritableStreamInterface and ReadableStreamInterface from the React library, and hence can be used in an asynchronous manner.
Handling errors
Handling errors when dealing with strings is as simple as catching an exception. All exceptions thrown implement TransformExceptionInterface:
When using stream filters, error handling is difficult because PHP seems to
simply ignore errors produced by the filter. If 0 bytes are written by
fwrite()
, it's a fair indication that an error occurred:
When using React streams, simply handle the error
event:
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 from RFC 4648
- Base64 for MIME message bodies from RFC 2045
- Base64 with URL and filename safe alphabet from RFC 4648
- Base32 from RFC 4648
- Base32 with extended hexadecimal alphabet from RFC 4648
- Base16 (hexadecimal) from RFC 4648
- URI percent encoding from RFC 3986
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)
- endec.base64mime-decode (see also PHP's convert.base64-decode)
- 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
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, all decoders implement DecoderInterface, and all codecs implement EncoderInterface, DecoderInterface, and CodecInterface. This allows for type hints to accurately express requirements.
Implementing a custom encoding
Encoders and decoders are built upon Confetti transforms. For details on how to implement a transform, see the Confetti documentation for [implementing a transform].
As an example, given the following transform:
It is extremely simple to create a related encoder, decoder, or codec:
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.
All versions of endec with dependencies
eloquent/confetti Version ~0.3.1
evenement/evenement Version >=1,<3
icecave/isolator Version ~2
react/stream Version >=0.3,<0.5