@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | * @return mixed |
| 11 | 11 | */ |
| 12 | 12 | protected function escape( $input, $wrapChar = '`' ) { |
| 13 | - return str_replace($wrapChar, $wrapChar . $wrapChar, $input); |
|
| 13 | + return str_replace( $wrapChar, $wrapChar.$wrapChar, $input ); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** |
@@ -19,6 +19,6 @@ discard block |
||
| 19 | 19 | * @return string |
| 20 | 20 | */ |
| 21 | 21 | protected function mkString( $input, $wrapChar = '`' ) { |
| 22 | - return $wrapChar . $this->escape($input, $wrapChar) . $wrapChar; |
|
| 22 | + return $wrapChar.$this->escape( $input, $wrapChar ).$wrapChar; |
|
| 23 | 23 | } |
| 24 | 24 | } |
@@ -9,7 +9,7 @@ |
||
| 9 | 9 | * @param int $decimals |
| 10 | 10 | */ |
| 11 | 11 | public function __construct( $name, $decimals ) { |
| 12 | - parent::__construct($name); |
|
| 12 | + parent::__construct( $name ); |
|
| 13 | 13 | $this->decimals = $decimals; |
| 14 | 14 | } |
| 15 | 15 | |
@@ -14,16 +14,16 @@ |
||
| 14 | 14 | * @param int $length 2 or 4 |
| 15 | 15 | */ |
| 16 | 16 | public function __construct( $name, $length = 4 ) { |
| 17 | - parent::__construct($name); |
|
| 18 | - $this->setLength($length); |
|
| 17 | + parent::__construct( $name ); |
|
| 18 | + $this->setLength( $length ); |
|
| 19 | 19 | } |
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | 22 | * @param int $length 2 or 4 |
| 23 | 23 | */ |
| 24 | 24 | public function setLength( $length ) { |
| 25 | - if( $length != 2 && $length != 4 ) { |
|
| 26 | - throw new \InvalidArgumentException("invalid length '{$length}' - year can only be of length 2 or 4"); |
|
| 25 | + if ( $length != 2 && $length != 4 ) { |
|
| 26 | + throw new \InvalidArgumentException( "invalid length '{$length}' - year can only be of length 2 or 4" ); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | $this->length = $length; |
@@ -14,7 +14,7 @@ |
||
| 14 | 14 | * @param int $length |
| 15 | 15 | */ |
| 16 | 16 | public function __construct( $name, $length ) { |
| 17 | - parent::__construct($name); |
|
| 18 | - $this->setLength($length); |
|
| 17 | + parent::__construct( $name ); |
|
| 18 | + $this->setLength( $length ); |
|
| 19 | 19 | } |
| 20 | 20 | } |
@@ -47,14 +47,14 @@ discard block |
||
| 47 | 47 | * @param \donatj\MySqlSchema\Table $table |
| 48 | 48 | */ |
| 49 | 49 | public function addTable( Table $table ) { |
| 50 | - $this->tables[spl_object_hash($table)] = $table; |
|
| 50 | + $this->tables[ spl_object_hash( $table ) ] = $table; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | 54 | * @return \donatj\MySqlSchema\Table[] |
| 55 | 55 | */ |
| 56 | 56 | public function getTables() { |
| 57 | - return array_values($this->tables); |
|
| 57 | + return array_values( $this->tables ); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
@@ -107,54 +107,54 @@ discard block |
||
| 107 | 107 | $type = $this->getTypeName(); |
| 108 | 108 | |
| 109 | 109 | $nullable = ''; |
| 110 | - if( !$this->nullable ) { |
|
| 110 | + if ( !$this->nullable ) { |
|
| 111 | 111 | $nullable = ' NOT NULL'; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | $length = ''; |
| 115 | - if( $this instanceof RequiredLengthInterface || |
|
| 116 | - ($this instanceof OptionalLengthInterface && $this->getLength()) |
|
| 115 | + if ( $this instanceof RequiredLengthInterface || |
|
| 116 | + ( $this instanceof OptionalLengthInterface && $this->getLength() ) |
|
| 117 | 117 | ) { |
| 118 | - $l = intval($this->getLength()); |
|
| 118 | + $l = intval( $this->getLength() ); |
|
| 119 | 119 | $length = "($l)"; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | $signed = ''; |
| 123 | - if( $this instanceof SignedInterface ) { |
|
| 124 | - if( !$this->isSigned() ) { |
|
| 123 | + if ( $this instanceof SignedInterface ) { |
|
| 124 | + if ( !$this->isSigned() ) { |
|
| 125 | 125 | $signed = " unsigned"; |
| 126 | 126 | } |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | $default = ''; |
| 130 | - if( !is_null($this->default) ) { |
|
| 131 | - $default = ' DEFAULT ' . $this->mkString($this->default, "'");; |
|
| 130 | + if ( !is_null( $this->default ) ) { |
|
| 131 | + $default = ' DEFAULT '.$this->mkString( $this->default, "'" ); ; |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | $charset = ''; |
| 135 | 135 | $collation = ''; |
| 136 | - if( $this instanceof CharsetColumnInterface ) { |
|
| 137 | - if( $this->getCharset() ) { |
|
| 138 | - $charset = ' CHARACTER SET ' . $this->getCharset(); |
|
| 139 | - if( $this->getCollation() ) { |
|
| 140 | - $collation = ' COLLATE ' . $this->getCollation(); |
|
| 136 | + if ( $this instanceof CharsetColumnInterface ) { |
|
| 137 | + if ( $this->getCharset() ) { |
|
| 138 | + $charset = ' CHARACTER SET '.$this->getCharset(); |
|
| 139 | + if ( $this->getCollation() ) { |
|
| 140 | + $collation = ' COLLATE '.$this->getCollation(); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | $comment = ''; |
| 146 | - if( $this->comment ) { |
|
| 147 | - $comment = ' COMMENT ' . $this->mkString($this->comment, "'"); |
|
| 146 | + if ( $this->comment ) { |
|
| 147 | + $comment = ' COMMENT '.$this->mkString( $this->comment, "'" ); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | $autoIncrement = ''; |
| 151 | - if( $this instanceof AbstractIntegerColumn ) { |
|
| 152 | - if( $table->isAutoIncrement($this) ) { |
|
| 151 | + if ( $this instanceof AbstractIntegerColumn ) { |
|
| 152 | + if ( $table->isAutoIncrement( $this ) ) { |
|
| 153 | 153 | $autoIncrement = ' AUTO_INCREMENT'; |
| 154 | 154 | } |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | - $name = $this->mkString($this->name); |
|
| 157 | + $name = $this->mkString( $this->name ); |
|
| 158 | 158 | |
| 159 | 159 | return "{$name} {$type}{$length}{$signed}{$charset}{$collation}{$nullable}{$default}{$autoIncrement}{$comment}"; |
| 160 | 160 | } |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | public function addAutoIncrement( AbstractIntegerColumn $column ) { |
| 94 | 94 | $this->autoIncrement = $column; |
| 95 | 95 | |
| 96 | - $this->addPrimaryKey($column); |
|
| 96 | + $this->addPrimaryKey( $column ); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | /** |
@@ -110,9 +110,9 @@ discard block |
||
| 110 | 110 | protected $primaryKeys = [ ]; |
| 111 | 111 | |
| 112 | 112 | public function addPrimaryKey( AbstractColumn $column ) { |
| 113 | - $this->primaryKeys[spl_object_hash($column)] = $column; |
|
| 113 | + $this->primaryKeys[ spl_object_hash( $column ) ] = $column; |
|
| 114 | 114 | |
| 115 | - $this->addColumn($column); |
|
| 115 | + $this->addColumn( $column ); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /** |
@@ -120,33 +120,33 @@ discard block |
||
| 120 | 120 | * @return bool |
| 121 | 121 | */ |
| 122 | 122 | public function isPrimaryKey( AbstractColumn $column ) { |
| 123 | - return isset($this->primaryKeys[spl_object_hash($column)]); |
|
| 123 | + return isset( $this->primaryKeys[ spl_object_hash( $column ) ] ); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | protected $keys = [ ]; |
| 127 | 127 | |
| 128 | 128 | public function addKeyColumn( $keyName, AbstractColumn $column, $index = null, $type = 'NORMAL', $method = '' ) { |
| 129 | - if( !isset($this->keys[$keyName]) ) { |
|
| 130 | - $this->keys[$keyName] = [ |
|
| 129 | + if ( !isset( $this->keys[ $keyName ] ) ) { |
|
| 130 | + $this->keys[ $keyName ] = [ |
|
| 131 | 131 | 'columns' => [ ], |
| 132 | 132 | ]; |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - $this->keys[$keyName]['type'] = $type; |
|
| 136 | - $this->keys[$keyName]['method'] = $method; |
|
| 135 | + $this->keys[ $keyName ][ 'type' ] = $type; |
|
| 136 | + $this->keys[ $keyName ][ 'method' ] = $method; |
|
| 137 | 137 | |
| 138 | 138 | |
| 139 | - if( is_null($index) ) { |
|
| 140 | - $this->keys[$keyName]['columns'][] = $column; |
|
| 139 | + if ( is_null( $index ) ) { |
|
| 140 | + $this->keys[ $keyName ][ 'columns' ][ ] = $column; |
|
| 141 | 141 | } else { |
| 142 | - $this->keys[$keyName]['columns'][$index] = $column; |
|
| 142 | + $this->keys[ $keyName ][ 'columns' ][ $index ] = $column; |
|
| 143 | 143 | } |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | protected $foreignKeys = [ ]; |
| 147 | 147 | |
| 148 | 148 | public function addForeignKey( AbstractColumn $local, AbstractColumn $remote ) { |
| 149 | - $this->foreignKeys[spl_object_hash($local)] = [ |
|
| 149 | + $this->foreignKeys[ spl_object_hash( $local ) ] = [ |
|
| 150 | 150 | 'local' => $local, |
| 151 | 151 | 'remote' => $remote, |
| 152 | 152 | ]; |
@@ -158,97 +158,97 @@ discard block |
||
| 158 | 158 | protected $columns = [ ]; |
| 159 | 159 | |
| 160 | 160 | public function addColumn( AbstractColumn $column ) { |
| 161 | - $this->columns[spl_object_hash($column)] = $column; |
|
| 162 | - $column->addTable($this); |
|
| 161 | + $this->columns[ spl_object_hash( $column ) ] = $column; |
|
| 162 | + $column->addTable( $this ); |
|
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | public function toString() { |
| 166 | 166 | $warnings = [ ]; |
| 167 | 167 | $statements = [ ]; |
| 168 | - foreach( $this->columns as $column ) { |
|
| 169 | - $statements[] = "\t" . $column->toString($this); |
|
| 168 | + foreach ( $this->columns as $column ) { |
|
| 169 | + $statements[ ] = "\t".$column->toString( $this ); |
|
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - if( count($this->primaryKeys) > 0 ) { |
|
| 172 | + if ( count( $this->primaryKeys ) > 0 ) { |
|
| 173 | 173 | $primary = "\tPRIMARY KEY ("; |
| 174 | - $primary .= implode(",", array_map(function ( AbstractColumn $column ) { |
|
| 175 | - return $this->mkString($column->getName()); |
|
| 176 | - }, $this->primaryKeys)); |
|
| 174 | + $primary .= implode( ",", array_map( function ( AbstractColumn $column ) { |
|
| 175 | + return $this->mkString( $column->getName() ); |
|
| 176 | + }, $this->primaryKeys ) ); |
|
| 177 | 177 | $primary .= ")"; |
| 178 | - $statements[] = $primary; |
|
| 178 | + $statements[ ] = $primary; |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - if( !is_null($this->autoIncrement) ) { |
|
| 182 | - if( $this->autoIncrement->isSigned() ) { |
|
| 183 | - $warnings[] = $this->mkString($this->autoIncrement->getName()) . ' is a signed AUTO_INCREMENT'; |
|
| 181 | + if ( !is_null( $this->autoIncrement ) ) { |
|
| 182 | + if ( $this->autoIncrement->isSigned() ) { |
|
| 183 | + $warnings[ ] = $this->mkString( $this->autoIncrement->getName() ).' is a signed AUTO_INCREMENT'; |
|
| 184 | 184 | } |
| 185 | - if( $this->autoIncrement->isNullable() ) { |
|
| 186 | - $warnings[] = $this->mkString($this->autoIncrement->getName()) . ' is a nullable AUTO_INCREMENT'; |
|
| 185 | + if ( $this->autoIncrement->isNullable() ) { |
|
| 186 | + $warnings[ ] = $this->mkString( $this->autoIncrement->getName() ).' is a nullable AUTO_INCREMENT'; |
|
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - foreach( $this->keys as $keyName => $key ) { |
|
| 190 | + foreach ( $this->keys as $keyName => $key ) { |
|
| 191 | 191 | $keys = "\t"; |
| 192 | - if( $key['type'] != 'NORMAL' ) { |
|
| 193 | - $keys .= $key['type'] . ' '; |
|
| 192 | + if ( $key[ 'type' ] != 'NORMAL' ) { |
|
| 193 | + $keys .= $key[ 'type' ].' '; |
|
| 194 | 194 | } |
| 195 | - $keys .= "KEY " . $this->mkString($keyName) . " ("; |
|
| 196 | - $keys .= implode(",", array_map(function ( AbstractColumn $column ) { |
|
| 197 | - return $this->mkString($column->getName()); |
|
| 198 | - }, $key['columns'])); |
|
| 195 | + $keys .= "KEY ".$this->mkString( $keyName )." ("; |
|
| 196 | + $keys .= implode( ",", array_map( function ( AbstractColumn $column ) { |
|
| 197 | + return $this->mkString( $column->getName() ); |
|
| 198 | + }, $key[ 'columns' ] ) ); |
|
| 199 | 199 | $keys .= ")"; |
| 200 | - $statements[] = $keys; |
|
| 200 | + $statements[ ] = $keys; |
|
| 201 | 201 | } |
| 202 | 202 | |
| 203 | - foreach( $this->foreignKeys as $fks ) { |
|
| 203 | + foreach ( $this->foreignKeys as $fks ) { |
|
| 204 | 204 | /** |
| 205 | 205 | * @var $local AbstractColumn |
| 206 | 206 | * @var $remote AbstractColumn |
| 207 | 207 | */ |
| 208 | - $local = $fks['local']; |
|
| 209 | - $remote = $fks['remote']; |
|
| 208 | + $local = $fks[ 'local' ]; |
|
| 209 | + $remote = $fks[ 'remote' ]; |
|
| 210 | 210 | |
| 211 | 211 | $tables = $remote->getTables(); |
| 212 | 212 | // @todo doesn't really need to be a PK, just a key |
| 213 | - $tables = array_filter($tables, function ( Table $a ) use ( $remote ) { |
|
| 214 | - return $a->isPrimaryKey($remote); |
|
| 213 | + $tables = array_filter( $tables, function ( Table $a ) use ( $remote ) { |
|
| 214 | + return $a->isPrimaryKey( $remote ); |
|
| 215 | 215 | }); |
| 216 | 216 | |
| 217 | - foreach( $tables as $tbl ) { |
|
| 217 | + foreach ( $tables as $tbl ) { |
|
| 218 | 218 | // @todo check length and perhaps other stuff |
| 219 | - if( $local->getTypeName() != $remote->getTypeName() ) { |
|
| 220 | - $warnings[] = $this->mkString($this->autoIncrement->getName()) . ' type does not match defined foreign key type'; |
|
| 219 | + if ( $local->getTypeName() != $remote->getTypeName() ) { |
|
| 220 | + $warnings[ ] = $this->mkString( $this->autoIncrement->getName() ).' type does not match defined foreign key type'; |
|
| 221 | 221 | } |
| 222 | - $localName = $this->mkString($local->getName()); |
|
| 223 | - $remoteName = $this->mkString($remote->getName()); |
|
| 224 | - $remoteTblName = $this->mkString($tbl->getName()); |
|
| 222 | + $localName = $this->mkString( $local->getName() ); |
|
| 223 | + $remoteName = $this->mkString( $remote->getName() ); |
|
| 224 | + $remoteTblName = $this->mkString( $tbl->getName() ); |
|
| 225 | 225 | |
| 226 | 226 | $keys = "\tFOREIGN KEY ({$localName}) REFERENCES {$remoteTblName}({$remoteName})"; |
| 227 | - $statements[] = $keys; |
|
| 227 | + $statements[ ] = $keys; |
|
| 228 | 228 | } |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | $charset = ''; |
| 232 | 232 | $collation = ''; |
| 233 | - if( $this->getCharset() ) { |
|
| 234 | - $charset = ' CHARACTER SET ' . $this->getCharset(); |
|
| 235 | - if( $this->getCollation() ) { |
|
| 236 | - $collation = ' COLLATE ' . $this->getCollation(); |
|
| 233 | + if ( $this->getCharset() ) { |
|
| 234 | + $charset = ' CHARACTER SET '.$this->getCharset(); |
|
| 235 | + if ( $this->getCollation() ) { |
|
| 236 | + $collation = ' COLLATE '.$this->getCollation(); |
|
| 237 | 237 | } |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | |
| 241 | 241 | $comment = ''; |
| 242 | - if( $this->comment ) { |
|
| 243 | - $comment = ' COMMENT ' . $this->mkString($this->comment, "'"); |
|
| 242 | + if ( $this->comment ) { |
|
| 243 | + $comment = ' COMMENT '.$this->mkString( $this->comment, "'" ); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - $name = $this->mkString($this->name); |
|
| 247 | - $stmnts = implode(",\n", $statements); |
|
| 246 | + $name = $this->mkString( $this->name ); |
|
| 247 | + $stmnts = implode( ",\n", $statements ); |
|
| 248 | 248 | |
| 249 | 249 | $warn = ''; |
| 250 | - if( count($warnings) > 0 ) { |
|
| 251 | - $warn = "\n# Warning: " . implode("\n# Warning: ", $warnings); |
|
| 250 | + if ( count( $warnings ) > 0 ) { |
|
| 251 | + $warn = "\n# Warning: ".implode( "\n# Warning: ", $warnings ); |
|
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | return <<<EOT |
@@ -256,6 +256,6 @@ |
||
| 256 | 256 | {$stmnts} |
| 257 | 257 | ){$charset}{$collation}{$comment}{$warn}; |
| 258 | 258 | |
| 259 | -EOT; |
|
| 259 | +eot; |
|
| 260 | 260 | } |
| 261 | 261 | } |