PHPackages                             habibeh92/converter - 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. habibeh92/converter

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

habibeh92/converter
===================

mapper

v1.0.0(3y ago)16PHPPHP &gt;=8.0

Since Dec 2Pushed 3y ago1 watchersCompare

[ Source](https://github.com/habibeh92/converter)[ Packagist](https://packagist.org/packages/habibeh92/converter)[ RSS](/packages/habibeh92-converter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

Converter
=========

[](#converter)

Simple data converter to convert different data types like JSON and XML into specified entities.

Installation
------------

[](#installation)

You can install it in your application via composer using command below:

```
composer require habibeh92/converter

```

### Requirements

[](#requirements)

- `"php": ">=8.0"`

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

[](#how-to-use)

Once it installed, you can convert any type of data easily (currently JSON and XML is supported) into predefined objects. In that case, you need to call `handle` method of the `Converter`, which needs an instance of `Decoder`, to simply convert your data. The `handle` method receives an instance of entity and the data, which is needed to be converted, and returns an initialized instance.

```
$entity = new DataEntity();
$converter = new Converter(new JsonDecoder());
$entity = $converter->handle($entity, $json_data);
```

### Defining Entities

[](#defining-entities)

The entities, which are going to be filled with the data, should have some predefined properties with specified `DataType` attribute. The `DataType` needs two parameters, `field` and `type`, which `field` is required, and it shows the field name in the incoming data. The `type` parameter should be filled for the array properties, and it shows the entity of each object.

```
