<commit_before># Voilab Mapping system

## Install

Via Composer

Create a composer.json file in your project root:
``` json
{
    "require": {
        "voilab/mapping": "1.*"
    }
}
```

``` bash
$ composer require voilab/mapping
```

## Mapping structure

``` php
$mapping = [
    // int key = (no key) same value for mapping and data array
    'id',
    // string key = set a different key for mapping
    'name' => 'familyname',
    // function = return the custom value. $data is either an object or
    // an array, depending on the resultset hydratation
    'address' => function (MyObject $data) {
        return $data->getNpa() . ' ' . $data->getCity();
    },
    // value with dots = fetch directely a child relation field. Last
    // element is field. Each other is a relation. If relation is a
    // collection, fetch the first one
    'user_first_group_id' => 'groups[].id',
    'coord_type_name' => 'coordinate.type.text1',
    // array = simple relation
    'politeness' => [
        'short' => 'text2',
        'long' => 'text1'
    ],
    // array = collection relations (double array)
    'groups' => [[
        'id',
        'name'
    ]],
    // change relation's data accessor
    'relation_a' => [
        \voilab\mapping\Mapping::RELATION_KEY => 'relationKeyA',
        'id'
    ],
    // wildcard to fetch all fields with relations. If it's an object,
    // it must implement an array iterator
    '*'
];
```
## Usage

``` php
// have a set of data structure, like $user['groups'][0]['name']
$data = $user;
$map = new \voilab\mapping\Mapping();
$result = $map->map($user, [
    'id',
    'name' => 'login',
    'groups' => [[
        'id',
        'name'
    ]]
]);
print_r($result);
```

## Testing

``` bash
$ phpunit
```

## Security

If you discover any security related issues, please email <EMAIL> instead of using the issue tracker.

## Credits

- [tafel](https://github.com/tafel)
- [voilab](https://github.com/voilab)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.<commit_msg>documentation<commit_after># Voilab Mapping system

## Install

Via Composer

Create a composer.json file in your project root:
``` json
{
    "require": {
        "voilab/mapping": "1.*"
    }
}
```

``` bash
$ composer require voilab/mapping
```

## Mapping structure

``` php
$mapping = [
    // int key = (no key) same value for mapping and data array
    'id',
    // string key = set a different key for mapping
    'name' => 'familyname',
    // function = return the custom value. $data is either an object or
    // an array, depending on the resultset hydratation
    'address' => function (MyObject $data) {
        return $data->getNpa() . ' ' . $data->getCity();
    },
    // value with dots = fetch directely a child relation field. Last
    // element is field. Each other is a relation. If relation is a
    // collection, fetch the first one
    'user_first_group_id' => 'groups[].id',
    'coord_type_name' => 'coordinate.type.text1',
    // array = simple relation
    'politeness' => [
        'short' => 'text2',
        'long' => 'text1'
    ],
    // array = collection relations (double array)
    'groups' => [[
        'id',
        'name'
    ]],
    // change relation's data accessor
    'relation_a' => [
        \voilab\mapping\Mapping::RELATION_KEY => 'relationKeyA',
        'id'
    ],
    // wildcard to fetch all fields with relations. If it's an object,
    // it must implement an array iterator
    '*'
];
```

## Sample dataset

``` php
// have a mapping instance
$mapping = new \voilab\mapping\Mapping();

// have a set of datas. This could also be an object with
// methods like getName(), getInterests(), etc.
$data = [
    'id' => 1,
    'name' => 'John',
    'email' => '<EMAIL>',
    'interests' => [
        [
            'type' => 'intellect',
            'description' => 'Some book',
            'contact' => [
                'name' => 'Some author'
            ]
        ],
        [
            'type' => 'sport',
            'description' => 'Football'
        ]
    ],
    'bestFriend' => [
        'id' => 2,
        'name' => 'Fred'
        'age' => 30
    ],
    'work' => [
        'description' => 'Free worker',
        'section' => [
            'type' => 'Social'
        ]
    ]
];
```

## Examples
### Simple mapping
```php
$mapped = $mapping->map($data, [
    'id',
    'name'
]);

// results in
[
    'id' => 1,
    'name' => 'John'
]
```

### Function call mapping
```php
$mapped = $mapping->map($data, [
    'id',
    'email' => function ($data) {
        return $data['email'] == '<EMAIL>' ? '<EMAIL>' : $data['email'];
    }
]);

// results in
[
    'id' => 1,
    'email' => '<EMAIL>'
]
```


### Change mapping key
```php
$mapped = $mapping->map($data, [
    'id',
    'personName' => 'name'
]);

// results in
[
    'id' => 1,
    'personName' => 'John'
]
```

### One-to-one or many-to-one relation mapping
```php
$mapped = $mapping->map($data, [
    'work' => [
        'section' => [
            'type'
        ]
    ]
]);

// results in
[
    'work' => [
        'section' => [
            'type' => 'Social'
        ]
    ]
]
```

### Change relation mapping key
```php
$mapped = $mapping->map($data, [
    'nextWork' => [
        \voilab\mapping\Mapping::RELATION_KEY => 'work',
        'description'
    ]
]);

// results in
[
    'nextWork' => [
        'description' => 'Free worker'
    ]
]
```

### One-to-many or many-to-many relation mapping
```php
$mapped = $mapping->map($data, [
    'interests' => [[
        'description'
    ]]
]);

// results in
[
    'interests' => [
        ['description' => 'Some book'],
        ['description' => 'Football']
    ]
]
```

### Complex relation mapping
```php
$mapped = $mapping->map($data, [
    'interests' => [[
        'contact' => [
            'name'
        ]
    ]]
]);

// results in
[
    'interests' => [
        ['contact' => [
            'name' => '<NAME>'
        ]],
        ['contact' => null]
    ]
]
```

### Wildcard mapping
```php
$mapped = $mapping->map($data, [
    'name',
    'bestFriend' => [
        '*'
    ]
]);

// results in
[
    'name' => 'John',
    'bestFriend' => [
        'id' => 2,
        'name' => 'Fred'
        'age' => 30
    ]
]
```

## Plugins
### One-to-one or many-to-one relation mapping
```php
$mapped = $mapping->map($data, [
    'sectionType' => 'work.section.type',
    'interests' => [[
        'contactName' => 'contact.name'
    ]]
]);

// results in
[
    'sectionType' => 'Social',
    'interests' => [
        ['contactName' => 'Some author']
        ['contactName' => null]
    ]
]
```

### One-to-many or many-to-many relation mapping
```php
$mapped = $mapping->map($data, [
    'firstInterestContactName' => 'interests[].contact.name'
]);

// results in
[
    'firstInterestContactName' => 'Some author'
]
```

## Testing

``` bash
$ phpunit
```

## Security

If you discover any security related issues, please email <EMAIL> instead of using the issue tracker.

## Credits

- [tafel](https://github.com/tafel)
- [voilab](https://github.com/voilab)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
{'fraction_non_alphanumeric': 0.1853035143769968, 'fraction_numerical': 0.0026392554521461314, 'mean_word_length': 2.2638259292837715, 'pattern_counts': {'":': 4, '<': 10, '<?xml version=': 0, '>': 107, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1001030', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2365, 'n_words': 623}