Passed
Push — master ( fbc447...748ca4 )
by Jonathan
18:21
created

BuildTrait::buildMergeSettingsArray()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 25
ccs 16
cts 16
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 6
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\Filter\Filter;
19
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
20
use TxTextControl\ReportingCloud\PropertyMap\DocumentSettings as DocumentSettingsPropertyMap;
21
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap;
22
use TxTextControl\ReportingCloud\Stdlib\StringUtils;
23
24
/**
25
 * Trait BuildTrait
26
 *
27
 * @package TxTextControl\ReportingCloud
28
 * @author  Jonathan Maron (@JonathanMaron)
29
 */
30
trait BuildTrait
31
{
32
    /**
33
     * Build Methods
34
     * -----------------------------------------------------------------------------------------------------------------
35
     */
36
37
    /**
38
     * Using the passed propertyMap, recursively build array
39
     *
40
     * @param array       $array       Array
41
     * @param PropertyMap $propertyMap PropertyMap
42
     *
43
     * @return array
44
     */
45 21
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
46
    {
47 21
        $ret = [];
48
49 21
        foreach ($array as $key => $value) {
50 21
            $map = $propertyMap->getMap();
51 21
            if (isset($map[$key])) {
52 21
                $key = $map[$key];
53
            }
54 21
            if (is_array($value)) {
55 9
                $value = $this->buildPropertyMapArray($value, $propertyMap);
56
            }
57 21
            $ret[$key] = $value;
58
        }
59
60 21
        return $ret;
61
    }
62
63
    /**
64
     * Using passed documentsData array, build array for backend
65
     *
66
     * @param array $array
67
     *
68
     * @return array
69
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
70
     */
71 3
    protected function buildDocumentsArray(array $array): array
72
    {
73 3
        $ret = [];
74
75 3
        foreach ($array as $inner) {
76 3
            Assert::isArray($inner);
77 3
            $document = [];
78 3
            foreach ($inner as $key => $value) {
79 2
                switch ($key) {
80 3
                    case 'filename':
81 3
                        Assert::filenameExists($value);
82 3
                        Assert::assertDocumentExtension($value);
83 3
                        $filename             = realpath($value);
84 3
                        $data                 = file_get_contents($filename);
85 3
                        $data                 = base64_encode($data);
86 3
                        $document['document'] = $data;
87 3
                        break;
88 3
                    case 'divider':
89 3
                        Assert::assertDocumentDivider($value);
90 3
                        $document['documentDivider'] = $value;
91 3
                        break;
92
                }
93
            }
94 3
            $ret[] = $document;
95
        }
96
97 3
        return $ret;
98
    }
99
100
    /**
101
     * Using passed documentsSettings array, build array for backend
102
     *
103
     * @param array $array
104
     *
105
     * @return array
106
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
107
     * @throws \Exception
108
     */
109 3
    protected function buildDocumentSettingsArray(array $array): array
110
    {
111 3
        $ret = [];
112
113 3
        $propertyMap = new DocumentSettingsPropertyMap();
114
115 3
        foreach ($propertyMap->getMap() as $property => $key) {
116 3
            if (isset($array[$key])) {
117 3
                $value = $array[$key];
118 3
                if (StringUtils::endsWith($key, '_date')) {
119 3
                    Assert::assertTimestamp($value);
120 3
                    $value = Filter::filterTimestampToDateTime($value);
121
                }
122 3
                $ret[$property] = $value;
123
            }
124
        }
125
126 3
        return $ret;
127
    }
128
129
    /**
130
     * Using passed mergeSettings array, build array for backend
131
     *
132
     * @param array $array MergeSettings array
133
     *
134
     * @return array
135
     * @throws \TxTextControl\ReportingCloud\Exception\InvalidArgumentException
136
     * @throws \Exception
137
     */
138 27
    protected function buildMergeSettingsArray(array $array): array
139
    {
140 27
        $ret = [];
141
142 27
        $propertyMap = new MergeSettingsPropertyMap();
143
144 27
        foreach ($propertyMap->getMap() 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 12
        return $ret;
163
    }
164
165
    /**
166
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
167
     *
168
     * @param array $array
169
     *
170
     * @return array
171
     */
172 30
    protected function buildFindAndReplaceDataArray(array $array): array
173
    {
174 30
        $ret = [];
175
176 30
        foreach ($array as $key => $value) {
177 30
            $ret[] = [
178 30
                $key,
179 30
                $value,
180
            ];
181
        }
182
183 30
        return $ret;
184
    }
185
}
186