Download the PHP package azavyalov/json-mapper without Composer

On this page you can find all versions of the php package azavyalov/json-mapper. 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 json-mapper

This package maps PHP arrays to strictly-typed objects. You can convert arrays to Data Transfer Objects (DTO), Value Objects (VO), validate API JSON responses, or basically instantiate any classes with arrays. Inspired by Go/Rust/Zig/... structs and was born out of a necesity to validate 3rd-party API JSON responses.

Installation

Usage

Example

Below is a working example demonstrating most of the features (read the comments in the code too). Please read the following sections to have a better idea of what is and what is not allowed.

The Rules

This mapper is mostly strict with a few optional exceptions (see the following sections). Implicit conversions are not allowed, e.g. passing a '0' instead of 0 to an int field would result in an exception. Each class property has to have a type specified, which could be either a builtin PHP type or another class (nested objects). Properties can be nullable. Union and mixed types are not allowed.

Each array has to have its element type specified in the DocBlock in the [] format, e.g. @param string[] $myArrayField. Array elements could also be another class objects, e.g. @param ArrayElementClass[] $myArrayField.

If each element in an array is of the same type and cannot be null - you want a typed array, e.g. string[]. If elements of an array are of different types - you want a nested object (another class with typed properties). Sometimes objects will have peculiar key names that cannot be converted into PHP variable names, in which case your only option would be to use a map, e.g. array<string, int>.

A proeprty described in the class must be present in the JSON unless the property is marked as nullable. Properties not described in the class but present in the JSON will be simply ignored.

By default, each property has to have a type.

Options

Array Type Declarations

The type of PHP arrays has to be specified in the class' constructor's DocBlock. For arrays use the [] format and for maps use the array<> format with both key and value types specified.

What's the difference between an array and a map?

In PHP, an "array" is not an actual array and is more like a representation of a loose JSON object. An actual array (as seen in stritcly typed programming languages) is a sequence of values of the same type. The keys of an array are subsequential numbers starting from 0 (0 -> 1 -> 2 -> 3 -> ...).

A map is a collection of key-value pairs. With maps your keys could be both numbers and strings, and values could be whichiver type just like in an array. This package allows the keys to be either int or string, but not both (int|string is not allowed). All the values should be of the same type.


All versions of json-mapper with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
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 azavyalov/json-mapper contains the following files

Loading the files please wait ...