Download the PHP package treptow-labs/envelope without Composer

On this page you can find all versions of the php package treptow-labs/envelope. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package envelope

Envelope

Envelope is a small PHP library for building DTOs that know the difference between "this field was not provided" and "this field was explicitly set to null."

It's built around a Option / Some / None type (similar to Rust's Option<T> or Java's Optional<T>), and an Envelope base class that turns "not provided" properties into fields that are automatically omitted from your DTO's array/JSON output.

The problem

null is overloaded. Say you have a PATCH /users/{id} endpoint backed by a DTO like this:

If a client sends {"name": "Jane"}, how do you know they didn't mean to touch bio — versus a client sending {"name": "Jane", "bio": null}, who is explicitly asking you to clear bio?

Once a field is nullable, you can no longer use null as a sentinel for "not provided," because null is also a legitimate value. The usual workarounds — checking array_key_exists everywhere, sprinkling isset() checks through your update logic, or maintaining a parallel list of "dirty" fields — get messy fast, especially once a DTO has more than a couple of optional fields.

Envelope solves this by giving every property two possible states instead of two:

Your DTO stays a normal, typed PHP class, but toArray() / toJson() will only ever include the fields that were actually provided — making it safe to feed straight into Model::update(), Model::fill(), or any other partial-update code path.

Installation

Option, Some, and None

Option is an abstract class with two concrete implementations:

You can also build an Option from a raw value, optionally providing a sentinel that counts as "none":

Building a DTO with Envelope

Extend Envelope and type each property as an Option. Calling toArray() (or toJson()) will unwrap every Some and skip every None automatically:

Notice that isActive is present with a value of null (the "explicitly cleared" case) while bio doesn't appear at all, because it was never provided.

Populating a DTO from request data: EnvelopeResolver

EnvelopeResolver wraps a raw array (e.g. $request->validated()) and gives you typed accessors that return Some if the key was present in the array, None if it wasn't:

Available accessors, all following the same pattern:

Method Returns
get(string $key, ?Closure $callback = null) Raw value, optionally transformed by $callback
string(string $key, bool $nullable = true, bool $throw = false) Cast to string
int(string $key, ...) / integer(...) Cast to int
float(string $key, ...) Cast to float
boolean(string $key, ...) Cast via FILTER_VALIDATE_BOOLEAN
array(string $key, ...) Cast to array
enum(string $key, string $enumClass, bool $nullable = true) An instance of $enumClass, via $enumClass::tryFrom()

Each of these:

Every accessor has an ...OrThrow sibling (stringOrThrow, intOrThrow/integerOrThrow, floatOrThrow, booleanOrThrow, arrayOrThrow) that's shorthand for calling it with nullable: false, throw: true; useful for fields that were provided but must not resolve to null:

Shaping the output: attributes

Three attributes let you control how a property is serialized without touching your update logic.

#[MapsTo] - rename the output key

#[Omit] - always exclude a property from toArray output

Useful for properties you need on the DTO for internal logic but never want serialized, regardless of whether they're Some or None:

#[FormatUsing] - transform the value before it's output

Pass a Formatter class (and any constructor arguments it needs):

Envelope ships with two formatters out of the box:

You can write your own by implementing the Formatter interface:

Putting it all together: a PATCH endpoint


All versions of envelope with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3.0
illuminate/contracts Version ^10.0 || ^11.0 || ^12.0 || ^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package treptow-labs/envelope contains the following files

Loading the files please wait ...