A simple PHP library to transfer data from a source (object or array) to an object.

Overview

SimplexMapper

A simple PHP library to transfer data from a source (object or array) to an object.

$dbData = [
    'username' => 'pfazzi',
    'emailAddress' => '[email protected]',
    'isEnabled' => '1',
];

$mapper = new \Pfazzi\SimplexMapper\Mapper();

$user = $mapper->map(
    source: $dbData, 
    target: User::class, 
);

Are you looking for an easy way to convert an array to an object?
Do you want to map a DTO to a domain object?
Do you want to hydrate an object from an array?
Use SimplexMapper!

Features

  • Easy to use
  • Zero configuration
  • Zero dependencies
  • Supports PHP 8 union types
  • Supports nested objects
  • Uses reflection, no need to modify your objects

Installation

Install SimplexMapper with composer

composer require pfazzi/simplex-mapper

Usage

Map

Maps the source data to a new instance of the target class

$mapper = new \Pfazzi\SimplexMapper\Mapper();

// From Array 

$userEntity = $mapper->map(source: ['name' => 'patrick'], target: UserEntity::class);

// From stdClass 

$rawData = new \stdClass();
$rawData->name = 'Patrick';

$userEntity = $mapper->map(source: $rawData, target: UserEntity::class);

// From anonymous class

$rawData = new class {
    public function __construct(private string $name = 'Patrick') {}
};

$userEntity = $mapper->map(source: $rawData, target: UserEntity::class);

// From object

$rawData = new UserDto('Patrick');

$userEntity = $mapper->map(source: $rawData, target: UserEntity::class);

Hydrate

Hydrates an object instance with data from the source:

$mapper = new \Pfazzi\SimplexMapper\Mapper();

$userEntity = $entityManager->find(UserEntity::class, 1);

// From Array 

$mapper->hydrate(source: ['name' => 'patrick'], target: $userEntity);

// From stdClass 

$rawData = new \stdClass();
$rawData->name = 'Patrick';

$mapper->map(source: $rawData, target: $userEntity);

// From anonymous class

$rawData = new class {
    public function __construct(private string $name = 'Patrick') {}
};

$mapper->map(source: $rawData, target: $userEntity);

// From object

$rawData = new UserDto('Patrick');

$mapper->map(source: $rawData, target: $userEntity);

Use Cases

Map database data to read models

You want to read data from supplier table using Doctrine DBAL Connection and instantiate a Supplier read model:

class Supplier
{
    public function __construct(
        public int $id,
        public string $taxId,
        public bool $onConsignment,
    ) {
    }
}

$result = $connection->executeQuery(<<<'SQL'
    SELECT
        supplier.id,
        supplier.tax_id AS taxId,
        supplier.is_enabled AS isEnabled
    FROM supplier
    WHERE supplier.tax_id = :taxId
SQL, ['taxId' => $taxId]);

$data = $result->fetchAssociative();

Without SimplexMapper you have to write mapping code, such as:

$readModel = new Supplier(
    (int) $data['id'],
    $data['taxId'],
    '1' == $data['isEnabled'],
);

With SimplexMapper all you need to write is just the following line:

$readModel = $this->mapper->map($data, Supplier::class);
You might also like...
A data mapper implementation for your persistence model in PHP.

Atlas.Orm Atlas is a data mapper implementation for persistence models (not domain models). As such, Atlas uses the term "record" to indicate that its

Analogue ORM : Data Mapper ORM for Laravel/PHP

(this project is looking for a new maintainer) Analogue ORM Analogue is a flexible, easy-to-use ORM for PHP. It is a transposition of the Eloquent ORM

phpMyFAQ - Open Source FAQ web application for PHP and MySQL, PostgreSQL and other databases

phpMyFAQ 3.1 What is phpMyFAQ? phpMyFAQ is a multilingual, completely database-driven FAQ-system. It supports various databases to store all data, PHP

A simple library to access and manipulate database records. Built on top of Dibi and hardwired for PostgreSQL.

grifart/tables A simple library to access and manipulate database records. Built on top of Dibi and hardwired for PostgreSQL. This library is develope

MySQL Spatial Data Extension integration with Laravel.

Laravel MySQL Spatial extension Laravel package to easily work with MySQL Spatial Data Types and MySQL Spatial Functions. Please check the documentati

MySQL Load Data Infile Support For Laravel

Laravel Load File 💽 A package to help with loading files into MySQL tables. This uses MySQL's LOAD DATA statement to load text files quickly into you

Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.
Eloquent Filter is a package for filter data of models by the query strings. Easy to use and fully dynamic.

Eloquent Filter Eloquent Filter adds custom filters to your Eloquent Models in Laravel. It's easy to use and fully dynamic. Table of Content Introduct

Database adapter for data interfaces

Yii _____ The package ... Requirements PHP 7.4 or higher. Installation The package could be installed with composer: composer require yiisoft/_____ --

Small script for importing the KvK (Dutch Chamber of Commerce) Open Data Set (CSV file) to a MySQL database.
Small script for importing the KvK (Dutch Chamber of Commerce) Open Data Set (CSV file) to a MySQL database.

KvK-CSV-2-SQL Small script for importing the KvK (Dutch Chamber of Commerce) Open Data Set (CSV file) to a MySQL database. Table of content KvK-CSV-2-

Owner
Patrick Luca Fazzi
PHP & Symfony Developer
Patrick Luca Fazzi
A simple program to query mysql data and display the queried data in JSON format

A simple program to query mysql data and display the queried data in JSON format. The data displayed in JSON format will change and update as the data in your mysql database changes.

null 2 Mar 7, 2022
Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP

Propel2 Propel2 is an open-source Object-Relational Mapping (ORM) for PHP. Requirements Propel uses the following Symfony Components: Config Console F

Propel 1.2k Dec 27, 2022
ATK Data - Data Access Framework for high-latency databases (Cloud SQL/NoSQL).

ATK Data - Data Model Abstraction for Agile Toolkit Agile Toolkit is a Low Code framework written in PHP. Agile UI implement server side rendering eng

Agile Toolkit 257 Dec 29, 2022
PHP Object Model Manager for Postgresql

POMM: The PHP Object Model Manager for Postgresql Note This is the 1,x version of Pomm. This package is not maintained anymore, the stable Pomm 2.0 is

Grégoire HUBERT 161 Oct 17, 2022
[READ-ONLY] A flexible, lightweight and powerful Object-Relational Mapper for PHP, implemented using the DataMapper pattern. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp

CakePHP ORM The CakePHP ORM provides a powerful and flexible way to work with relational databases. Using a datamapper pattern the ORM allows you to m

CakePHP 146 Sep 28, 2022
Doctrine Object Relational Mapper (ORM)

3.0.x 2.9.x 2.8.x Doctrine 2 is an object-relational mapper (ORM) for PHP 7.1+ that provides transparent persistence for PHP objects. It sits on top o

Doctrine 9.5k Jan 2, 2023
A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.

Idiorm http://j4mie.github.com/idiormandparis/ Feature/API complete Idiorm is now considered to be feature complete as of version 1.5.0. Whilst it wil

Jamie Matthews 2k Dec 27, 2022
Convention-based Object-Relational Mapper

Corma Corma is a high-performance, convention-based ORM based on Doctrine DBAL. Corma is great because: No complex and difficult to verify annotations

Michael O'Connell 30 Dec 20, 2022
Simple MySQL library for PHP 5.4+ includes Query Builder, PDO Native functions, Helper functions for quick use.

Simple MySQL library for PHP 5.4+ includes Query Builder, PDO Native functions, Helper functions for quick use.

Kodols 9 Dec 22, 2022
A simple library for managing database connections, results pagination and building queries in PHP

PHP lions-software-database-manager This is a simple library for managing database connections, results pagination and building queries in PHP. Esta é

Lions Software 0 Feb 7, 2022