PHPackages                             alevikzs/phmap - 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. alevikzs/phmap

ActiveLibrary

alevikzs/phmap
==============

Library provide mapping some php structures (json string, array, object) to specific object. This library based on Phalcon annotations.

v2.3.1(9y ago)014MITPHPPHP &gt;=5.4.0

Since Jan 29Pushed 9y agoCompare

[ Source](https://github.com/alevikzs/phmap)[ Packagist](https://packagist.org/packages/alevikzs/phmap)[ RSS](/packages/alevikzs-phmap/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (2)Versions (7)Used By (0)

PhMap
=====

[](#phmap)

[![License](https://camo.githubusercontent.com/baa121d4c0d200a42dee409e84658d974d3a8429c80865b4486155f1c1a31d98/687474703a2f2f706f7365722e707567782e6f72672f616c6576696b7a732f70686d61702f6c6963656e7365)](https://packagist.org/packages/alevikzs/phmap)[![Latest Stable Version](https://camo.githubusercontent.com/5766db0f4ebd8843114e8406c8fb04e812d0b7b258890b876eeef1174558f9fc/687474703a2f2f706f7365722e707567782e6f72672f616c6576696b7a732f70686d61702f762f737461626c65)](https://packagist.org/packages/alevikzs/phmap)[![Total Downloads](https://camo.githubusercontent.com/697b7668aefdfabe4f56117be9fbbb95eadd499b4c1cfe41f3097170e5239fb4/687474703a2f2f706f7365722e707567782e6f72672f616c6576696b7a732f70686d61702f646f776e6c6f616473)](https://packagist.org/packages/alevikzs/phmap)[![Dependency Status](https://camo.githubusercontent.com/e8309a5a837dde0eddb2e5549877e5b10062a52638eefd03453a8eb3d3687769/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f757365722f70726f6a656374732f3536646130313762643561353335303032396165356364382f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/user/projects/56da017bd5a5350029ae5cd8)[![Reference Status](https://camo.githubusercontent.com/6fdc4410954c6dd7f77e35e267679add851bbedae9ae942d40b4148a5572fb54/68747470733a2f2f7777772e76657273696f6e6579652e636f6d2f7068702f616c6576696b7a733a70686d61702f7265666572656e63655f62616467652e7376673f7374796c653d666c6174)](https://www.versioneye.com/php/alevikzs:phmap/references)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/7355fb2a72b48802ff0fc091e7728310042cee4af4ced101d807043bd59d016a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c6576696b7a732f70686d61702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alevikzs/phmap/?branch=master)[![Code Climate](https://camo.githubusercontent.com/6cc11daa768145b374bbf9eed5100bf87f0ca0c57852f5c978afae05841a3290/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f616c6576696b7a732f70686d61702f6261646765732f6770612e737667)](https://codeclimate.com/github/alevikzs/phmap)[![Build Status](https://camo.githubusercontent.com/949ab821e2a0d346403bab7c2199c2047b4f8b6e3dfc15ee6d74905346902740/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c6576696b7a732f70686d61702f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alevikzs/phmap/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/53c81006b16ff50e5e3b0c27c30fa7db1fb5eb228731098022850caa069946b7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c6576696b7a732f70686d61702f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alevikzs/phmap/?branch=master)

About
-----

[](#about)

The PhMap is a PHP package for create objects from JSON strings, associative arrays and objects. The PhMap is based on [phalcon annotations](https://docs.phalconphp.com/en/latest/reference/annotations.html).

Requirements
------------

[](#requirements)

- PHP &gt;= 5.4 &amp;&amp; &lt; 7.0;
- [Phalcon framework](https://phalconphp.com) &gt;= 2.0;
- If you will be using [APC](http://php.net/manual/en/book.apc.php) or [XCache](https://xcache.lighttpd.net/) adapters you need to install corresponding PHP extensions.

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

[](#installation)

1. Require the package and its dependencies with composer: `$ composer require alevikzs/phmap`
2. Install [Phalcon framework](https://phalconphp.com). Detail guide is [here](https://phalconphp.com/en/download).

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

[](#how-to-use)

```
class Tree {

    private $height;
    private $name;
    private $branch;

    public function getHeight() {
        return $this->height;
    }

    public function setHeight($height) {
        $this->height = $height;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getBranch() {
        return $this->branch;
    }

    /**
     * @mapper(class="\Tests\Dummy\Branch")
     */
    public function setBranch(Branch $branch) {
        $this->branch = $branch;
    }

}
```

As you can see, if some property of you object has type of another class - you must declare an annotation `@mapper`for setter method of this property. This annotation has two arguments: `class` and `isArray`. The first argument is a string with the class name and second is a boolean value that indicates your property value is array or not.

```
class Branch {

    private $length;
    private $leaves;

    public function getLength() {
        return $this->length;
    }

    public function setLength($length) {
        $this->length = $length;
    }

    public function getLeaves() {
        return $this->leaves;
    }

    /**
     * @mapper(class="\Tests\Dummy\Leaf", isArray=true)
     */
    public function setLeaves(array $leaves) {
        $this->leaves = $leaves;
    }

}

class Leaf {

    private $height;
    private $width;

    public function getHeight() {
        return $this->height;
    }

    public function setHeight($height) {
        $this->height = $height;
    }

    public function getWidth() {
        return $this->width;
    }

    public function setWidth($width) {
        $this->width = $width;
    }

}
```

Create object of Tree class from JSON string:

```
$result = (new \PhMap\Wrapper\Json($json, 'Tree'))->map();
```

Create object of Tree class from associative array:

```
$result = (new \PhMap\Mapper\Structure\Associative($array, 'Tree'))->map();
```

Create object of Tree class from another object:

```
$result = (new \PhMap\Mapper\Structure\Object($object, 'Tree'))->map();
```

You can use `\PhMap\Mapper\Smart` if you don't know what type of you value. In this case mapping instructions will be applied automatically:

```
$result = (new \PhMap\Wrapper\Smart($value, 'Tree'))->map();
```

By default mapper use [memory adapter](https://docs.phalconphp.com/en/latest/api/Phalcon_Annotations_Adapter_Memory.html), but also you can use [file adapter](https://docs.phalconphp.com/en/latest/api/Phalcon_Annotations_Adapter_Files.html), [APC adapter](https://docs.phalconphp.com/en/latest/api/Phalcon_Annotations_Adapter_Apc.html) and [XCache adapter](https://docs.phalconphp.com/en/latest/api/Phalcon_Annotations_Adapter_Xcache.html):

```
new \PhMap\Wrapper\Json($json, 'Tree', \PhMap\Mapper::MEMORY_ANNOTATION_ADAPTER);

new \PhMap\Wrapper\Smart($json, 'Tree', \PhMap\Mapper::FILES_ANNOTATION_ADAPTER);

new \PhMap\Mapper\Structure\Associative($array, 'Tree', \PhMap\Mapper::APC_ANNOTATION_ADAPTER);

new \PhMap\Mapper\Structure\Object($object, 'Tree', \PhMap\Mapper::X_CACHE_ANNOTATION_ADAPTER);
```

Also, you can pass already exist object to constructor:

```
$tree = new Tree();

$result = (new \PhMap\Mapper\Smart($json, $tree))->map();
```

You can reuse mapper object. Just set the necessary properties, and call the method `map()`:

```
$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree', \PhMap\Mapper::X_CACHE_ANNOTATION_ADAPTER)
$result = $mapper->map();

$mapper->setInputObject($branch)
    ->setOutputObject(new Branch())
    ->setAnnotationAdapterType(Mapper::MEMORY_ANNOTATION_ADAPTER);
$result = $mapper->map();
```

Mapper object has `setTransforms(Transforms $transforms)` method. The argument of this method is a transforms object. This object is used to declare a set of rules where each rule indicates what field of input value corresponds to the output field value:

```
$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree')

$transforms = (new \PhMap\Transforms())
    ->add(
        (new \PhMap\Transform())
            ->setInputFieldName('nameIn')
            ->setOutputFieldName('name')
    )
    ->add(
        (new \PhMap\Transform())
            ->setInputFieldName('branchIn')
            ->setOutputFieldName('branch')
            ->setTransforms(
                (new \PhMap\Transforms())->add(
                    (new \PhMap\Transform())
                        ->setInputFieldName('leavesIn')
                        ->setOutputFieldName('leaves')
                )
            )
    );

$result = $mapper->setTransforms($transforms)->map();
```

For skipping some attributes and not map them, you can use `setSkipAttributes(array $attributes)`:

```
$mapper = new \PhMap\Mapper\Structure\Object($tree, 'Tree')

$attributes = [
    'name',
    'branch.length'
];

$result = $mapper->setSkipAttributes($attributes)->map();
```

If you want to disable validation you can use `disableValidation()` method:

```
$result = $mapper->disableValidation()->map();
```

If you want your class can map some value to itself you must use MapperTrait in your class declaration:

```
class Tree {

   use \PhMap\MapperTrait;

   //other class declaration

}
```

and then you can call `mapper()` or `staticMapper()` methods:

```
$tree = new Tree();
$result = $tree->mapper($json)->map();

$result = Tree::staticMapper($json)->map();
```

The MIT License (MIT)
---------------------

[](#the-mit-license-mit)

**Copyright (c) 2016 Alexey Novikov **

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~81 days

Recently: every ~95 days

Total

6

Last Release

3353d ago

Major Versions

v1.0 → v2.02016-02-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/51e76d1247dbf9653314f84eff8808f87e9e556f0df5c9d19b31730ab417439b?d=identicon)[alevikzs](/maintainers/alevikzs)

---

Top Contributors

[![alevikzs](https://avatars.githubusercontent.com/u/2803511?v=4)](https://github.com/alevikzs "alevikzs (62 commits)")

---

Tags

apcdeserializationphalconphpxcacheannotationsphalconjson mapperarray mapper

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alevikzs-phmap/health.svg)

```
[![Health](https://phpackages.com/badges/alevikzs-phmap/health.svg)](https://phpackages.com/packages/alevikzs-phmap)
```

###  Alternatives

[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[phalcon/devtools

This tools provide you useful scripts to generate code helping to develop faster and easy applications that use with Phalcon framework.

1.3k2.0M54](/packages/phalcon-devtools)[phalcon/incubator

Adapters, prototypes or functionality that can be potentially incorporated to the C-framework.

7222.9M81](/packages/phalcon-incubator)[jms/di-extra-bundle

Allows to configure dependency injection using annotations

32813.3M88](/packages/jms-di-extra-bundle)[jms/aop-bundle

Adds AOP capabilities to Symfony2

20413.7M22](/packages/jms-aop-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
