PHPackages                             giacomofurlan/object-trans-mapper-validator - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. giacomofurlan/object-trans-mapper-validator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

giacomofurlan/object-trans-mapper-validator
===========================================

Map stdClass objects into particular classes and perform validation

1.1.1(7y ago)19.8kMITPHPPHP &gt;=7.1.0

Since Dec 9Pushed 7y ago1 watchersCompare

[ Source](https://github.com/elegos/PHP-object-transmapper-validator)[ Packagist](https://packagist.org/packages/giacomofurlan/object-trans-mapper-validator)[ Docs](https://github.com/elegos)[ RSS](/packages/giacomofurlan-object-trans-mapper-validator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (2)Versions (11)Used By (0)

PHP object trans-mapper validator
=================================

[](#php-object-trans-mapper-validator)

[![codecov](https://camo.githubusercontent.com/ed9810667de22935290b98bcd59760cfaa9319e00bb8ec661c46b5e4cf0dc4e6/68747470733a2f2f636f6465636f762e696f2f67682f656c65676f732f5048502d6f626a6563742d7472616e736d61707065722d76616c696461746f722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/elegos/PHP-object-transmapper-validator)

This library aims to easily create both data-mappers and validators from the HTTP request down to a model.

Instead of manually validating every single request checking if the variable exists, if the type is correct etc, and eventually push the data into a model, this library is able to parse the request and automatically map it into a given class, as easy as the following:

```
$requestData = json_decode($payload); // whatever
$data = $this->transmapper->map($requestData, MyModel::class[, ...$override]);

```

It supports all the scalar values (`bool`, `int`, `float`, `string`), classes and arrays (both of scalar values or classes) recursively.

How to use
----------

[](#how-to-use)

First of all, create a new model class where the data will be pushed in. **Its constructor must be argument-less!**

```
class MyModel {
    // eventual argument-less constructor
    public function __constructor() {
        ...
    }

```

Then we can add some variables, using the `GiacomoFurlan\ObjectTransmapperValidator\Annotation\Validation\Validate` annotation in order to describe the validation

```
class MyModel {
    /**
    * @var bool
    * @Validate(
    *     options here
    * )
    */
    private $myBoolean;

```

The only mandatory option is `type`, which can be any of the scalar values and their aliases (`bool`, `boolean`, `int`, `integer`, `float`, `double`, `string`), an array of scalar values (es. `int[]` or `integer[]`), a class name (fully qualified name, i.e. `My\Full\Namespace\ClassName`) or an array of objects (always fully qualified name, i.e. `My\Full\Namespace\ClassName[]`).

You can optionally extend the model with `GiacomoFurlan\ObjectTransmapperValidator\Model\MappedModel`, which provides the method `isMapped(string $attribute) : bool`. Note that extending this model will forbid the use of `_mapped` as attribute name in your model (the attribute's value will be ignored).

To trans-map the standard object to the given model, call the transmapper's map method:

```
$myTransmapper->map($stdClass, MyModel::class[, $override1, $override2...])

```

Overrides allow you to dynamically change some aspects of the validation in order to make the system dynamic depending on the environment variables, in particular: `mandatory`, `nullable` and `regex`. These overrides will follow the specs of the options written below. The format is the following: `['dot.notation.variable' => ['mandatory' => true, 'nullable' => true]]`

Override examples:

- `['myString' => ['regex' => '/^must_start_with_string/']]`
- `['myInt' => ['mandatory' => false]]`
- `['mySubModel.myVar' => [...]]`

The options are:

- `type` string
- `mandatory` boolean (default true), the source object must contain the attribute
- `nullable` boolean (default false), the source may contain the attribute, even if it can be null regardless of the specified type
- `regex` string (default null), the regex to check against the value to map. Performed only if `type="string"`
- `typeExceptionClass` string, the fully-qualified class name of the exception to throw in case of type mismatch
- `typeExceptionMessage` string, the message thrown for the previous exception, must instert two `%s`: (1) found type, (2) expected type
- `typeExceptionCode` int, the type mismatch exception's code (default 3000)
- `mandatoryExceptionClass` string, the fully-qualified class name of the exception to throw in case of missing mandatory attribute
- `mandatoryExceptionMessage` string, the message thrown for the previous exception, must instert one `%s` for the missing attribute's name
- `mandatoryExceptionCode` int, the mandatory exception's code (default 3001)
- `regexExceptionClass` string, the fully-qualified class name of the exception to throw in case of regex mismatch
- `regexExceptionMessage` string, the message thrown for the previous exception, must instert two `%s`: (1) the value, (2) the regex
- `regexExceptionCode` int, the regex exception's code (default 3002)

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity64

Established project with proven stability

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

Recently: every ~142 days

Total

10

Last Release

2866d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5558fe66fdc65b41b2d2c9692ad40cccf80176db8de20d9c0926b3248e1b11fa?d=identicon)[giacomofurlan](/maintainers/giacomofurlan)

---

Tags

validationconversionmaptranscode

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/giacomofurlan-object-trans-mapper-validator/health.svg)

```
[![Health](https://phpackages.com/badges/giacomofurlan-object-trans-mapper-validator/health.svg)](https://phpackages.com/packages/giacomofurlan-object-trans-mapper-validator)
```

###  Alternatives

[composer/semver

Version comparison library that offers utilities, version constraint parsing and validation.

3.3k489.6M671](/packages/composer-semver)[giggsey/libphonenumber-for-php

A library for parsing, formatting, storing and validating international phone numbers, a PHP Port of Google's libphonenumber.

5.0k148.7M416](/packages/giggsey-libphonenumber-for-php)[respect/validation

The most awesome validation engine ever created for PHP

5.9k37.4M383](/packages/respect-validation)[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M106](/packages/propaganistas-laravel-phone)[opis/json-schema

Json Schema Validator for PHP

64236.9M185](/packages/opis-json-schema)[giggsey/libphonenumber-for-php-lite

A lite version of giggsey/libphonenumber-for-php, which is a PHP Port of Google's libphonenumber

8412.9M47](/packages/giggsey-libphonenumber-for-php-lite)

PHPackages © 2026

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