| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SetBased\Audit\MySql\Metadata; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class for the metadata of a database table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class TableMetadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |    * The properties of the table that are stored by this class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |    * var string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |   private static $fields = ['table_schema', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |                             'table_name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |                             'engine', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |                             'character_set_name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |                             'table_collation']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |    * The metadata of the columns of this table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |    * @var TableColumnsMetadata. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   private $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |    * The the properties of this table column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |    * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |   private $properties = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |    * Object constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |    * @param array[] $properties The metadata of the table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |    * @param array[] $columns    The metadata of the columns of this table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 25 |  |   public function __construct($properties, $columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 25 |  |     foreach (static::$fields as $field) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 25 |  |       if (isset($properties[$field])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |       { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 25 |  |         $this->properties[$field] = $properties[$field]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 25 |  |     $this->columns = new TableColumnsMetadata($columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 25 |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |    * Compares two the metadata of two tables. Returns an array with the names of the different properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |    * @param TableMetadata $table1 The metadata of the first table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |    * @param TableMetadata $table2 The metadata of the second table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |    * @return string[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 8 |  |   public static function compareOptions($table1, $table2) | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |   { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 8 |  |     $diff = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 8 |  |     foreach (self::$fields as $field) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 8 |  |       if (!in_array($field, ['table_schema', 'table_name'])) | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |       { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 8 |  |         if ($table1->getProperty($field)!=$table2->getProperty($field)) | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 8 |  |           $diff[] = $field; | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |       } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 8 |  |     return $diff; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |    * Returns table columns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |    * @return TableColumnsMetadata | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 25 |  |   public function getColumns() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 25 |  |     return $this->columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |    * Returns a property of this table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |    * @param string $name The name of the property. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |    * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 8 |  |   public function getProperty($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 8 |  |     if (isset($this->properties[$name])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 8 |  |       return $this->properties[$name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |    * Returns the name of schema. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 25 |  |   public function getSchemaName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 25 |  |     return $this->properties['table_schema']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |    * Returns the name of this table. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 25 |  |   public function getTableName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |   { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 25 |  |     return $this->properties['table_name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |   //-------------------------------------------------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  | //---------------------------------------------------------------------------------------------------------------------- | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  | 
            
                        
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: