Passed
Branch master (81c7db)
by Jonathan
10:52 queued 01:40
created

BuildTrait   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
wmc 12
eloc 30
dl 0
loc 85
ccs 41
cts 42
cp 0.9762
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMergeSettingsArray() 0 24 6
A buildFindAndReplaceDataArray() 0 12 2
A buildPropertyMapArray() 0 16 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
/**
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 14
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap)
43
    {
44 14
        $ret = [];
45
46 14
        foreach ($array as $key => $value) {
47 14
            $map = $propertyMap->getMap();
48 14
            if (isset($map[$key])) {
49 14
                $key = $map[$key];
50 7
            }
51 14
            if (is_array($value)) {
52 6
                $value = $this->buildPropertyMapArray($value, $propertyMap);
53 3
            }
54 14
            $ret[$key] = $value;
55 7
        }
56
57 14
        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 18
    protected function buildMergeSettingsArray(array $array)
68
    {
69 18
        $ret = [];
70
71 18
        $propertyMap = new MergeSettingsPropertyMap();
72
73 18
        foreach ($propertyMap->getMap() as $property => $key) {
74 18
            if (isset($array[$key])) {
75 18
                $value = $array[$key];
76 18
                if ('culture' == $key) {
77 2
                    StaticValidator::execute($value, 'Culture');
78
                }
79 18
                if ('remove_' == substr($key, 0, 7)) {
80 12
                    StaticValidator::execute($value, 'TypeBoolean');
81 4
                }
82 18
                if ('_date' == substr($key, -5)) {
83 18
                    StaticValidator::execute($value, 'Timestamp');
84 14
                    $value = StaticFilter::execute($value, 'TimestampToDateTime');
85 7
                }
86 18
                $ret[$property] = $value;
87 9
            }
88 9
        }
89
90 8
        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 8
    protected function buildFindAndReplaceDataArray(array $array)
101
    {
102 8
        $ret = [];
103
104 8
        foreach ($array as $key => $value) {
105 8
            array_push($ret, [
106 8
                $key,
107 8
                $value,
108 4
            ]);
109 4
        }
110
111 8
        return $ret;
112
    }
113
}
114