Completed
Push — 1.0 ( df18cb...d1b573 )
by Valentin
01:34
created

FieldDefinitionMapper::callStruct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
namespace Transfer\EzPlatform\Repository\Values\Mapper;
10
11
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionCreateStruct;
12
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinitionUpdateStruct;
13
use Transfer\EzPlatform\Repository\Values\FieldDefinitionObject;
14
15
/**
16
 * Field definition mapper.
17
 *
18
 * @internal
19
 *
20
 * @author Harald Tollefsen <[email protected]>
21
 */
22
class FieldDefinitionMapper
23
{
24
    /**
25
     * @var FieldDefinitionObject
26
     */
27
    public $fieldDefinitionObject;
28
29
    /**
30
     * @param FieldDefinitionObject $fieldDefinitionObject
31
     */
32 37
    public function __construct(FieldDefinitionObject $fieldDefinitionObject)
33
    {
34 37
        $this->fieldDefinitionObject = $fieldDefinitionObject;
35 37
    }
36
37
    /**
38
     * @param FieldDefinitionCreateStruct $createStruct
39
     */
40 5
    public function mapObjectToCreateStruct(FieldDefinitionCreateStruct $createStruct)
41
    {
42
        // Name collection (ez => transfer)
43
        $keys = array(
44 5
            'names' => 'names',
45 5
            'descriptions' => 'descriptions',
46 5
            'fieldGroup' => 'field_group',
47 5
            'position' => 'position',
48 5
            'isTranslatable' => 'is_translatable',
49 5
            'isRequired' => 'is_required',
50 5
            'isInfoCollector' => 'is_info_collector',
51 5
            'isSearchable' => 'is_searchable',
52 5
            'fieldSettings' => 'field_settings',
53 5
            'defaultValue' => 'default_value',
54 5
            'identifier' => 'identifier',
55 5
            'validatorConfiguration' => 'validator_configuration',
56 5
            'fieldTypeIdentifier' => 'type',
57 5
        );
58
59 5
        $this->arrayToStruct($createStruct, $keys);
60
61 5
        $this->callStruct($createStruct);
62 5
    }
63
64
    /**
65
     * @param FieldDefinitionUpdateStruct $updateStruct
66
     */
67 33
    public function mapObjectToUpdateStruct(FieldDefinitionUpdateStruct $updateStruct)
68
    {
69
        // Name collection (ez => transfer)
70
        $keys = array(
71 33
            'names' => 'names',
72 33
            'descriptions' => 'descriptions',
73 33
            'fieldGroup' => 'field_group',
74 33
            'position' => 'position',
75 33
            'isTranslatable' => 'is_translatable',
76 33
            'isRequired' => 'is_required',
77 33
            'isInfoCollector' => 'is_info_collector',
78 33
            'isSearchable' => 'is_searchable',
79 33
            'fieldSettings' => 'field_settings',
80 33
            'defaultValue' => 'default_value',
81 33
            'identifier' => 'identifier',
82 33
            'validatorConfiguration' => 'validator_configuration',
83 33
        );
84
85 33
        $this->arrayToStruct($updateStruct, $keys);
86
87 33
        $this->callStruct($updateStruct);
88 33
    }
89
90
    /**
91
     * @param FieldDefinitionCreateStruct|FieldDefinitionUpdateStruct $struct
92
     * @param array                                                   $keys
93
     */
94 37
    private function arrayToStruct($struct, $keys)
95
    {
96 37
        foreach ($keys as $ezKey => $transferKey) {
97 37
            if (isset($this->fieldDefinitionObject->data[$transferKey])) {
98 37
                $struct->$ezKey = $this->fieldDefinitionObject->data[$transferKey];
99 37
            }
100 37
        }
101 37
    }
102
103
    /**
104
     * @param FieldDefinitionCreateStruct|FieldDefinitionUpdateStruct $struct
105
     */
106 37
    private function callStruct($struct)
107
    {
108 37
        if ($this->fieldDefinitionObject->getProperty('struct_callback')) {
109 1
            $callback = $this->fieldDefinitionObject->getProperty('struct_callback');
110 1
            $callback($struct);
111 1
        }
112 37
    }
113
}
114