Passed
Push — master ( e72220...d47cbb )
by Jonathan
12:45
created

BuildTrait::buildPropertyMapArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 13
cts 13
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 2
crap 4
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
trait BuildTrait
22
{
23
    /**
24
     * Build Methods
25
     * -----------------------------------------------------------------------------------------------------------------
26
     */
27
28
    /**
29
     * Using the passed propertyMap, recursively build array
30
     *
31
     * @param array       $array       Array
32
     * @param PropertyMap $propertyMap PropertyMap
33
     *
34
     * @return array
35
     */
36 14
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap)
37
    {
38 14
        $ret = [];
39
40 14
        foreach ($array as $key => $value) {
41 14
            $map = $propertyMap->getMap();
42 14
            if (isset($map[$key])) {
43 14
                $key = $map[$key];
44 7
            }
45 14
            if (is_array($value)) {
46 6
                $value = $this->buildPropertyMapArray($value, $propertyMap);
47 3
            }
48 14
            $ret[$key] = $value;
49 7
        }
50
51 14
        return $ret;
52
    }
53
54
    /**
55
     * Using passed mergeSettings array, build array for backend
56
     *
57
     * @param array $array MergeSettings array
58
     *
59
     * @return array
60
     */
61 18
    protected function buildMergeSettingsArray(array $array)
62
    {
63 18
        $ret = [];
64
65 18
        $propertyMap = new MergeSettingsPropertyMap();
66
67 18
        foreach ($propertyMap->getMap() as $property => $key) {
68 18
            if (isset($array[$key])) {
69 18
                $value = $array[$key];
70 18
                if ('culture' == $key) {
71 2
                    StaticValidator::execute($value, 'Culture');
72
                }
73 18
                if ('remove_' == substr($key, 0, 7)) {
74 12
                    StaticValidator::execute($value, 'TypeBoolean');
75 4
                }
76 18
                if ('_date' == substr($key, -5)) {
77 18
                    StaticValidator::execute($value, 'Timestamp');
78 14
                    $value = StaticFilter::execute($value, 'TimestampToDateTime');
79 7
                }
80 18
                $ret[$property] = $value;
81 9
            }
82 9
        }
83
84 8
        return $ret;
85
    }
86
87
    /**
88
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
89
     *
90
     * @param array $array FindAndReplaceData array
91
     *
92
     * @return array
93
     */
94 8
    protected function buildFindAndReplaceDataArray(array $array)
95
    {
96 8
        $ret = [];
97
98 8
        foreach ($array as $key => $value) {
99 8
            array_push($ret, [
100 8
                $key,
101 8
                $value,
102 4
            ]);
103 4
        }
104
105 8
        return $ret;
106
    }
107
}
108