wundii/data-mapper

Modern PHP 8.2+ mapping/deserialize of XML, JSON and Arrays into PHP classes

1.2.5 2025-06-08 13:09 UTC

This package is auto-updated.

Last update: 2025-06-15 16:42:24 UTC


README

PHP-Tests PHPStan VERSION PHP Rector ECS PHPUnit codecov Downloads

This is a modern php 8.2+ mapper relies on strict data types, dependency capability and convenient processing for mapping xml, objects, json and arrays into objects.

Features

  • Mapping source data into objects
  • Mapping source data with a list of elements into a list of objects
  • Initialize object via constructor, properties or methods
  • Map nested objects, arrays of objects
  • Class mapping for interfaces or other classes
  • Custom root element for starting with the source data
  • Auto-casting for float types (eu to us decimal separator)

Supported Types

  • null
  • bool|?bool
  • int|?int
  • float|?float
  • string|?string
  • array
    • int[]
    • float[]
    • string[]
    • object[]
  • object|?object
  • enum|?enum

Supported Formats

  • array
  • json
  • objects in development
    • public property
    • public getters
    • method toArray()
  • xml

Installation

Require the bundle and its dependencies with composer:

composer require wundii/data-mapper

Installations for frameworks

Usage

Minimal usage

use Wundii\DataMapper\DataMapper;

/**
 * DataConfig default settings
 * - ApproachEnum::SETTER - will use the constructor to map the data
 * - AccessibleEnum::PUBLIC - will use only public properties/methods
 * - classMap = [] - will not map any classes 
 */

$dataMapper = new DataMapper();

$testClass = $dataMapper->array($array, TestClass::class);
$testClass = $dataMapper->json($json, TestClass::class);
$testClass = $dataMapper->xml($xml, TestClass::class);

Usage with custom configuration

use Wundii\DataMapper\DataConfig;
use Wundii\DataMapper\DataMapper;
use Wundii\DataMapper\Enum\ApproachEnum;

$dataConfig = new DataConfig(
        approachEnum: ApproachEnum::PROPERTY,
        classMap: [
            DateTimeInterface::class => DateTime::class,
        ],
    );
$dataMapper = new DataMapper($dataConfig);

$testClass = $dataMapper->array($array, TestClass::class);
$testClass = $dataMapper->json($json, TestClass::class);
$testClass = $dataMapper->xml($xml, TestClass::class);