Test Failed
Push — master ( 35b555...9b1d30 )
by Jonathan
08:17
created

BuildTrait::buildPropertyMapArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.9666
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 20
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
17
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap;
18
use TxTextControl\ReportingCloud\Validator\StaticValidator;
19
use TxTextControl\ReportingCloud\Filter\StaticFilter;
20
21
/**
22
 * Trait BuildTrait
23
 *
24
 * @package TxTextControl\ReportingCloud
25
 * @author  Jonathan Maron (@JonathanMaron)
26
 */
27
trait BuildTrait
28
{
29
    /**
30
     * Build Methods
31
     * -----------------------------------------------------------------------------------------------------------------
32
     */
33
34
    /**
35
     * Using the passed propertyMap, recursively build array
36
     *
37
     * @param array       $array       Array
38
     * @param PropertyMap $propertyMap PropertyMap
39
     *
40
     * @return array
41
     */
42
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap)
43
    {
44
        $ret = [];
45
46
        foreach ($array as $key => $value) {
47
            $map = $propertyMap->getMap();
48
            if (isset($map[$key])) {
49
                $key = $map[$key];
50
            }
51
            if (is_array($value)) {
52
                $value = $this->buildPropertyMapArray($value, $propertyMap);
53
            }
54
            $ret[$key] = $value;
55
        }
56
57
        return $ret;
58
    }
59
60
    /**
61
     * Using passed mergeSettings array, build array for backend
62
     *
63
     * @param array $array MergeSettings array
64
     *
65
     * @return array
66
     */
67 14
    protected function buildMergeSettingsArray(array $array)
68
    {
69 14
        $ret = [];
70
71 14
        $propertyMap = new MergeSettingsPropertyMap();
72
73 14
        foreach ($propertyMap->getMap() as $property => $key) {
74 14
            if (isset($array[$key])) {
75 14
                $value = $array[$key];
76 14
                if ('culture' == $key) {
77 2
                    StaticValidator::execute($value, 'Culture');
78
                }
79 14
                if ('remove_' == substr($key, 0, 7)) {
80 8
                    StaticValidator::execute($value, 'TypeBoolean');
81 2
                }
82 14
                if ('_date' == substr($key, -5)) {
83 14
                    StaticValidator::execute($value, 'Timestamp');
84 10
                    $value = StaticFilter::execute($value, 'TimestampToDateTime');
85 5
                }
86 14
                $ret[$property] = $value;
87 7
            }
88 7
        }
89
90 4
        return $ret;
91
    }
92
93
    /**
94
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
95
     *
96
     * @param array $array FindAndReplaceData array
97
     *
98
     * @return array
99
     */
100 6
    protected function buildFindAndReplaceDataArray(array $array)
101
    {
102 6
        $ret = [];
103
104 6
        foreach ($array as $key => $value) {
105 6
            array_push($ret, [
106 6
                $key,
107 6
                $value,
108 3
            ]);
109 3
        }
110
111 6
        return $ret;
112
    }
113
}
114