PHPackages                             rogerthomas84/dtoinflator - 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. rogerthomas84/dtoinflator

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

rogerthomas84/dtoinflator
=========================

DtoInflator is a library to inflate arrays or objects into their corresponding models.

2.0.1(5y ago)068911MITPHPPHP &gt;=7.1

Since Nov 7Pushed 5y ago1 watchersCompare

[ Source](https://github.com/rogerthomas84/dtoinflator)[ Packagist](https://packagist.org/packages/rogerthomas84/dtoinflator)[ Docs](https://github.com/rogerthomas84/dtoinflator)[ RSS](/packages/rogerthomas84-dtoinflator/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (1)Versions (9)Used By (1)

DTO Inflator
============

[](#dto-inflator)

DtoInflator is a helpful library for converting arrays or generic objects into DTOs.

This was originally written for helping ease the management of responses from API services.

Usage
-----

[](#usage)

Create your model, extending `DtoInflatorAbstract`

If you require a sub model, add the property of `protected $keyToClassMap` to your parent model mapping the key name to the fully qualified class name. This tells the library to identify the key and that it needs to inflate a specific model.

**All models should extend `DtoInflatorAbstract`.**

If you have a property within a DTO called 'favourite' and it requires a sub model, you could map it with this:

```
protected $keyToClassMap = [
    'favourite' => '\MyNamespace\Favourite'
];

```

If you require an array of models (for example someone can have various favourite items), you can simply append `[]` onto the class name in the map.

So, if you have a property called 'favourites' and it's an array of child models, the key to class map should look like this:

```
protected $keyToClassMap = [
    'favourites' => '\MyNamespace\Favourite[]'
];

```

Sometimes you might want to change the name of a property to something else, for example in the case of API responses, you might want to change underscored keys with camelcase. To do this, simply expose the `fieldToFieldMap`variable in your model. Where the key is the original name, and the value is the new key to use in the DTO.

**Please note, the newly named key is only used in inflation.**

```
protected $fieldToFieldMap = [
    'my_underscore_key' => 'myUnderscoreKey'
];

```

You might also want to shorten keys every now and then. To do this, you can pass a second parameter into the inflate methods, defining where these keys need to be mapped.

```
protected $longToShortKeys = [
    'user_first_name' => 'fn', // would map the key 'user_first_name' to use 'fn'
    'user_last_name' => 'ln' // would map the key 'user_last_name' to use 'ln'
];

```

Examples
--------

[](#examples)

There are example models in the `tests/DtoInflatorTests/TestModels` directory.

Inflating
---------

[](#inflating)

You can inflate a single record by calling `inflateSingleArray` passing the array of data or alternatively, if you've got an object (for example `stdClass`) you can use `inflateSingleObject`

```
