| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Consolidation\OutputFormatters\StructuredData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Consolidation\OutputFormatters\Options\FormatterOptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Consolidation\OutputFormatters\StructuredData\ListDataInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Consolidation\OutputFormatters\Transformations\PropertyParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Consolidation\OutputFormatters\Transformations\ReorderFields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Consolidation\OutputFormatters\Transformations\TableTransformation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Consolidation\OutputFormatters\Transformations\AssociativeListTableTransformation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Holds an array where each element of the array is one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * key : value pair.  The keys must be unique, as is typically | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * the case for associative arrays. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class AssociativeList extends AbstractStructuredList | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 4 |  |      * Restructure this data for output by converting it into a table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * transformation object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 4 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 4 |  |      * @param FormatterOptions $options Options that affect output formatting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @return Consolidation\OutputFormatters\Transformations\TableTransformation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 3 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function restructure(FormatterOptions $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 3 |  |         $data = [$this->getArrayCopy()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |         $options->setConfigurationDefault('list-orientation', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 3 |  |         $tableTransformer = $this->createTableTransformation($data, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return $tableTransformer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 32 | 2 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     public function getListData(FormatterOptions $options) | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 2 |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $data = $this->getArrayCopy(); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $defaults = $this->defaultOptions(); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $fieldLabels = $this->getReorderedFieldLabels([$data], $options, $defaults); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $result = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         foreach ($fieldLabels as $id => $label) { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             $result[$id] = $data[$id]; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         return $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function defaultOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             FormatterOptions::LIST_ORIENTATION => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ] + parent::defaultOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected function instantiateTableTransformation($data, $fieldLabels, $rowLabels) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return new AssociativeListTableTransformation($data, $fieldLabels, $rowLabels); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                        
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.