Passed
Push — master ( 2b97dd...4cf6b1 )
by Jonathan
20:51
created

BuildTrait::buildMergeSettingsArray()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7.0061

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 32
ccs 19
cts 20
cp 0.95
rs 8.8333
c 0
b 0
f 0
cc 7
nc 11
nop 1
crap 7.0061
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud;
16
17
use TxTextControl\ReportingCloud\Assert\Assert;
18
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
19
use TxTextControl\ReportingCloud\Filter\Filter;
20
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
21
use TxTextControl\ReportingCloud\PropertyMap\DocumentSettings as DocumentSettingsPropertyMap;
22
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap;
23
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
24
25
/**
26
 * Trait BuildTrait
27
 *
28
 * @package TxTextControl\ReportingCloud
29
 * @author  Jonathan Maron (@JonathanMaron)
30
 */
31
trait BuildTrait
32
{
33
    // <editor-fold desc="Methods">
34
35
    /**
36
     * Using the passed propertyMap, recursively build array
37
     *
38
     * @param array       $array       Array
39
     * @param PropertyMap $propertyMap PropertyMap
40
     *
41
     * @return array
42
     */
43 30
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
44
    {
45 30
        $ret = [];
46
47 30
        foreach ($array as $key => $value) {
48 30
            $map = $propertyMap->getMap();
49 30
            if (isset($map[$key])) {
50 30
                $key = $map[$key];
51
            }
52 30
            if (is_array($value)) {
53 12
                $value = $this->buildPropertyMapArray($value, $propertyMap);
54
            }
55 30
            $ret[$key] = $value;
56
        }
57
58 30
        return $ret;
59
    }
60
61
    /**
62
     * Using passed documentsData array, build array for backend
63
     *
64
     * @param array $array
65
     *
66
     * @return array
67
     * @throws InvalidArgumentException
68
     */
69 3
    protected function buildDocumentsArray(array $array): array
70
    {
71 3
        $ret = [];
72
73 3
        foreach ($array as $inner) {
74 3
            Assert::isArray($inner);
75 3
            $document = [];
76 3
            foreach ($inner as $key => $value) {
77 2
                switch ($key) {
78 3
                    case 'filename':
79 3
                        Assert::filenameExists($value);
80 3
                        Assert::assertDocumentExtension($value);
81 3
                        $data                 = (string) file_get_contents($value);
82 3
                        $data                 = base64_encode($data);
83 3
                        $document['document'] = $data;
84 3
                        break;
85 3
                    case 'divider':
86 3
                        Assert::assertDocumentDivider($value);
87 3
                        $document['documentDivider'] = $value;
88 3
                        break;
89
                }
90
            }
91 3
            $ret[] = $document;
92
        }
93
94 3
        return $ret;
95
    }
96
97
    /**
98
     * Using passed documentsSettings array, build array for backend
99
     *
100
     * @param array $array
101
     *
102
     * @return array
103
     * @throws InvalidArgumentException
104
     */
105 3
    protected function buildDocumentSettingsArray(array $array): array
106
    {
107 3
        $ret = [];
108
109 3
        $propertyMap = new DocumentSettingsPropertyMap();
110
111 3
        $map = $propertyMap->getMap();
112
113 3
        if (!is_array($map)) {
114
            return $ret;
115
        }
116
117 3
        foreach ($map as $property => $key) {
118 3
            $key = (string) $key;
119 3
            if (!isset($array[$key])) {
120
                continue;
121
            }
122 3
            $value = $array[$key];
123 3
            if (StringUtils::endsWith($key, '_date')) {
124 3
                Assert::assertTimestamp($value);
125 3
                $value = Filter::filterTimestampToDateTime($value);
126
            }
127 3
            $ret[$property] = $value;
128
        }
129
130 3
        return $ret;
131
    }
132
133
    /**
134
     * Using passed mergeSettings array, build array for backend
135
     *
136
     * @param array $array MergeSettings array
137
     *
138
     * @return array
139
     * @throws InvalidArgumentException
140
     */
141 27
    protected function buildMergeSettingsArray(array $array): array
142
    {
143 27
        $ret = [];
144
145 27
        $propertyMap = new MergeSettingsPropertyMap();
146
147 27
        $map = $propertyMap->getMap();
148
149 27
        if (!is_array($map)) {
150
            return $ret;
151
        }
152
153 27
        foreach ($map as $property => $key) {
154 27
            $key = (string) $key;
155 27
            if (!isset($array[$key])) {
156 18
                continue;
157
            }
158 27
            $value = $array[$key];
159 27
            if ('culture' === $key) {
160 3
                Assert::assertCulture($value);
161
            }
162 27
            if (StringUtils::startsWith($key, 'remove_')) {
163 18
                Assert::boolean($value);
164
            }
165 27
            if (StringUtils::endsWith($key, '_date')) {
166 27
                Assert::assertTimestamp($value);
167 21
                $value = Filter::filterTimestampToDateTime($value);
168
            }
169 27
            $ret[$property] = $value;
170
        }
171
172 12
        return $ret;
173
    }
174
175
    /**
176
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
177
     *
178
     * @param array $array
179
     *
180
     * @return array
181
     */
182 30
    protected function buildFindAndReplaceDataArray(array $array): array
183
    {
184 30
        $ret = [];
185
186 30
        foreach ($array as $key => $value) {
187 30
            $ret[] = [
188 30
                $key,
189 30
                $value,
190
            ];
191
        }
192
193 30
        return $ret;
194
    }
195
196
    // </editor-fold>
197
}
198