| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Consolidation\OutputFormatters\Transformations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Consolidation\OutputFormatters\StructuredData\TableDataInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Consolidation\OutputFormatters\StructuredData\OriginalDataInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | class TableTransformation extends \ArrayObject implements TableDataInterface, OriginalDataInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     protected $headers; | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     protected $rowLabels; | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     protected $layout; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     const TABLE_LAYOUT = 'table'; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 | 7 |  |     const LIST_LAYOUT = 'list'; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 7 |  |     public function __construct($data, $fieldLabels, $rowLabels = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 7 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 7 |  |         $this->headers = $fieldLabels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 7 |  |         $this->rowLabels = $rowLabels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 7 |  |         $rows = static::transformRows($data, $fieldLabels); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->layout = self::TABLE_LAYOUT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 3 |  |         parent::__construct($rows); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 3 |  |     public function setLayout($layout) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->layout = $layout; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function getLayout() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 7 |  |         return $this->layout; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function isList() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 7 |  |         return $this->layout == self::LIST_LAYOUT; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 7 |  |     protected static function transformRows($data, $fieldLabels) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 7 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 7 |  |         $rows = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 7 |  |         foreach ($data as $rowid => $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $rows[$rowid] = static::transformRow($row, $fieldLabels); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 7 |  |         return $rows; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 7 |  |     protected static function transformRow($row, $fieldLabels) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 7 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 7 |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 7 |  |         foreach ($fieldLabels as $key => $label) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $result[$key] = array_key_exists($key, $row) ? $row[$key] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 7 |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function getHeaders() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |         return $this->headers; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |     public function getHeader($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (array_key_exists($key, $this->headers)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return $this->headers[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         return $key; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function getRowLabels() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 7 |  |         return $this->rowLabels; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 7 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 7 |  |     public function getRowLabel($rowid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |         if (array_key_exists($rowid, $this->rowLabels)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 7 |  |             return $this->rowLabels[$rowid]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |         return $rowid; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 82 | 7 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function getOriginalData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return $this->getArrayCopy(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 88 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 3 |  |     public function getTableData($includeRowKey = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |         $data = $this->getArrayCopy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  |         if ($this->isList()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |             $data = $this->convertTableToList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if ($includeRowKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |             $data = $this->getRowDataWithKey($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |         return $data; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |     protected function convertTableToList() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 3 |  |         foreach ($this as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 3 |  |             foreach ($row as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 $result[$key][] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     protected function getRowDataWithKey($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $result = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $i = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         foreach ($data as $key => $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             array_unshift($row, $this->getHeader($key)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $i++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $result[$key] = $row; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |