Passed
Branch development-2.0 (f1893c)
by Jonathan
06:14
created

BuildTrait::buildPropertyMapArray()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 5
nop 2
crap 4
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
23
/**
24
 * Trait BuildTrait
25
 *
26
 * @package TxTextControl\ReportingCloud
27
 * @author  Jonathan Maron (@JonathanMaron)
28
 */
29
trait BuildTrait
30
{
31
    /**
32
     * Build Methods
33
     * -----------------------------------------------------------------------------------------------------------------
34
     */
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 7
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
45
    {
46 7
        $ret = [];
47
48 7
        foreach ($array as $key => $value) {
49 7
            $map = $propertyMap->getMap();
50 7
            if (isset($map[$key])) {
51 7
                $key = $map[$key];
52
            }
53 7
            if (is_array($value)) {
54 3
                $value = $this->buildPropertyMapArray($value, $propertyMap);
55
            }
56 7
            $ret[$key] = $value;
57
        }
58
59 7
        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 \Exception
69
     */
70 1
    protected function buildDocumentsArray(array $array): array
71
    {
72 1
        $ret = [];
73
74 1
        foreach ($array as $inner) {
75 1
            Assert::isArray($inner);
76 1
            $document = [];
77 1
            foreach ($inner as $key => $value) {
78
                switch ($key) {
79 1
                    case 'filename':
80 1
                        Assert::filenameExists($value);
81 1
                        Assert::assertDocumentExtension($value);
82 1
                        $value                = realpath($value);
83 1
                        $binary               = file_get_contents($value);
84 1
                        $document['document'] = base64_encode($binary);
85 1
                        break;
86 1
                    case 'divider':
87 1
                        Assert::assertDocumentDivider($value);
88 1
                        $document['documentDivider'] = $value;
89 1
                        break;
90
                }
91
            }
92 1
            $ret[] = $document;
93
        }
94
95 1
        return $ret;
96
    }
97
98
    /**
99
     * Using passed documentsSettings array, build array for backend
100
     *
101
     * @param array $array
102
     *
103
     * @return array
104
     * @throws \Exception
105
     */
106 1
    protected function buildDocumentSettingsArray(array $array): array
107
    {
108 1
        $ret = [];
109
110 1
        $propertyMap = new DocumentSettingsPropertyMap();
111
112 1
        foreach ($propertyMap->getMap() as $property => $key) {
113 1
            if (isset($array[$key])) {
114 1
                $value = $array[$key];
115 1
                if ('_date' == substr($key, -5)) {
116 1
                    Assert::assertTimestamp($value);
117 1
                    $value = Filter::filterTimestampToDateTime($value);
118
                }
119 1
                $ret[$property] = $value;
120
            }
121
        }
122
123 1
        return $ret;
124
    }
125
126
    /**
127
     * Using passed mergeSettings array, build array for backend
128
     *
129
     * @param array $array MergeSettings array
130
     *
131
     * @return array
132
     * @throws \Exception
133
     */
134 9
    protected function buildMergeSettingsArray(array $array): array
135
    {
136 9
        $ret = [];
137
138 9
        $propertyMap = new MergeSettingsPropertyMap();
139
140 9
        foreach ($propertyMap->getMap() as $property => $key) {
141 9
            if (isset($array[$key])) {
142 9
                $value = $array[$key];
143 9
                if ('culture' == $key) {
144 1
                    Assert::assertCulture($value);
145
                }
146 9
                if ('remove_' == substr($key, 0, 7)) {
147 6
                    Assert::boolean($value);
148
                }
149 9
                if ('_date' == substr($key, -5)) {
150 9
                    Assert::assertTimestamp($value);
151 7
                    $value = Filter::filterTimestampToDateTime($value);
152
                }
153 9
                $ret[$property] = $value;
154
            }
155
        }
156
157 4
        return $ret;
158
    }
159
160
    /**
161
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
162
     *
163
     * @param array $array
164
     *
165
     * @return array
166
     */
167 4
    protected function buildFindAndReplaceDataArray(array $array): array
168
    {
169 4
        $ret = [];
170
171 4
        foreach ($array as $key => $value) {
172 4
            array_push($ret, [
173 4
                $key,
174 4
                $value,
175
            ]);
176
        }
177
178 4
        return $ret;
179
    }
180
}
181