Passed
Branch append-document-1.0 (2fd487)
by Jonathan
17:18
created

BuildTrait::buildDocumentsArray()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 26
ccs 21
cts 21
cp 1
rs 9.3222
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud;
15
16
use TxTextControl\ReportingCloud\Filter\StaticFilter;
17
use TxTextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
18
use TxTextControl\ReportingCloud\PropertyMap\DocumentSettings as DocumentSettingsPropertyMap;
19
use TxTextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap;
20
use TxTextControl\ReportingCloud\Validator\StaticValidator;
21
22
/**
23
 * Trait BuildTrait
24
 *
25
 * @package TxTextControl\ReportingCloud
26
 * @author  Jonathan Maron (@JonathanMaron)
27
 */
28
trait BuildTrait
29
{
30
    /**
31
     * Build Methods
32
     * -----------------------------------------------------------------------------------------------------------------
33
     */
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 14
    protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap)
44
    {
45 14
        $ret = [];
46
47 14
        foreach ($array as $key => $value) {
48 14
            $map = $propertyMap->getMap();
49 14
            if (isset($map[$key])) {
50 14
                $key = $map[$key];
51 7
            }
52 14
            if (is_array($value)) {
53 6
                $value = $this->buildPropertyMapArray($value, $propertyMap);
54 3
            }
55 14
            $ret[$key] = $value;
56 7
        }
57
58 14
        return $ret;
59
    }
60
61
    /**
62
     * Using passed documentsData array, build array for backend
63
     *
64
     * @param array $array AppendDocument array
65
     *
66
     * @return array
67
     */
68 2
    protected function buildDocumentsArray(array $array)
69
    {
70 2
        $ret = [];
71
72 2
        foreach ($array as $inner) {
73 2
            StaticValidator::execute($inner, 'TypeArray');
74 2
            $document = [];
75 2
            foreach ($inner as $key => $value) {
76
                switch ($key) {
77 2
                    case 'filename':
78 2
                        StaticValidator::execute($value, 'FileExists');
79 2
                        StaticValidator::execute($value, 'DocumentExtension');
80 2
                        $value                = realpath($value);
81 2
                        $binary               = file_get_contents($value);
82 2
                        $document['document'] = base64_encode($binary);
83 2
                        break;
84 2
                    case 'divider':
85 2
                        StaticValidator::execute($value, 'DocumentDivider');
86 2
                        $document['documentDivider'] = $value;
87 2
                        break;
88
                }
89 1
            }
90 2
            $ret[] = $document;
91 1
        }
92
93 2
        return $ret;
94
    }
95
96
    /**
97
     * Using passed documentsSettings array, build array for backend
98
     *
99
     * @param array $array
100
     *
101
     * @return array
102
     */
103 2
    protected function buildDocumentSettingsArray(array $array)
104
    {
105 2
        $ret = [];
106
107 2
        $propertyMap = new DocumentSettingsPropertyMap();
108
109 2
        foreach ($propertyMap->getMap() as $property => $key) {
110 2
            if (isset($array[$key])) {
111 2
                $value = $array[$key];
112 2
                if ('_date' == substr($key, -5)) {
113 2
                    StaticValidator::execute($value, 'Timestamp');
114 2
                    $value = StaticFilter::execute($value, 'TimestampToDateTime');
115 1
                }
116 2
                $ret[$property] = $value;
117 1
            }
118 1
        }
119
120 2
        return $ret;
121
    }
122
123
    /**
124
     * Using passed mergeSettings array, build array for backend
125
     *
126
     * @param array $array MergeSettings array
127
     *
128
     * @return array
129
     */
130 18
    protected function buildMergeSettingsArray(array $array)
131
    {
132 18
        $ret = [];
133
134 18
        $propertyMap = new MergeSettingsPropertyMap();
135
136 18
        foreach ($propertyMap->getMap() as $property => $key) {
137 18
            if (isset($array[$key])) {
138 18
                $value = $array[$key];
139 18
                if ('culture' == $key) {
140 2
                    StaticValidator::execute($value, 'Culture');
141
                }
142 18
                if ('remove_' == substr($key, 0, 7)) {
143 12
                    StaticValidator::execute($value, 'TypeBoolean');
144 4
                }
145 18
                if ('_date' == substr($key, -5)) {
146 18
                    StaticValidator::execute($value, 'Timestamp');
147 14
                    $value = StaticFilter::execute($value, 'TimestampToDateTime');
148 7
                }
149 18
                $ret[$property] = $value;
150 9
            }
151 9
        }
152
153 8
        return $ret;
154
    }
155
156
    /**
157
     * Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
158
     *
159
     * @param array $array FindAndReplaceData array
160
     *
161
     * @return array
162
     */
163 8
    protected function buildFindAndReplaceDataArray(array $array)
164
    {
165 8
        $ret = [];
166
167 8
        foreach ($array as $key => $value) {
168 8
            array_push($ret, [
169 8
                $key,
170 8
                $value,
171 4
            ]);
172 4
        }
173
174 8
        return $ret;
175
    }
176
}
177