BuildTrait   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Test Coverage

Coverage 95.95%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 24
eloc 70
c 8
b 0
f 0
dl 0
loc 163
ccs 71
cts 74
cp 0.9595
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildPropertyMapArray() 0 16 4
A buildDocumentSettingsArray() 0 25 6
B buildMergeSettingsArray() 0 34 7
A buildDocumentsArray() 0 26 5
A buildFindAndReplaceDataArray() 0 12 2
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 © 2022 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
     * Using the passed propertyMap, recursively build array
37
     *
38
     * @param array       $array       Array
39
     * @param PropertyMap $propertyMap PropertyMap
40
     *
41
     * @return array
42
     */
43 24
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
44
    {
45 24
        $ret = [];
46
47 24
        foreach ($array as $key => $value) {
48 24
            $map = $propertyMap->getMap();
49 24
            if (isset($map[$key])) {
50 24
                $key = $map[$key];
51
            }
52 24
            if (is_array($value)) {
53 10
                $value = $this->buildPropertyMapArray($value, $propertyMap);
54
            }
55 24
            $ret[$key] = $value;
56
        }
57
58 24
        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 2
    protected function buildDocumentsArray(array $array): array
70
    {
71 2
        $ret = [];
72
73 2
        foreach ($array as $inner) {
74 2
            assert(is_array($inner));
75 2
            $document = [];
76 2
            foreach ($inner as $key => $value) {
77
                switch ($key) {
78 2
                    case 'filename':
79 2
                        assert(is_string($value));
80 2
                        Assert::assertFilenameExists($value);
81 2
                        Assert::assertDocumentExtension($value);
82 2
                        $document['document'] = FileUtils::read($value, true);
83 2
                        break;
84 2
                    case 'divider':
85 2
                        assert(is_int($value));
86 2
                        Assert::assertDocumentDivider($value);
87 2
                        $document['documentDivider'] = $value;
88 2
                        break;
89
                }
90
            }
91 2
            $ret[] = $document;
92
        }
93
94 2
        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 2
    protected function buildDocumentSettingsArray(array $array): array
106
    {
107 2
        $ret = [];
108
109 2
        $propertyMap = new DocumentSettingsPropertyMap();
110
111 2
        $map = $propertyMap->getMap();
112
113 2
        if (0 === count($map)) {
114
            return $ret;
115
        }
116
117 2
        foreach ($map as $property => $key) {
118 2
            if (!isset($array[$key])) {
119
                continue;
120
            }
121 2
            $value = $array[$key];
122 2
            if (StringUtils::endsWith($key, '_date') && is_int($value)) {
123 2
                Assert::assertTimestamp($value);
124 2
                $value = Filter::filterTimestampToDateTime($value);
125
            }
126 2
            $ret[$property] = $value;
127
        }
128
129 2
        return $ret;
130
    }
131
132
    /**
133
     * Using passed mergeSettings array, build array for backend
134
     *
135
     * @param array $array MergeSettings array
136
     *
137
     * @return array
138
     * @throws InvalidArgumentException
139
     */
140 18
    protected function buildMergeSettingsArray(array $array): array
141
    {
142 18
        $ret = [];
143
144 18
        $propertyMap = new MergeSettingsPropertyMap();
145
146 18
        $map = $propertyMap->getMap();
147
148 18
        if (0 === count($map)) {
149
            return $ret;
150
        }
151
152 18
        foreach ($map as $property => $key) {
153 18
            if (!isset($array[$key])) {
154 12
                continue;
155
            }
156 18
            $value = $array[$key];
157 18
            if ('culture' === $key) {
158 2
                assert(is_string($value));
159 2
                Assert::assertCulture($value);
160
            }
161 18
            if (StringUtils::startsWith($key, 'remove_')) {
162 12
                Assert::assertRemove($value);
163 8
                assert(is_bool($value));
164
            }
165 18
            if (StringUtils::endsWith($key, '_date')) {
166 18
                assert(is_int($value));
167 18
                Assert::assertTimestamp($value);
168 14
                $value = Filter::filterTimestampToDateTime($value);
169
            }
170 18
            $ret[$property] = $value;
171
        }
172
173 8
        return $ret;
174
    }
175
176
    /**
177
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
178
     *
179
     * @param array $array
180
     *
181
     * @return array
182
     */
183 20
    protected function buildFindAndReplaceDataArray(array $array): array
184
    {
185 20
        $ret = [];
186
187 20
        foreach ($array as $key => $value) {
188 20
            $ret[] = [
189 20
                $key,
190 20
                $value,
191 20
            ];
192
        }
193
194 20
        return $ret;
195
    }
196
197
    // </editor-fold>
198
}
199