Completed
Push — master ( dc16a8...a0dbc9 )
by Jonathan
04:38
created

ReportingCloudTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 60
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildPropertyMapArray() 0 17 4
B buildMergeSettingsArray() 0 23 5
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * Official wrapper (authored by Text Control GmbH, publisher of ReportingCloud) to access ReportingCloud in PHP.
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 © 2016 Text Control GmbH
12
 */
13
namespace TxTextControl\ReportingCloud;
14
15
use TxTextControl\ReportingCloud\Filter\TimestampToDateTime as TimestampToDateTimeFilter;
16
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
17
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap;
18
use TxTextControl\ReportingCloud\Validator\StaticValidator;
19
20
/**
21
 * ReportingCloudTrait
22
 *
23
 * @package TxTextControl\ReportingCloud
24
 * @author  Jonathan Maron (@JonathanMaron)
25
 */
26
trait ReportingCloudTrait
27
{
28
29
    /**
30
     * Using the passed propertyMap, recursively build array
31
     *
32
     * @param array       $array       Array
33
     * @param PropertyMap $propertyMap PropertyMap
34
     *
35
     * @return array
36
     */
37
    protected function buildPropertyMapArray($array, PropertyMap $propertyMap)
38
    {
39
        $ret = [];
40
41
        foreach ($array as $key => $value) {
42
            $map = $propertyMap->getMap();
43
            if (isset($map[$key])) {
44
                $key = $map[$key];
45
            }
46
            if (is_array($value)) {
47
                $value = $this->buildPropertyMapArray($value, $propertyMap);
48
            }
49
            $ret[$key] = $value;
50
        }
51
52
        return $ret;
53
    }
54
55
    /**
56
     * Using passed mergeSettings array, build array for backend
57
     *
58
     * @param array $mergeSettings MergeSettings array
59
     *
60
     * @return array
61
     */
62
    protected function buildMergeSettingsArray($mergeSettings)
63
    {
64
        $ret = [];
65
66
        $filter      = new TimestampToDateTimeFilter();
67
        $propertyMap = new MergeSettingsPropertyMap();
68
69
        foreach ($propertyMap->getMap() as $property => $key) {
70
            if (isset($mergeSettings[$key])) {
71
                $value = $mergeSettings[$key];
72
                if ('remove_' == substr($key, 0, 7)) {
73
                    StaticValidator::execute($value, 'TypeBoolean');
74
                }
75
                if ('_date' == substr($key, -5)) {
76
                    StaticValidator::execute($value, 'Timestamp');
77
                    $value = $filter->filter($value);
78
                }
79
                $ret[$property] = $value;
80
            }
81
        }
82
83
        return $ret;
84
    }
85
}