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

BuildTrait::startsWith()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\Stdlib\Stdlib;
18
use TxTextControl\ReportingCloud\Assert\Assert;
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
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 7
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
46
    {
47 7
        $ret = [];
48
49 7
        foreach ($array as $key => $value) {
50 7
            $map = $propertyMap->getMap();
51 7
            if (isset($map[$key])) {
52 7
                $key = $map[$key];
53
            }
54 7
            if (is_array($value)) {
55 3
                $value = $this->buildPropertyMapArray($value, $propertyMap);
56
            }
57 7
            $ret[$key] = $value;
58
        }
59
60 7
        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 1
    protected function buildDocumentsArray(array $array): array
72
    {
73 1
        $ret = [];
74
75 1
        foreach ($array as $inner) {
76 1
            Assert::isArray($inner);
77 1
            $document = [];
78 1
            foreach ($inner as $key => $value) {
79
                switch ($key) {
80 1
                    case 'filename':
81 1
                        Assert::filenameExists($value);
82 1
                        Assert::assertDocumentExtension($value);
83 1
                        $filename             = realpath($value);
84 1
                        $data                 = file_get_contents($filename);
85 1
                        $data                 = base64_encode($data);
86 1
                        $document['document'] = $data;
87 1
                        break;
88 1
                    case 'divider':
89 1
                        Assert::assertDocumentDivider($value);
90 1
                        $document['documentDivider'] = $value;
91 1
                        break;
92
                }
93
            }
94 1
            $ret[] = $document;
95
        }
96
97 1
        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 1
    protected function buildDocumentSettingsArray(array $array): array
110
    {
111 1
        $ret = [];
112
113 1
        $propertyMap = new DocumentSettingsPropertyMap();
114
115 1
        foreach ($propertyMap->getMap() as $property => $key) {
116 1
            if (isset($array[$key])) {
117 1
                $value = $array[$key];
118 1
                if (Stdlib::endsWith($key, '_date')) {
119 1
                    Assert::assertTimestamp($value);
120 1
                    $value = Filter::filterTimestampToDateTime($value);
121
                }
122 1
                $ret[$property] = $value;
123
            }
124
        }
125
126 1
        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 9
    protected function buildMergeSettingsArray(array $array): array
139
    {
140 9
        $ret = [];
141
142 9
        $propertyMap = new MergeSettingsPropertyMap();
143
144 9
        foreach ($propertyMap->getMap() as $property => $key) {
145 9
            if (!isset($array[$key])) {
146 6
                continue;
147
            }
148 9
            $value = $array[$key];
149 9
            if ('culture' === $key) {
150 1
                Assert::assertCulture($value);
151
            }
152 9
            if (Stdlib::startsWith($key, 'remove_')) {
153 6
                Assert::boolean($value);
154
            }
155 9
            if (Stdlib::endsWith($key, '_date')) {
156 9
                Assert::assertTimestamp($value);
157 7
                $value = Filter::filterTimestampToDateTime($value);
158
            }
159 9
            $ret[$property] = $value;
160
        }
161
162 4
        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 10
    protected function buildFindAndReplaceDataArray(array $array): array
173
    {
174 10
        $ret = [];
175
176 10
        foreach ($array as $key => $value) {
177 10
            $ret[] = [
178 10
                $key,
179 10
                $value,
180
            ];
181
        }
182
183 10
        return $ret;
184
    }
185
}
186