Passed
Push — master ( e0a48b...50471a )
by Jonathan
16:31
created

BuildTrait::buildDocumentsArray()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 24
ccs 18
cts 18
cp 1
rs 9.3888
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
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\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       $array       Array
40
     * @param PropertyMap $propertyMap PropertyMap
41
     *
42
     * @return array
43
     */
44 30
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
45
    {
46 30
        $ret = [];
47
48 30
        foreach ($array as $key => $value) {
49 30
            $map = $propertyMap->getMap();
50 30
            if (isset($map[$key])) {
51 30
                $key = $map[$key];
52
            }
53 30
            if (is_array($value)) {
54 12
                $value = $this->buildPropertyMapArray($value, $propertyMap);
55
            }
56 30
            $ret[$key] = $value;
57
        }
58
59 30
        return $ret;
60
    }
61
62
    /**
63
     * Using passed documentsData array, build array for backend
64
     *
65
     * @param array $array
66
     *
67
     * @return array
68
     * @throws InvalidArgumentException
69
     */
70 3
    protected function buildDocumentsArray(array $array): array
71
    {
72 3
        $ret = [];
73
74 3
        foreach ($array as $inner) {
75 3
            Assert::isArray($inner);
76 3
            $document = [];
77 3
            foreach ($inner as $key => $value) {
78 2
                switch ($key) {
79 3
                    case 'filename':
80 3
                        Assert::filenameExists($value);
81 3
                        Assert::assertDocumentExtension($value);
82 3
                        $document['document'] = FileUtils::read($value, true);
83 3
                        break;
84 3
                    case 'divider':
85 3
                        Assert::assertDocumentDivider($value);
86 3
                        $document['documentDivider'] = $value;
87 3
                        break;
88
                }
89
            }
90 3
            $ret[] = $document;
91
        }
92
93 3
        return $ret;
94
    }
95
96
    /**
97
     * Using passed documentsSettings array, build array for backend
98
     *
99
     * @param array $array
100
     *
101
     * @return array
102
     * @throws InvalidArgumentException
103
     */
104 3
    protected function buildDocumentSettingsArray(array $array): array
105
    {
106 3
        $ret = [];
107
108 3
        $propertyMap = new DocumentSettingsPropertyMap();
109
110 3
        $map = $propertyMap->getMap();
111
112 3
        if (!is_array($map)) {
113
            return $ret;
114
        }
115
116 3
        foreach ($map as $property => $key) {
117 3
            $key = (string) $key;
118 3
            if (!isset($array[$key])) {
119
                continue;
120
            }
121 3
            $value = $array[$key];
122 3
            if (StringUtils::endsWith($key, '_date')) {
123 3
                Assert::assertTimestamp($value);
124 3
                $value = Filter::filterTimestampToDateTime($value);
125
            }
126 3
            $ret[$property] = $value;
127
        }
128
129 3
        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 27
    protected function buildMergeSettingsArray(array $array): array
141
    {
142 27
        $ret = [];
143
144 27
        $propertyMap = new MergeSettingsPropertyMap();
145
146 27
        $map = $propertyMap->getMap();
147
148 27
        if (!is_array($map)) {
149
            return $ret;
150
        }
151
152 27
        foreach ($map as $property => $key) {
153 27
            $key = (string) $key;
154 27
            if (!isset($array[$key])) {
155 18
                continue;
156
            }
157 27
            $value = $array[$key];
158 27
            if ('culture' === $key) {
159 3
                Assert::assertCulture($value);
160
            }
161 27
            if (StringUtils::startsWith($key, 'remove_')) {
162 18
                Assert::boolean($value);
163
            }
164 27
            if (StringUtils::endsWith($key, '_date')) {
165 27
                Assert::assertTimestamp($value);
166 21
                $value = Filter::filterTimestampToDateTime($value);
167
            }
168 27
            $ret[$property] = $value;
169
        }
170
171 12
        return $ret;
172
    }
173
174
    /**
175
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
176
     *
177
     * @param array $array
178
     *
179
     * @return array
180
     */
181 30
    protected function buildFindAndReplaceDataArray(array $array): array
182
    {
183 30
        $ret = [];
184
185 30
        foreach ($array as $key => $value) {
186 30
            $ret[] = [
187 30
                $key,
188 30
                $value,
189
            ];
190
        }
191
192 30
        return $ret;
193
    }
194
195
    // </editor-fold>
196
}
197