Passed
Push — master ( b8e2b8...5d024f )
by Jonathan
17:35
created

BuildTrait::buildMergeSettingsArray()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7.0046

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 21
c 6
b 0
f 0
dl 0
loc 34
ccs 21
cts 22
cp 0.9545
rs 8.6506
cc 7
nc 11
nop 1
crap 7.0046
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://git.io/Jejj2 for the canonical source repository
11
 * @license   https://git.io/Jejjr
12
 * @copyright © 2021 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\FileUtils;
24
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
25
26
/**
27
 * Trait BuildTrait
28
 *
29
 * @package TxTextControl\ReportingCloud
30
 * @author  Jonathan Maron (@JonathanMaron)
31
 */
32
trait BuildTrait
33
{
34
    // <editor-fold desc="Methods">
35
36
    /**
37
     * Using the passed propertyMap, recursively build array
38
     *
39
     * @param array<int|string, array|bool|int|string> $array       Array
40
     * @param PropertyMap                              $propertyMap PropertyMap
41
     *
42
     * @return array<int|string, array|bool|int|string>
43
     */
44 24
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
45
    {
46 24
        $ret = [];
47
48 24
        foreach ($array as $key => $value) {
49 24
            $map = $propertyMap->getMap();
50 24
            if (isset($map[$key])) {
51 24
                $key = $map[$key];
52
            }
53 24
            if (is_array($value)) {
54 10
                $value = $this->buildPropertyMapArray($value, $propertyMap);
55
            }
56 24
            $ret[$key] = $value;
57
        }
58
59 24
        return $ret;
60
    }
61
62
    /**
63
     * Using passed documentsData array, build array for backend
64
     *
65
     * @param array<int|string, array|bool|int|string> $array
66
     *
67
     * @return array<int, array>
68
     * @throws InvalidArgumentException
69
     */
70 2
    protected function buildDocumentsArray(array $array): array
71
    {
72 2
        $ret = [];
73
74 2
        foreach ($array as $inner) {
75 2
            assert(is_array($inner));
76 2
            $document = [];
77 2
            foreach ($inner as $key => $value) {
78 2
                switch ($key) {
79 2
                    case 'filename':
80 2
                        assert(is_string($value));
81 2
                        Assert::assertFilenameExists($value);
82 2
                        Assert::assertDocumentExtension($value);
83 2
                        $document['document'] = FileUtils::read($value, true);
84 2
                        break;
85 2
                    case 'divider':
86 2
                        assert(is_int($value));
87 2
                        Assert::assertDocumentDivider($value);
88 2
                        $document['documentDivider'] = $value;
89 2
                        break;
90
                }
91
            }
92 2
            $ret[] = $document;
93
        }
94
95 2
        return $ret;
96
    }
97
98
    /**
99
     * Using passed documentsSettings array, build array for backend
100
     *
101
     * @param array<string, bool|int|string> $array
102
     *
103
     * @return array<string, bool|int|string>
104
     * @throws InvalidArgumentException
105
     */
106 2
    protected function buildDocumentSettingsArray(array $array): array
107
    {
108 2
        $ret = [];
109
110 2
        $propertyMap = new DocumentSettingsPropertyMap();
111
112 2
        $map = $propertyMap->getMap();
113
114 2
        if (0 === count($map)) {
115
            return $ret;
116
        }
117
118 2
        foreach ($map as $property => $key) {
119 2
            if (!isset($array[$key])) {
120
                continue;
121
            }
122 2
            $value = $array[$key];
123 2
            if (StringUtils::endsWith($key, '_date') && is_int($value)) {
124 2
                Assert::assertTimestamp($value);
125 2
                $value = Filter::filterTimestampToDateTime($value);
126
            }
127 2
            $ret[$property] = $value;
128
        }
129
130 2
        return $ret;
131
    }
132
133
    /**
134
     * Using passed mergeSettings array, build array for backend
135
     *
136
     * @param array<string, bool|int|string> $array MergeSettings array
137
     *
138
     * @return array<string, bool|int|string>
139
     * @throws InvalidArgumentException
140
     */
141 18
    protected function buildMergeSettingsArray(array $array): array
142
    {
143 18
        $ret = [];
144
145 18
        $propertyMap = new MergeSettingsPropertyMap();
146
147 18
        $map = $propertyMap->getMap();
148
149 18
        if (0 === count($map)) {
150
            return $ret;
151
        }
152
153 18
        foreach ($map as $property => $key) {
154 18
            if (!isset($array[$key])) {
155 12
                continue;
156
            }
157 18
            $value = $array[$key];
158 18
            if ('culture' === $key) {
159 2
                assert(is_string($value));
160 2
                Assert::assertCulture($value);
161
            }
162 18
            if (StringUtils::startsWith($key, 'remove_')) {
163 12
                Assert::assertRemove($value);
164 8
                assert(is_bool($value));
165
            }
166 18
            if (StringUtils::endsWith($key, '_date')) {
167 18
                assert(is_int($value));
168 18
                Assert::assertTimestamp($value);
169 14
                $value = Filter::filterTimestampToDateTime($value);
170
            }
171 18
            $ret[$property] = $value;
172
        }
173
174 8
        return $ret;
175
    }
176
177
    /**
178
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
179
     *
180
     * @param array<string, string> $array
181
     *
182
     * @return array<int, array<int, int|string>>
183
     */
184 20
    protected function buildFindAndReplaceDataArray(array $array): array
185
    {
186 20
        $ret = [];
187
188 20
        foreach ($array as $key => $value) {
189 10
            $ret[] = [
190 20
                $key,
191 20
                $value,
192
            ];
193
        }
194
195 20
        return $ret;
196
    }
197
198
    // </editor-fold>
199
}
200