-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildTrait.php
More file actions
167 lines (143 loc) · 4.68 KB
/
Copy pathBuildTrait.php
File metadata and controls
167 lines (143 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
/**
* ReportingCloud PHP SDK
*
* PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
*
* @link https://www.reporting.cloud to learn more about ReportingCloud
* @link https://tinyurl.com/vmbbh6kd for the canonical source repository
* @license https://tinyurl.com/3pc9am89
* @copyright © 2023 Text Control GmbH
*/
namespace TextControl\ReportingCloud;
use TextControl\ReportingCloud\Assert\Assert;
use TextControl\ReportingCloud\Filter\Filter;
use TextControl\ReportingCloud\PropertyMap\AbstractPropertyMap as PropertyMap;
use TextControl\ReportingCloud\PropertyMap\DocumentSettings as DocumentSettingsPropertyMap;
use TextControl\ReportingCloud\PropertyMap\MergeSettings as MergeSettingsPropertyMap;
use TextControl\ReportingCloud\Stdlib\FileUtils;
/**
* Trait BuildTrait
*/
trait BuildTrait
{
/**
* Using the passed propertyMap, recursively build array
*
* @param array $array Array
* @param PropertyMap $propertyMap PropertyMap
*/
protected function buildPropertyMapArray(array $array, PropertyMap $propertyMap): array
{
$ret = [];
foreach ($array as $key => $value) {
$map = $propertyMap->getMap();
if (isset($map[$key])) {
$key = $map[$key];
}
if (is_array($value)) {
$value = $this->buildPropertyMapArray($value, $propertyMap);
}
$ret[$key] = $value;
}
return $ret;
}
/**
* Using passed documentsData array, build array for backend
*/
protected function buildDocumentsArray(array $array): array
{
$ret = [];
foreach ($array as $inner) {
assert(is_array($inner));
$document = [];
foreach ($inner as $key => $value) {
if ('filename' === $key) {
assert(is_string($value));
Assert::assertFilenameExists($value);
Assert::assertDocumentExtension($value);
$document['document'] = FileUtils::read($value, true);
continue;
}
if ('divider' === $key) {
assert(is_int($value));
Assert::assertDocumentDivider($value);
$document['documentDivider'] = $value;
}
}
$ret[] = $document;
}
return $ret;
}
/**
* Using passed documentsSettings array, build array for backend
*/
protected function buildDocumentSettingsArray(array $array): array
{
$ret = [];
$propertyMap = new DocumentSettingsPropertyMap();
$map = $propertyMap->getMap();
if ([] === $map) {
return $ret;
}
foreach ($map as $property => $key) {
if (!isset($array[$key])) {
continue;
}
$value = $array[$key];
if (str_ends_with($key, '_date') && is_int($value)) {
Assert::assertTimestamp($value);
$value = Filter::filterTimestampToDateTime($value);
}
$ret[$property] = $value;
}
return $ret;
}
/**
* Using passed mergeSettings array, build array for backend
*
* @param array $array MergeSettings array
*/
protected function buildMergeSettingsArray(array $array): array
{
$ret = [];
$propertyMap = new MergeSettingsPropertyMap();
$map = $propertyMap->getMap();
if ([] === $map) {
return $ret;
}
foreach ($map as $property => $key) {
if (!isset($array[$key])) {
continue;
}
$value = $array[$key];
if ('culture' === $key) {
assert(is_string($value));
Assert::assertCulture($value);
}
if (str_starts_with($key, 'remove_')) {
Assert::assertRemove($value);
assert(is_bool($value));
}
if (str_ends_with($key, '_date')) {
assert(is_int($value));
Assert::assertTimestamp($value);
$value = Filter::filterTimestampToDateTime($value);
}
$ret[$property] = $value;
}
return $ret;
}
/**
* Using passed findAndReplaceData associative array (key-value), build array for backend (list of string arrays)
*/
protected function buildFindAndReplaceDataArray(array $array): array
{
$ret = [];
foreach ($array as $key => $value) {
$ret[] = [$key, $value];
}
return $ret;
}
}