Completed
Push — master ( e22522...ee64fa )
by Sergei
13:37
created

UserDataMapper::mapData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Modera\BackendSecurityBundle\DataMapper;
4
5
use Modera\ServerCrudBundle\DataMapping\DefaultDataMapper;
6
7
/**
8
 * This class add support of excluded Fields, that will not me automatically
9
 * mapped.
10
 * Also User meta field mapping requires some by hand handling.
11
 *
12
 * @author    Alex Plaksin <[email protected]>
13
 * @copyright 2016 Modera Foundation
14
 */
15
class UserDataMapper extends DefaultDataMapper
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $excludedFields = array('meta');
21
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function getAllowedFields($entityClass)
26
    {
27
        $me = $this;
28
29
        return array_filter(
30
            parent::getAllowedFields($entityClass),
31
            function ($fieldName) use ($me) {
32
                if (array_search($fieldName, $me->excludedFields) !== false) {
33
                    return false;
34
                }
35
36
                return true;
37
            }
38
        );
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function mapData(array $params, $entity)
45
    {
46
        parent::mapData($params, $entity);
47
48
        if (array_key_exists('meta', $params)) {
49
            if (is_array($params['meta'])) {
50
                $entity->setMeta($params['meta']);
51
            } else {
52
                $entity->clearMeta();
53
            }
54
        }
55
    }
56
}
57