| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Scaffolder\Compilers\AngularJs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Facades\File; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Scaffolder\Compilers\AbstractCompiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Scaffolder\Compilers\Support\FileToCompile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Scaffolder\Compilers\Support\PathParser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Scaffolder\Support\Directory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class ListControllerCompiler extends AbstractCompiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	protected $cachePrefix 	= 'list_controller_'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	protected $stubFilename = 'ListController.js' ; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | 	public function __construct($scaffolderConfig, $modelData = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | 		$this->stubsDirectory = __DIR__ . '/../../../../stubs/AngularJs/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | 		parent::__construct($scaffolderConfig, $modelData); | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * Replace and store the Stub. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	public function replaceAndStore() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		return $this->replaceColumns() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 					->store(new FileToCompile(false, $this->modelData->modelHash)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * replace columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	public function replaceColumns(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		$columns = $this->getGridColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$this->stub = str_replace('{{grid_columns}}',  join(",\n ", $columns) , $this->stub);  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		return $this ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	public function getGridColumns(){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$columns = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		foreach ($this->modelData->fields as $field) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			array_push($columns,  "\t\t\t".sprintf('{ name: vm. $t("%s.columns.%s"), field: "%s%s" }',  $this->modelData->tableName, $field->name, $this->eagerTable, $field->name ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			// Check foreign key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 			if ($field->foreignKey && isset($field->foreignKey->eager) && $field->foreignKey->eager) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 				// search eager fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 				$foreignModelData = $this->getModelData($field->foreignKey->table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 				$foreignControllerCompiler = new ListControllerCompiler($this->scaffolderConfig, $foreignModelData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 				$foreignControllerCompiler->setEagerTable($field->foreignKey->table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 				$eagerColumns 	= $foreignControllerCompiler->getGridColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 				$columns = array_merge($columns, $eagerColumns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		return $columns;  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * Get output filename | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	protected function getOutputFilename() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		$folder = PathParser::parse($this->scaffolderConfig->generator->paths->pages).$this->modelData->tableName.'/list/' ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		Directory::createIfNotExists($folder, 0755, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		return $folder .$this->modelData->tableName . '_list.controller.js'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  | } | 
            
                        
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.