Passed
Branch static-analysis (3fe576)
by Jonathan
19:22
created

BuildTrait   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 23
eloc 64
dl 0
loc 154
ccs 68
cts 68
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildDocumentSettingsArray() 0 21 5
A buildPropertyMapArray() 0 16 4
B buildMergeSettingsArray() 0 28 7
A buildDocumentsArray() 0 26 5
A buildFindAndReplaceDataArray() 0 12 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP Wrapper
6
 *
7
 * PHP wrapper 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 3
        if (is_array($map)) {
113 3
            foreach ($map as $property => $key) {
114 3
                if (isset($array[$key])) {
115 3
                    $value = $array[$key];
116 3
                    if (StringUtils::endsWith($key, '_date')) {
117 3
                        Assert::assertTimestamp($value);
118 3
                        $value = Filter::filterTimestampToDateTime($value);
119
                    }
120 3
                    $ret[$property] = $value;
121
                }
122
            }
123
        }
124
125 3
        return $ret;
126
    }
127
128
    /**
129
     * Using passed mergeSettings array, build array for backend
130
     *
131
     * @param array $array MergeSettings array
132
     *
133
     * @return array
134
     * @throws InvalidArgumentException
135
     */
136 27
    protected function buildMergeSettingsArray(array $array): array
137
    {
138 27
        $ret = [];
139
140 27
        $propertyMap = new MergeSettingsPropertyMap();
141
142 27
        $map = $propertyMap->getMap();
143 27
        if (is_array($map)) {
144 27
            foreach ($map as $property => $key) {
145 27
                if (!isset($array[$key])) {
146 18
                    continue;
147
                }
148 27
                $value = $array[$key];
149 27
                if ('culture' === $key) {
150 3
                    Assert::assertCulture($value);
151
                }
152 27
                if (StringUtils::startsWith($key, 'remove_')) {
153 18
                    Assert::boolean($value);
154
                }
155 27
                if (StringUtils::endsWith($key, '_date')) {
156 27
                    Assert::assertTimestamp($value);
157 21
                    $value = Filter::filterTimestampToDateTime($value);
158
                }
159 27
                $ret[$property] = $value;
160
            }
161
        }
162
163 12
        return $ret;
164
    }
165
166
    /**
167
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
168
     *
169
     * @param array $array
170
     *
171
     * @return array
172
     */
173 30
    protected function buildFindAndReplaceDataArray(array $array): array
174
    {
175 30
        $ret = [];
176
177 30
        foreach ($array as $key => $value) {
178 30
            $ret[] = [
179 30
                $key,
180 30
                $value,
181
            ];
182
        }
183
184 30
        return $ret;
185
    }
186
187
    // </editor-fold>
188
}
189