Last active
December 22, 2015 20:19
-
-
Save JeroenDeDauw/6525656 to your computer and use it in GitHub Desktop.
Fields vs method arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleTableSchemaUpdater implements TableSchemaUpdater { | |
private $schemaModifier; | |
public function __construct( SchemaModifier $schemaModifier ) { | |
$this->schemaModifier = $schemaModifier; | |
} | |
/** | |
* @see TableSchemaUpdater::updateTable | |
* | |
* @param TableDefinition $currentTable | |
* @param TableDefinition $newTable | |
* | |
* @throws SchemaUpdateFailedException | |
*/ | |
public function updateTable( TableDefinition $currentTable, TableDefinition $newTable ) { | |
// TODO: assert same table | |
$this->removeRemovedFields( $currentTable, $newTable ); | |
$this->addAddedFields( $currentTable, $newTable ); | |
// TODO | |
} | |
private function removeRemovedFields( TableDefinition $currentTable, TableDefinition $newTable ) { | |
$removedFields = array_diff_key( $currentTable->getFields(), $newTable->getFields() ); | |
foreach ( $removedFields as $removedField ) { | |
$this->schemaModifier->removeField( $currentTable->getName(), $removedField->getName() ); | |
} | |
} | |
private function addAddedFields( TableDefinition $currentTable, TableDefinition $newTable ) { | |
$addedFields = array_diff_key( $newTable->getFields(), $currentTable->getFields() ); | |
foreach ( $addedFields as $addedField ) { | |
$this->schemaModifier->addField( $currentTable->getName(), $addedField ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleTableSchemaUpdater implements TableSchemaUpdater { | |
private $schemaModifier; | |
/** | |
* @var TableDefinition | |
*/ | |
private $currentTable; | |
/** | |
* @var TableDefinition | |
*/ | |
private $newTable; | |
public function __construct( SchemaModifier $schemaModifier ) { | |
$this->schemaModifier = $schemaModifier; | |
} | |
/** | |
* @see TableSchemaUpdater::updateTable | |
* | |
* @param TableDefinition $currentTable | |
* @param TableDefinition $newTable | |
* | |
* @throws SchemaUpdateFailedException | |
*/ | |
public function updateTable( TableDefinition $currentTable, TableDefinition $newTable ) { | |
// TODO: assert same table | |
$this->currentTable = $currentTable; | |
$this->newTable = $newTable; | |
$this->removeRemovedFields(); | |
$this->addAddedFields(); | |
// TODO | |
} | |
private function removeRemovedFields() { | |
$removedFields = array_diff_key( $this->currentTable->getFields(), $this->newTable->getFields() ); | |
foreach ( $removedFields as $removedField ) { | |
$this->schemaModifier->removeField( $this->currentTable->getName(), $removedField->getName() ); | |
} | |
} | |
private function addAddedFields() { | |
$addedFields = array_diff_key( $this->newTable->getFields(), $this->currentTable->getFields() ); | |
foreach ( $addedFields as $addedField ) { | |
$this->schemaModifier->addField( $this->currentTable->getName(), $addedField ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleTableSchemaUpdater implements TableSchemaUpdater { | |
private $schemaModifier; | |
/** | |
* @var FieldDefinition[] | |
*/ | |
private $addedFields; | |
/** | |
* @var FieldDefinition[] | |
*/ | |
private $removedFields; | |
/** | |
* @var string | |
*/ | |
private $tableName; | |
public function __construct( SchemaModifier $schemaModifier ) { | |
$this->schemaModifier = $schemaModifier; | |
} | |
/** | |
* @see TableSchemaUpdater::updateTable | |
* | |
* @param TableDefinition $currentTable | |
* @param TableDefinition $newTable | |
* | |
* @throws SchemaUpdateFailedException | |
*/ | |
public function updateTable( TableDefinition $currentTable, TableDefinition $newTable ) { | |
// TODO: assert same table | |
$this->tableName = $currentTable->getName(); | |
$this->addedFields = array_diff_key( | |
$newTable->getFields(), | |
$currentTable->getFields() | |
); | |
$this->removedFields = array_diff_key( | |
$currentTable->getFields(), | |
$newTable->getFields() | |
); | |
$this->removeRemovedFields(); | |
$this->addAddedFields(); | |
// TODO | |
} | |
private function removeRemovedFields() { | |
foreach ( $this->removedFields as $removedField ) { | |
$this->schemaModifier->removeField( $this->tableName, $removedField->getName() ); | |
} | |
} | |
private function addAddedFields() { | |
foreach ( $this->addedFields as $addedField ) { | |
$this->schemaModifier->addField( $this->tableName, $addedField ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleTableSchemaUpdater implements TableSchemaUpdater { | |
private $schemaModifier; | |
/** | |
* @var string | |
*/ | |
private $tableName; | |
public function __construct( SchemaModifier $schemaModifier ) { | |
$this->schemaModifier = $schemaModifier; | |
} | |
/** | |
* @see TableSchemaUpdater::updateTable | |
* | |
* @param TableDefinition $currentTable | |
* @param TableDefinition $newTable | |
* | |
* @throws SchemaUpdateFailedException | |
*/ | |
public function updateTable( TableDefinition $currentTable, TableDefinition $newTable ) { | |
// TODO: assert same table | |
$this->tableName = $currentTable->getName(); | |
$this->removeFields( | |
array_diff_key( | |
$currentTable->getFields(), | |
$newTable->getFields() | |
) | |
); | |
$this->addFields( | |
array_diff_key( | |
$newTable->getFields(), | |
$currentTable->getFields() | |
) | |
); | |
// TODO | |
} | |
/** | |
* @param FieldDefinition[] $fields | |
*/ | |
private function removeFields( array $fields ) { | |
foreach ( $fields as $field ) { | |
$this->schemaModifier->removeField( $this->tableName, $field->getName() ); | |
} | |
} | |
/** | |
* @param FieldDefinition[] $fields | |
*/ | |
private function addFields( array $fields ) { | |
foreach ( $fields as $field ) { | |
$this->schemaModifier->addField( $this->tableName, $field ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleTableSchemaUpdater implements TableSchemaUpdater { | |
private $schemaModifier; | |
public function __construct( SchemaModifier $schemaModifier ) { | |
$this->schemaModifier = $schemaModifier; | |
} | |
/** | |
* @see TableSchemaUpdater::updateTable | |
* | |
* @param TableDefinition $currentTable | |
* @param TableDefinition $newTable | |
* | |
* @throws SchemaUpdateFailedException | |
*/ | |
public function updateTable( TableDefinition $currentTable, TableDefinition $newTable ) { | |
// TODO: assert same table | |
$updater = new PrivateTableUpdate( $this->schemaModifier, $currentTable, $newTable ); | |
$updater->updateTable(); | |
} | |
} | |
class PrivateTableUpdate { | |
private $schemaModifier; | |
private $currentTable; | |
private $newTable; | |
public function __construct( SchemaModifier $schemaModifier, | |
TableDefinition $currentTable, TableDefinition $newTable ) { | |
$this->schemaModifier = $schemaModifier; | |
$this->currentTable = $currentTable; | |
$this->newTable = $newTable; | |
} | |
public function updateTable() { | |
$this->removeRemovedFields(); | |
$this->addAddedFields(); | |
// TODO | |
} | |
private function removeRemovedFields() { | |
$removedFields = array_diff_key( $this->currentTable->getFields(), $this->newTable->getFields() ); | |
foreach ( $removedFields as $removedField ) { | |
$this->schemaModifier->removeField( $this->currentTable->getName(), $removedField->getName() ); | |
} | |
} | |
private function addAddedFields() { | |
$addedFields = array_diff_key( $this->newTable->getFields(), $this->currentTable->getFields() ); | |
foreach ( $addedFields as $addedField ) { | |
$this->schemaModifier->addField( $this->currentTable->getName(), $addedField ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleTableSchemaUpdater implements TableSchemaUpdater { | |
private $schemaModifier; | |
public function __construct( SchemaModifier $schemaModifier ) { | |
$this->schemaModifier = $schemaModifier; | |
} | |
/** | |
* @see TableSchemaUpdater::updateTable | |
* | |
* @param TableDefinition $currentTable | |
* @param TableDefinition $newTable | |
* | |
* @throws SchemaUpdateFailedException | |
*/ | |
public function updateTable( TableDefinition $currentTable, TableDefinition $newTable ) { | |
// TODO: assert same table | |
$updater = new PrivateTableUpdate( $this->schemaModifier, $currentTable, $newTable ); | |
$updater->updateTable(); | |
} | |
} | |
class PrivateTableUpdate { | |
private $schemaModifier; | |
private $currentTable; | |
private $newTable; | |
public function __construct( SchemaModifier $schemaModifier, | |
TableDefinition $currentTable, TableDefinition $newTable ) { | |
$this->schemaModifier = $schemaModifier; | |
$this->currentTable = $currentTable; | |
$this->newTable = $newTable; | |
} | |
public function updateTable() { | |
$this->removeFields( | |
array_diff_key( | |
$this->currentTable->getFields(), | |
$this->newTable->getFields() | |
) | |
); | |
$this->addFields( | |
array_diff_key( | |
$this->newTable->getFields(), | |
$this->currentTable->getFields() | |
) | |
); | |
// TODO | |
} | |
/** | |
* @param FieldDefinition[] $fields | |
*/ | |
private function removeFields( array $fields ) { | |
foreach ( $fields as $field ) { | |
$this->schemaModifier->removeField( $this->currentTable->getName(), $field->getName() ); | |
} | |
} | |
/** | |
* @param FieldDefinition[] $fields | |
*/ | |
private function addFields( array $fields ) { | |
foreach ( $fields as $field ) { | |
$this->schemaModifier->addField( $this->currentTable->getName(), $field ); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SimpleTableSchemaUpdater implements TableSchemaUpdater { | |
private $schemaModifier; | |
public function __construct( SchemaModifier $schemaModifier ) { | |
$this->schemaModifier = $schemaModifier; | |
} | |
/** | |
* @see TableSchemaUpdater::updateTable | |
* | |
* @param TableDefinition $currentTable | |
* @param TableDefinition $newTable | |
* | |
* @throws SchemaUpdateFailedException | |
*/ | |
public function updateTable( TableDefinition $currentTable, TableDefinition $newTable ) { | |
// TODO: assert same table | |
$updater = new PrivateTableUpdate( $this->schemaModifier, $currentTable, $newTable ); | |
$updater->removeRemovedFields(); | |
$updater->addAddedFields(); | |
// TODO | |
} | |
} | |
class PrivateTableUpdate { | |
private $schemaModifier; | |
private $currentTable; | |
private $newTable; | |
public function __construct( SchemaModifier $schemaModifier, | |
TableDefinition $currentTable, TableDefinition $newTable ) { | |
$this->schemaModifier = $schemaModifier; | |
$this->currentTable = $currentTable; | |
$this->newTable = $newTable; | |
} | |
public function removeRemovedFields() { | |
$fields = array_diff_key( | |
$this->currentTable->getFields(), | |
$this->newTable->getFields() | |
); | |
foreach ( $fields as $field ) { | |
$this->schemaModifier->removeField( $this->currentTable->getName(), $field->getName() ); | |
} | |
} | |
public function addAddedFields() { | |
$fields = array_diff_key( | |
$this->newTable->getFields(), | |
$this->currentTable->getFields() | |
); | |
foreach ( $fields as $field ) { | |
$this->schemaModifier->addField( $this->currentTable->getName(), $field ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment