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