PHPackages                             articus/data-transfer - 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. articus/data-transfer

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

articus/data-transfer
=====================

Library for merging source data to destination data only if destination data remains valid after that

0.6.3(1y ago)1030.4k↓44.4%1[1 PRs](https://github.com/Articus/DataTransfer/pulls)4MITPHPPHP ^7.4|^8.0

Since Aug 30Pushed 1y ago3 watchersCompare

[ Source](https://github.com/Articus/DataTransfer)[ Packagist](https://packagist.org/packages/articus/data-transfer)[ RSS](/packages/articus-data-transfer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (11)Versions (19)Used By (4)

Data Transfer
=============

[](#data-transfer)

[![GitHub Actions: Run tests](https://github.com/Articus/DataTransfer/workflows/Run%20tests/badge.svg)](https://github.com/Articus/DataTransfer/actions?query=workflow%3A%22Run+tests%22)[![Coveralls](https://camo.githubusercontent.com/a339deff0d658be812b5a6dd98a39248571b066fb0b5a4ed55917fa1496cb18f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f417274696375732f446174615472616e736665722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Articus/DataTransfer?branch=master)[![Codacy](https://camo.githubusercontent.com/d43bb51021cbf02f6f8b27779fe2427618282241787789d0e9467765860e60ce/68747470733a2f2f6170702e636f646163792e636f6d2f70726f6a6563742f62616467652f47726164652f3265633135616338633430633461373039653736363265396337313234666164)](https://app.codacy.com/gh/Articus/DataTransfer/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)

This library provides a "validating hydrator", a service that patches destination data with source data only if destination data remains valid after that. Source and destination can be anything - scalars, arrays, objects... So either you want to make a partial update of ORM entity with parsed JSON from HTTP-request or produce a plain DTO from this entity to send in AMQP-message this library can help you to do that in a neat convenient way.

How it works?
-------------

[](#how-it-works)

Let's make few definitions:

- **typed data** - some complex, application-specific, rigidly structured data like object or array of objects. For example, DTO's or ORM entities.
- **untyped data** - the opposite of **typed data** - some simple, general-purpose, amorphous data like scalar or array of scalars or stdClass instance. For example, result of `json_decode` or `yaml_parse`.
- **extract** - an algorithm to convert **typed data** to **untyped data**
- **merge** - an algorithm to patch one piece of **untyped data** with another piece of **untyped data**
- **validate** - an algorithm to check that **untyped data** is correct according some rules
- **hydrate** - an algorithm to patch **typed data** with **untyped data**

So if we have two pieces of **typed data** - *A* and *B* - this library does a rather simple thing to **transfer** *A* to *B*: it **merges** pieces of **untyped data** **extracted** from *A* and *B*, **validates** the result and **hydrates** *B* with **untyped data** **extracted** from *A* if validation is successful.

Why?
----

[](#why)

Personally I just needed something to easily update DTOs and Doctrine entities from untrusted sources (like request parsed body, request headers, request query parameters and etc). Something like [request body converter from FOSRestBundle](https://symfony.com/doc/master/bundles/FOSRestBundle/request_body_converter_listener.html) and [JMS Serializer](http://jmsyst.com/libs/serializer), but more flexible. The initial prototype was extremely useful for building APIs and after using it in several production projects I finally decided to make it a separate library. Hopefully, it will be useful for someone else.

How to install?
---------------

[](#how-to-install)

Just add `"articus/data-transfer"` to your [composer.json](https://getcomposer.org/doc/04-schema.md#require) and check [packages suggested by the library](https://getcomposer.org/doc/04-schema.md#suggest) for extra dependencies of optional components you may want to use.

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

[](#how-to-use)

Library provides a single service `Articus\DataTransfer\Service` that allows transferring data in various ways. So first of all you need to register it in your PSR-11 container. You can use any PSR-11 implementation you like, but integration with [Laminas Service Manager](https://docs.laminas.dev/laminas-servicemanager/) has slightly more features (to be precise - utilization of [plugin managers](https://docs.laminas.dev/laminas-servicemanager/plugin-managers/) and supports for [Laminas validators](https://docs.laminas.dev/laminas-validator/)). Here are two sample configurations:

- for [Laminas Service Manager](https://docs.laminas.dev/laminas-servicemanager/):

```
// Full example configuration in YAML just for readability
$configContent = setService('config', $config);

/** @var Articus\DataTransfer\Service $service */
$service = $container->get(Articus\DataTransfer\Service::class);
```

- for [Symfony Dependency Injection](https://symfony.com/doc/current/components/dependency_injection.html):

```
