From 31c5574fd8c1164a5d6695b15c6f8236f2e6b8cb Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Sun, 31 Aug 2014 19:58:10 +0800 Subject: [PATCH] Commiting the big week of work. This commit includes some basic entites and a basic data mapping framework to load/save said entites from the database. --- .gitignore | 2 + DataAccess/DataMapper/DataMapper.php | 63 +++ .../Helpers/AbstractPopulationHelper.php | 434 +++++++++++++++++++++ DataAccess/DataMapper/Helpers/EntityMapsHelper.php | 77 ++++ DataAccess/DataMapper/Helpers/IntMapsHelper.php | 44 +++ .../DataMapper/Helpers/VOArrayMapsHelper.php | 79 ++++ DataAccess/DataMapper/Helpers/VOMapsHelper.php | 85 ++++ .../DataMapper/Helpers/VarcharMapsHelper.php | 44 +++ DataAccess/DataMapper/IDataMapper.php | 15 + DataAccess/StepMania/ISimfileRepository.php | 12 + DataAccess/functions.php | 35 ++ Domain/ConstantsAndTypes/SimfileConstants.php | 15 + Domain/Entities/AbstractEntity.php | 22 ++ Domain/Entities/IDivineEntity.php | 9 + Domain/Entities/IUser.php | 14 + Domain/Entities/IUserBuilder.php | 16 + Domain/Entities/StepMania/ISimfile.php | 18 + Domain/Entities/StepMania/ISimfileBuilder.php | 20 + Domain/Entities/StepMania/Simfile.php | 103 +++++ Domain/Entities/StepMania/SimfileBuilder.php | 89 +++++ Domain/Entities/StepMania/SimfileFactory.php | 50 +++ .../StepMania/SimfileStepByStepBuilder.php | 154 ++++++++ Domain/Entities/User.php | 50 +++ Domain/Entities/UserBuilder.php | 57 +++ Domain/Entities/UserFactory.php | 37 ++ Domain/Entities/UserStepByStepBuilder.php | 88 +++++ Domain/Exception/InvalidCountryException.php | 7 + Domain/Exception/InvalidDanceModeException.php | 7 + Domain/Exception/InvalidDifficultyException.php | 7 + Domain/Exception/InvalidStepChartException.php | 7 + Domain/VOs/Country.php | 273 +++++++++++++ Domain/VOs/ICountry.php | 9 + Domain/VOs/IName.php | 10 + Domain/VOs/Name.php | 29 ++ Domain/VOs/StepMania/Artist.php | 17 + Domain/VOs/StepMania/BPM.php | 25 ++ Domain/VOs/StepMania/DanceMode.php | 34 ++ Domain/VOs/StepMania/Difficulty.php | 38 ++ Domain/VOs/StepMania/IArtist.php | 9 + Domain/VOs/StepMania/IBPM.php | 9 + Domain/VOs/StepMania/IDanceMode.php | 9 + Domain/VOs/StepMania/IDifficulty.php | 9 + Domain/VOs/StepMania/IStepArtist.php | 8 + Domain/VOs/StepMania/IStepChart.php | 11 + Domain/VOs/StepMania/ITag.php | 8 + Domain/VOs/StepMania/StepArtist.php | 18 + Domain/VOs/StepMania/StepChart.php | 50 +++ Domain/VOs/StepMania/Tag.php | 20 + README.md | 2 +- Services/Bar.php | 25 ++ Services/BarInterface.php | 22 ++ Services/Foo.php | 42 ++ composer.json | 12 + config/DI.php | 5 + config/DataMaps.php | 114 ++++++ divinelegy.sql | 188 +++++++++ public_html/index.php | 70 ++++ 57 files changed, 2725 insertions(+), 1 deletion(-) create mode 100644 DataAccess/DataMapper/DataMapper.php create mode 100644 DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php create mode 100644 DataAccess/DataMapper/Helpers/EntityMapsHelper.php create mode 100644 DataAccess/DataMapper/Helpers/IntMapsHelper.php create mode 100644 DataAccess/DataMapper/Helpers/VOArrayMapsHelper.php create mode 100644 DataAccess/DataMapper/Helpers/VOMapsHelper.php create mode 100644 DataAccess/DataMapper/Helpers/VarcharMapsHelper.php create mode 100644 DataAccess/DataMapper/IDataMapper.php create mode 100644 DataAccess/StepMania/ISimfileRepository.php create mode 100644 DataAccess/functions.php create mode 100644 Domain/ConstantsAndTypes/SimfileConstants.php create mode 100644 Domain/Entities/AbstractEntity.php create mode 100644 Domain/Entities/IDivineEntity.php create mode 100644 Domain/Entities/IUser.php create mode 100644 Domain/Entities/IUserBuilder.php create mode 100644 Domain/Entities/StepMania/ISimfile.php create mode 100644 Domain/Entities/StepMania/ISimfileBuilder.php create mode 100644 Domain/Entities/StepMania/Simfile.php create mode 100644 Domain/Entities/StepMania/SimfileBuilder.php create mode 100644 Domain/Entities/StepMania/SimfileFactory.php create mode 100644 Domain/Entities/StepMania/SimfileStepByStepBuilder.php create mode 100644 Domain/Entities/User.php create mode 100644 Domain/Entities/UserBuilder.php create mode 100644 Domain/Entities/UserFactory.php create mode 100644 Domain/Entities/UserStepByStepBuilder.php create mode 100644 Domain/Exception/InvalidCountryException.php create mode 100644 Domain/Exception/InvalidDanceModeException.php create mode 100644 Domain/Exception/InvalidDifficultyException.php create mode 100644 Domain/Exception/InvalidStepChartException.php create mode 100644 Domain/VOs/Country.php create mode 100644 Domain/VOs/ICountry.php create mode 100644 Domain/VOs/IName.php create mode 100644 Domain/VOs/Name.php create mode 100644 Domain/VOs/StepMania/Artist.php create mode 100644 Domain/VOs/StepMania/BPM.php create mode 100644 Domain/VOs/StepMania/DanceMode.php create mode 100644 Domain/VOs/StepMania/Difficulty.php create mode 100644 Domain/VOs/StepMania/IArtist.php create mode 100644 Domain/VOs/StepMania/IBPM.php create mode 100644 Domain/VOs/StepMania/IDanceMode.php create mode 100644 Domain/VOs/StepMania/IDifficulty.php create mode 100644 Domain/VOs/StepMania/IStepArtist.php create mode 100644 Domain/VOs/StepMania/IStepChart.php create mode 100644 Domain/VOs/StepMania/ITag.php create mode 100644 Domain/VOs/StepMania/StepArtist.php create mode 100644 Domain/VOs/StepMania/StepChart.php create mode 100644 Domain/VOs/StepMania/Tag.php create mode 100644 Services/Bar.php create mode 100644 Services/BarInterface.php create mode 100644 Services/Foo.php create mode 100644 composer.json create mode 100644 config/DI.php create mode 100644 config/DataMaps.php create mode 100644 divinelegy.sql create mode 100644 public_html/index.php diff --git a/.gitignore b/.gitignore index 5a8b920..7f3b1d0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ nbproject logs +composer.lock +vendor \ No newline at end of file diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php new file mode 100644 index 0000000..07b4011 --- /dev/null +++ b/DataAccess/DataMapper/DataMapper.php @@ -0,0 +1,63 @@ + false, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); + + $this->_db = new PDO($dsn, $username, $password, $options); + $this->_maps = include $maps; + } + + public function find($id, $entity) + { + $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id=%u', + $this->_maps[$entity]['table'], + $id)); + $statement->execute(); + $row = $statement->fetch(); + + $className = $this->_maps[$entity]['class']; //the entity to instantiate and return + $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db); + + if(count($constructors) == 0) + { + $class = new $className; + } else { + $r = new ReflectionClass($className); + $class = $r->newInstanceArgs($constructors); + } + + $class->setId($row['id']); + return $class; + } + + public function save(IDivineEntity $entity) + { + $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db); + echo '
';
+        print_r($queries);
+        echo '
'; + } + + public function remove(IDivineEntity $entity) { + ; + } +} diff --git a/DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php b/DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php new file mode 100644 index 0000000..6ea6490 --- /dev/null +++ b/DataAccess/DataMapper/Helpers/AbstractPopulationHelper.php @@ -0,0 +1,434 @@ + $mapsHelper) + { + switch(get_class($mapsHelper)) + { + case 'DataAccess\DataMapper\Helpers\IntMapsHelper': + case 'DataAccess\DataMapper\Helpers\VarcharMapsHelper': + $constructors[$constructor] = $row[$mapsHelper->getColumnName()]; + break; + case 'DataAccess\DataMapper\Helpers\VOMapsHelper': + case 'DataAccess\DataMapper\Helpers\VOArrayMapsHelper': + case 'DataAccess\DataMapper\Helpers\EntityMapsHelper': + $constructors[$constructor] = $mapsHelper->populate($maps, $db, $entity, $row); + break; + } + } + + return $constructors; + } + + static function generateUpdateSaveQuery($maps, $entity, $id, $db, &$queries = array(), $extraColumns = array()) + { + $entityMapsIndex = self::getMapsNameFromEntityObject($entity, $maps); + + if($id) + { + $query = sprintf('update %s set ', $maps[$entityMapsIndex]['table']); + } else { + $queryColumnNamesAndValues = array(); + } + + foreach($maps[$entityMapsIndex]['maps'] as $mapsHelper) + { + $accessor = $mapsHelper->getAccessor(); + $property = $entity->{$accessor}(); + + switch(get_class($mapsHelper)) + { + case 'DataAccess\DataMapper\Helpers\VOMapsHelper': + //we have a vo. Determine which way the reference is + $voMapsIndex = self::getMapsNameFromEntityObject($property, $maps); + $refDir = self::getReferenceDirection( + $maps[$entityMapsIndex]['table'], + $maps[$voMapsIndex]['table'], + $entityMapsIndex, + $mapsHelper->getTableName(), + $db); + + switch($refDir) + { + // our table stores their ID, all we do is update + // our reference. + case self::REFERENCE_FORWARD: + $voTableId = self::findVOInDB($maps, $property, $db); + + if($id) + { + $query .= sprintf('%s=%u, ', + strtolower($mapsHelper->getVOName() . '_id'), + $voTableId); + } else { + // we have a forward reference to a value object. + // see if it exists first: + if($voTableId) + { + $queryColumnNamesAndValues[strtolower($mapsHelper->getTableName() . '_id')] = $voTableId; + } else { + //make a note that this field will need the id from another + self::generateUpdateSaveQuery($maps, $property, NULL, $db, $queries); + $queryColumnNamesAndValues[strtolower($mapsHelper->getTableName() . '_id')] = '%INDEX_REF_' . (count($queries)-1) . '%'; + } + } + + break; + case self::REFERENCE_SELF: + //no need to find ids, but we need the + //column names + $columns = self::resolveColumnNamesAndValues($maps, $property); + foreach($columns as $columnName=>$columnValue) + { + if($id) + { + //TODO: logic to determine what the value is? i.e., string, int etc? + $query .= sprintf('%s="%s, "', + $columnName, + $columnValue + ); + } else { + $queryColumnNamesAndValues[$columnName] = $columnValue; + } + } + + break; + case self::REFERENCE_BACK: + echo 'bleh'; + + $voId = self::findVOInDB($maps, + $property, + $db, + array(strtolower($entityMapsIndex . '_id') => $id)); + if($voId) + { + self::generateUpdateSaveQuery($maps, $property, $voId, $db, $queries); + } else { + $extra = array(strtolower($entityMapsIndex . '_id') => '%MAIN_QUERY_ID%'); + self::generateUpdateSaveQuery($maps, $property, NULL, $db, $queries, $extra); + } + break; + } + + break; + + // We should never update referenced entities, the db + // should always store an ID as a reference to them. + // + // In the case where we cannot find the entity in the database, + // throw an exception ? + case 'DataAccess\DataMapper\Helpers\EntityMapsHelper': + $subEntityMapsIndex = self::getMapsNameFromEntityObject($property, $maps); + $refDir = self::getReferenceDirection( + $maps[$entityMapsIndex]['table'], + $maps[$subEntityMapsIndex]['table'], + $entityMapsIndex, + $mapsHelper->getTableName(), + $db); + + switch($refDir) + { + // our table stores their ID, all we do is update + // our reference. + case self::REFERENCE_FORWARD: + if($property->getId()) + { + // we exist in db already, update our reference + if($id) + { + $query .= sprintf('%s=%u, ', + strtolower($mapsHelper->getEntityName() . '_id'), + $property->getId()); + } else { + //not in db yet. make new ref + $queryColumnNamesAndValues[strtolower($mapsHelper->getEntityName() . '_id')] = $property->getId(); + } + } else { + // The entity we care about references an entity that + // has not yet been saved. + // + // TODO: Should we _try_ to save it? Or should + // it be enforced that entites already exist in the db? + // In the case of something like referencing a user entity, + // then for sure the user should already be saved because + // it makes no sense to assign a user at the time of simfile + // upload, they should have already completed the process. + // but could there be other entities where it does make sense + // for them to be created at the time of something else ? + throw new Exception(sprintf( + 'Could not find referenced entity, %s, in the database. Has it been saved yet?', + $mapsHelper->getEntityName())); + } + break; + } + break; + case 'DataAccess\DataMapper\Helpers\IntMapsHelper': + if($id) + { + //easy case, plain values in our table. + $query .= sprintf('%s=%u, ', + $mapsHelper->getColumnName(), + $property); + } else { + if(is_bool($property)) + { + $property = ($property) ? '1' : '0'; + } + $queryColumnNamesAndValues[$mapsHelper->getColumnName()] = sprintf('%u', $property); + } + break; + case 'DataAccess\DataMapper\Helpers\VarcharMapsHelper': + if($id){ + //easy case, plain values in our table. + $query .= sprintf('%s="%s", ', + $mapsHelper->getColumnName(), + $property); + } else { + $queryColumnNamesAndValues[$mapsHelper->getColumnName()] = sprintf('"%s"', $property); + } + + break; + + // I am making a bit of an assumption here. In my mind it only + // makes sense for an array of VOs to be stored in a different + // table in the DB since the main row can't possibly store + // different objects. + // + // in that regard, the way this works is that mapVOArrayToIds + // simply queries the DB and returns the VO ids in order then + // I assume that the object also has them in the same order + // (which it will if it is pulled out by this mapper. + // + // in the case of setting up a new entity, the VOs should never + // exist in the first place, so we just make them. + case 'DataAccess\DataMapper\Helpers\VOArrayMapsHelper': + if($id) + { + // If we assume that all elements in the array are the same then + // we can just use the first one to figure out which maps entry to use + $subEntityMapsIndex = self::getMapsNameFromEntityObject($property[0], $maps); + $voIds = self::mapVOArrayToIds($maps[$subEntityMapsIndex]['table'], + array(strtolower($entityMapsIndex . '_id'), $id), + $db); + + foreach($property as $index => $propertyArrayElement) + { + self::generateUpdateSaveQuery($maps, $propertyArrayElement, $voIds[$index], $db, $queries); + } + + break; + } else { + foreach($property as $propertyArrayElement) + { + // TODO: TRICKY! Since this is a back-reference, it + // needs the ID of the object we're trying to save + // to complete + $extra = array(strtolower($entityMapsIndex . '_id') => '%MAIN_QUERY_ID%'); + self::generateUpdateSaveQuery($maps, $propertyArrayElement, NULL, $db, $queries, $extra); + } + } + } + } + + if($id) + { + $query = substr($query, 0, -2); + $query .= sprintf(' WHERE id=%u', $id); + } else { + $queryColumnNamesAndValues = array_merge($queryColumnNamesAndValues, $extraColumns); + $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', + $maps[$entityMapsIndex]['table'], + implode(', ', array_keys($queryColumnNamesAndValues)), + implode(', ', $queryColumnNamesAndValues)); + } + + $queries[] = $query; + return $queries; + } + + static private function getMapsNameFromEntityObject($entity, $maps) + { + //todo maybe check that $entity is vo or entity + + $classname = get_class($entity); + foreach ($maps as $entityName => $map) + { + if($map['class'] == $classname) + { + return $entityName; + } + } + } + + static private function getReferenceDirection($tableA, $tableB, $nameA, $nameB, $db) + { + //TODO: check if tables are the same and return a constant for that + //echo '!!! ' . $tableA . ' needs ' . $nameB . ' : ' . $tableB . ' needs ' . $nameA . ' !!!
'; + + if($tableA === $tableB) + { + return self::REFERENCE_SELF; + } + + // first look in table A for a reference to B + $statement = $db->prepare(sprintf( + 'SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`="divinelegy" AND `TABLE_NAME`="%s"', + $tableA)); + + $statement->execute(); + $rows = $statement->fetchAll(); + + //print_r($rows); + + foreach($rows as $row) + { + if($row['COLUMN_NAME'] == strtolower($nameB . '_id')) + { + return self::REFERENCE_FORWARD; + } + } + + // now look in table b for a reference to a + $statement = $db->prepare(sprintf( + 'SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`="divinelegy" AND `TABLE_NAME`="%s"', + $tableB)); + + $statement->execute(); + $rows = $statement->fetchAll(); + + foreach($rows as $row) + { + if($row['COLUMN_NAME'] == strtolower($nameA . '_id')) + { + return self::REFERENCE_BACK; + } + } + } + + // can use this when we reference a VO + static public function findVOInDB($maps, $VO, $db, $extraColumns = array()) + { + $mapsIndex = self::getMapsNameFromEntityObject($VO, $maps); + $table = $maps[$mapsIndex]['table']; + + $columns = array_merge(self::resolveColumnNamesAndValues($maps, $VO), $extraColumns); + + $query = "SELECT * FROM $table where "; + + foreach($columns as $columnName => $columnValue) + { + $query .= sprintf('%s="%s" AND ', $columnName, $columnValue); + } + + $query = substr($query, 0, -4); + $statement = $db->prepare($query); + $statement->execute(); + $row = $statement->fetch(); + + return $row['id']; + } + + // this will figure out what columns belong to an entity and + // map the column names to the current entity values + static public function resolveColumnNamesAndValues($maps, $entity, $originalTable = null, &$columnNamesAndValues = array()) + { + $mapsIndex = self::getMapsNameFromEntityObject($entity, $maps); + + // This is the name of the table that the current object + // we are looking at belongs to. We need to compare this + // to original table to decide what to do. + $currentTable = $maps[$mapsIndex]['table']; + + if(!$originalTable) + { + // this will be the table that the VO we care about is stored in + // we check all future values to make sure they belong to this table. + // on the first pass we pull it out, and then on subsequent passes + // it should come in through the function call. + $originalTable = $currentTable; + } + + foreach($maps[$mapsIndex]['maps'] as $mapsHelper) + { + switch(get_class($mapsHelper)) + { + case 'DataAccess\DataMapper\Helpers\VOMapsHelper': + $accessor = $mapsHelper->getAccessor(); + $VO = $entity->{$accessor}(); + self::resolveColumnNamesAndValues($maps, $VO, $originalTable, $columnNamesAndValues); + break; + case 'DataAccess\DataMapper\Helpers\VarcharMapsHelper': + case 'DataAccess\DataMapper\Helpers\IntMapsHelper': + //is plain value. + + if($currentTable == $originalTable) + { + //It also keeps values in our table. Saving. + $accessor = $mapsHelper->getAccessor(); + $value = $entity->{$accessor}(); + + $columnNamesAndValues[$mapsHelper->getColumnName()] = $value; + } else { + //It does not store values in our table + //TODO: Should I try check if our table references the id of the record in the other table? + } + break; + } + } + + return $columnNamesAndValues; + } + + // When we have VO arrays, it should be the case that there is another + // table that references us. If we assume entries are in the db in the + // same order they are in the array, it should be possible to map them up. + // This way we can update existing VO entries instead of deleting and + // making new ones. And if there are VOs where we can't get an ID, that + // means we have to make a new one. + // + // Assumption: Array contains entries of all the same type + static public function mapVOArrayToIds($voTable, $columnToMatch, $db) + { + $query = sprintf('SELECT id from %s WHERE %s=%u', + $voTable, + $columnToMatch[0], + $columnToMatch[1]); + + $statement = $db->prepare($query); + $statement->execute(); + $rows = $statement->fetchAll(); + + $map = array(); + + foreach($rows as $row) + { + $map[] = $row['id']; + } + + return $map; + } +} + +//go off and resolve all column names for this table recursively +//do a check to make sure the VO we are investigating is in the same table +// -if it is: good, record its column name and value +// -if it isn't, we need to find this next VO in the db and record its ID as our column value +// -assuming a forward reference (IE our table has a column called voname_id. +// -if it doesn't, then we don't have to worry about it + diff --git a/DataAccess/DataMapper/Helpers/EntityMapsHelper.php b/DataAccess/DataMapper/Helpers/EntityMapsHelper.php new file mode 100644 index 0000000..fe45000 --- /dev/null +++ b/DataAccess/DataMapper/Helpers/EntityMapsHelper.php @@ -0,0 +1,77 @@ +_entityName = $entityName; + + if($tableName) { + $this->_tableName = $tableName; + } else { + $this->_tableName = strtolower($entityName); + } + + if($accessor) + { + $this->_accessor = $accessor; + } else + { + $this->_accessor = 'get'. str_replace('_', '', $entityName); + } + } + + public function getEntityName() + { + return $this->_entityName; + } + + public function getAccessor() + { + return $this->_accessor; + } + + public function getTableName() + { + return $this->_tableName; + } + + public function populate($maps, $db, $parent, $row) + { + $className = $maps[$this->_entityName]['class']; + $table = $maps[$this->_entityName]['table']; + + // If the table we already have contains the id of a row we need in + // another table + if(isset($row[$this->_tableName . '_id'])) { + $join_id = $row[$this->_tableName . '_id']; + $statement = $db->prepare(sprintf('SELECT * from %s WHERE id=%u', + $table, + $join_id)); + $statement->execute(); + $row = $statement->fetch(); + } + + $constructors = AbstractPopulationHelper::getConstrutorArray($maps, $this->_entityName, $row, $db); + + if(count($constructors) == 0) + { + $class = new $className; + } else { + $r = new ReflectionClass($className); + $class = $r->newInstanceArgs($constructors); + } + + $class->setId($row['id']); + return $class; + } +} + diff --git a/DataAccess/DataMapper/Helpers/IntMapsHelper.php b/DataAccess/DataMapper/Helpers/IntMapsHelper.php new file mode 100644 index 0000000..77ea64a --- /dev/null +++ b/DataAccess/DataMapper/Helpers/IntMapsHelper.php @@ -0,0 +1,44 @@ +_mapIndex = $mapIndex; + + if($tableName) { + $this->_tableName = $tableName; + } else { + $this->_tableName = strtolower($mapIndex); + } + + if($accessor) + { + $this->_accessor = $accessor; + } else + { + $this->_accessor = 'get'. str_replace('_', '', $mapIndex); + } + } + + public function getAccessor() + { + return $this->_accessor; + } + + public function getTableName() + { + return $this->_tableName; + } + + public function getColumnName() + { + return $this->_mapIndex; + } +} \ No newline at end of file diff --git a/DataAccess/DataMapper/Helpers/VOArrayMapsHelper.php b/DataAccess/DataMapper/Helpers/VOArrayMapsHelper.php new file mode 100644 index 0000000..d298e98 --- /dev/null +++ b/DataAccess/DataMapper/Helpers/VOArrayMapsHelper.php @@ -0,0 +1,79 @@ +_voName = $voName; + + if($tableName) { + $this->_tableName = $tableName; + } else { + $this->_tableName = strtolower($voName); + } + + if($accessor) + { + $this->_accessor = $accessor; + } else + { + $this->_accessor = 'get'. str_replace('_', '', $voName); + } + } + + public function getAccessor() + { + return $this->_accessor; + } + + public function getVOName() + { + return $this->_voName; + } + + public function getTableName() + { + return $this->_tableName; + } + + public function populate($maps, $db, $parent, $row) + { + $className = $maps[$this->_voName]['class']; + $table = $maps[$this->_voName]['table']; + $VOArray = array(); + + // in this case we look in another table for this row's id + $join_id = $row['id']; + $statement = $db->prepare(sprintf('SELECT * from %s WHERE %s=%u', + $table, + strtolower($parent . '_id'), + $join_id + )); + + $statement->execute(); + $rows = $statement->fetchAll(); + + foreach($rows as $row) + { + $constructors = AbstractPopulationHelper::getConstrutorArray($maps, $this->_voName, $row, $db); + + if(count($constructors) == 0) + { + $VOArray[] = new $className; + } else { + $r = new ReflectionClass($className); + $VOArray[] = $r->newInstanceArgs($constructors); + } + } + + return $VOArray; + } +} \ No newline at end of file diff --git a/DataAccess/DataMapper/Helpers/VOMapsHelper.php b/DataAccess/DataMapper/Helpers/VOMapsHelper.php new file mode 100644 index 0000000..497d5fe --- /dev/null +++ b/DataAccess/DataMapper/Helpers/VOMapsHelper.php @@ -0,0 +1,85 @@ +_voName = $voName; + + if($tableName) { + $this->_tableName = $tableName; + } else { + $this->_tableName = strtolower($voName); + } + + if($accessor) + { + $this->_accessor = $accessor; + } else + { + $this->_accessor = 'get'. str_replace('_', '', $voName); + } + } + + public function getAccessor() + { + return $this->_accessor; + } + + public function getVOName() + { + return $this->_voName; + } + + public function getTableName() + { + return $this->_tableName; + } + + public function populate($maps, $db, $parent, $row) + { + $className = $maps[$this->_voName]['class']; + $table = $maps[$this->_voName]['table']; + + // If the table we already have contains the id of a row we need in + // another table + if(isset($row[$this->_tableName . '_id'])) { + $join_id = $row[$this->_tableName . '_id']; + $statement = $db->prepare(sprintf('SELECT * from %s WHERE id=%u', + $table, + $join_id)); + + $statement->execute(); + $row = $statement->fetch(); + } elseif($maps[$this->_voName]['table'] != $maps[$parent]['table']) { + // in this case we look in another table for this row's id + $join_id = $row['id']; + $statement = $db->prepare(sprintf('SELECT * from %s WHERE %s=%u', + $table, + strtolower($parent . '_id'), + $join_id + )); + + $statement->execute(); + $row = $statement->fetch(); + } + + $constructors = AbstractPopulationHelper::getConstrutorArray($maps, $this->_voName, $row, $db); + + if(count($constructors) == 0) + { + return new $className; + } else { + $r = new ReflectionClass($className); + return $r->newInstanceArgs($constructors); + } + } +} diff --git a/DataAccess/DataMapper/Helpers/VarcharMapsHelper.php b/DataAccess/DataMapper/Helpers/VarcharMapsHelper.php new file mode 100644 index 0000000..70bc7f3 --- /dev/null +++ b/DataAccess/DataMapper/Helpers/VarcharMapsHelper.php @@ -0,0 +1,44 @@ +_mapIndex = $mapIndex; + + if($tableName) { + $this->_tableName = $tableName; + } else { + $this->_tableName = strtolower($mapIndex); + } + + if($accessor) + { + $this->_accessor = $accessor; + } else + { + $this->_accessor = 'get'. str_replace('_', '', $mapIndex); + } + } + + public function getTableName() + { + return $this->_tableName; + } + + public function getColumnName() + { + return $this->_mapIndex; + } + + public function getAccessor() + { + return $this->_accessor; + } +} \ No newline at end of file diff --git a/DataAccess/DataMapper/IDataMapper.php b/DataAccess/DataMapper/IDataMapper.php new file mode 100644 index 0000000..3e61b8f --- /dev/null +++ b/DataAccess/DataMapper/IDataMapper.php @@ -0,0 +1,15 @@ +id)) { + // throw new Exception('ID already set.'); + } + + $this->id = $id; + } + + public function getId() { + return $this->id; + } +} diff --git a/Domain/Entities/IDivineEntity.php b/Domain/Entities/IDivineEntity.php new file mode 100644 index 0000000..1b8d356 --- /dev/null +++ b/Domain/Entities/IDivineEntity.php @@ -0,0 +1,9 @@ +_title = $title; + $this->_artist = $artist; + $this->_uploader = $uploader; + $this->_bpm = $bpm; + $this->_bpmChanges = $bpmChanges; + $this->_stops = $stops; + $this->_fgChanges = $fgChanges; + $this->_bgChanges = $bgChanges; + + foreach($steps as $stepChart) { + if(!$stepChart instanceof IStepChart) { + throw new InvalidStepChartException(sprintf('Invalid StepChart array. All array elements must be an instance of Stepchart.')); + } + } + + $this->_steps = $steps; + } + + public function getTitle() + { + return $this->_title; + } + + public function getArtist() + { + return $this->_artist; + } + + public function getUploader() + { + return $this->_uploader; + } + + public function getBPM() + { + return $this->_bpm; + } + + public function hasBPMChanges() + { + return $this->_bpmChanges; + } + + public function hasStops() + { + return $this->_stops; + } + + public function hasFgChanges() + { + return $this->_fgChanges; + } + + public function hasBgChanges() + { + return $this->_bgChanges; + } + + public function addStepChart(StepChart $stepChart) { + $this->_steps[] = $stepChart; + } + + public function getSteps() + { + return $this->_steps; + } +} diff --git a/Domain/Entities/StepMania/SimfileBuilder.php b/Domain/Entities/StepMania/SimfileBuilder.php new file mode 100644 index 0000000..034fc8d --- /dev/null +++ b/Domain/Entities/StepMania/SimfileBuilder.php @@ -0,0 +1,89 @@ +_simfileFactory = $simfileFactory; + } + + public function With_Title($title) + { + $this->_title = $title; + return $this; + } + + public function With_Artist(IArtist $artist) { + $this->_artist = $artist; + return $this; + } + + public function With_Uploader(IUser $uploader) { + $this->_uploader = $uploader; + return $this; + } + + public function With_BPM(IBPM $bpm) { + $this->_bpm = $bpm; + return $this; + } + + public function With_BpmChanges($const) { + $this->_bpmChanges = $const; + return $this; + } + + public function With_Stops($const) { + $this->_stops = $const; + return $this; + } + + public function With_FgChanges($const) { + $this->_fgChanges = $const; + return $this; + } + + public function With_BgChanges($const) { + $this->_bgChanges = $const; + return $this; + } + + public function With_Steps(array $steps) { + $this->_steps = $steps; + return $this; + } + + public function build() { + return $this->_simfileFactory + ->createInstance($this->_title, + $this->_artist, + $this->_uploader, + $this->_bpm, + $this->_bpmChanges, + $this->_stops, + $this->_fgChanges, + $this->_bgChanges, + $this->_steps); + } +} + diff --git a/Domain/Entities/StepMania/SimfileFactory.php b/Domain/Entities/StepMania/SimfileFactory.php new file mode 100644 index 0000000..a784562 --- /dev/null +++ b/Domain/Entities/StepMania/SimfileFactory.php @@ -0,0 +1,50 @@ +_simfileBuilder = $builder; + } +} + + +class SimfileStepByStepBuilder extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder +{ + public function With_Title($title) { + $this->_simfileBuilder->With_Title($title); + return new SimfileStepByStepBuilder_With_Title($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_Title extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_Title +{ + public function With_Artist(IArtist $artist) + { + $this->_simfileBuilder->With_Artist($artist); + return new SimfileStepByStepBuilder_With_Artist($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_Artist extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_Artist +{ + public function With_Uploader(IUser $uploader) + { + $this->_simfileBuilder->With_Uploader($uploader); + return new SimfileStepByStepBuilder_With_Uploader($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_Uploader extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_Uploader +{ + public function With_BPM(IBPM $bpm) { + $this->_simfileBuilder->With_BPM($bpm); + return new SimfileStepByStepBuilder_With_BPM($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_BPM extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_BPM +{ + public function With_BpmChanges($const) { + $this->_simfileBuilder->With_BpmChanges($const); + return new SimfileStepByStepBuilder_With_BpmChanges($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_BpmChanges extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_BpmChanges +{ + public function With_Stops($const) { + $this->_simfileBuilder->With_Stops($const); + return new SimfileStepByStepBuilder_With_Stops($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_Stops extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_Stops +{ + public function With_FgChanges($const) { + $this->_simfileBuilder->With_FgChanges($const); + return new SimfileStepByStepBuilder_With_FgChanges($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_FgChanges extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_FgChanges +{ + public function With_BgChanges($const) { + $this->_simfileBuilder->With_BgChanges($const); + return new SimfileStepByStepBuilder_With_BgChanges($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_BgChanges extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_BgChanges +{ + public function With_Steps(array $steps) { + $this->_simfileBuilder->With_Steps($steps); + return new SimfileStepByStepBuilder_With_Steps($this->_simfileBuilder); + } +} + +class SimfileStepByStepBuilder_With_Steps extends AbstractSimfileStepByStepBuilder implements ISimfileStepByStepBuilder_With_Steps +{ + public function build() { + return $this->_simfileBuilder + ->build(); + } +} \ No newline at end of file diff --git a/Domain/Entities/User.php b/Domain/Entities/User.php new file mode 100644 index 0000000..1628017 --- /dev/null +++ b/Domain/Entities/User.php @@ -0,0 +1,50 @@ +_country = $country; + $this->_displayName = $displayName; + $this->_name = $name; + $this->_tags = $tags; + } + + public function getCountry() { + return $this->_country; + } + + public function getDisplayName() { + return $this->_displayName; + } + + public function getName() { + return $this->_name; + } + + public function getTags() { + return $this->_tags; + } + + public function getYearsStepArtist() { + return $this->_yearsStepArtist; + } +} diff --git a/Domain/Entities/UserBuilder.php b/Domain/Entities/UserBuilder.php new file mode 100644 index 0000000..44608f3 --- /dev/null +++ b/Domain/Entities/UserBuilder.php @@ -0,0 +1,57 @@ +_userFactory = $userFactory; + } + + public function With_Country(ICountry $country) { + $this->_country = $country; + return $this; + } + + public function With_DisplayName($name) { + $this->_displayName = $name; + return $this; + } + + public function With_Name(IName $name) { + $this->_name = $name; + return $this; + } + + public function With_Tags(array $tags) { + $this->_tags = $tags; + return $this; + } + + public function With_YearsStepArtist($years) { + $this->_yearsStepArtist = $years; + return $this; + } + + public function build() { + return $this->_userFactory + ->createInstance($this->_country, + $this->_displayName, + $this->_name, + $this->_tags, + $this->_yearsStepArtist); + } +} \ No newline at end of file diff --git a/Domain/Entities/UserFactory.php b/Domain/Entities/UserFactory.php new file mode 100644 index 0000000..a9a5402 --- /dev/null +++ b/Domain/Entities/UserFactory.php @@ -0,0 +1,37 @@ +_userBuilder = $builder; + } +} + +class UserStepByStepBuilder extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder +{ + public function With_Country(ICountry $country) { + $this->_userBuilder->With_Country($country); + return new UserStepByStepBuilder_With_Country($this->_userBuilder); + } +} + +class UserStepByStepBuilder_With_Country extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_Country +{ + public function With_DisplayName($name) { + $this->_userBuilder->With_DisplayName($name); + return new UserStepByStepBuilder_With_DisplayName($this->_userBuilder); + } +} + +class UserStepByStepBuilder_With_DisplayName extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_DisplayName +{ + public function With_Name(IName $name) { + $this->_userBuilder->With_Name($name); + return new UserStepByStepBuilder_With_Name($this->_userBuilder); + } +} + +class UserStepByStepBuilder_With_Name extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_Name +{ + public function With_Tags(array $tags) { + $this->_userBuilder->With_Tags($tags); + return new UserStepByStepBuilder_With_Tags($this->_userBuilder); + } +} + +class UserStepByStepBuilder_With_Tags extends AbstractUserStepByStepBuilder implements IUserStepByStepBuilder_With_Tags +{ + public function With_YearsStepArtist($years) { + $this->_userBuilder->With_YearsStepArtist($years); + return $this; + } + + public function build() { + return $this->_userBuilder + ->build(); + } +} \ No newline at end of file diff --git a/Domain/Exception/InvalidCountryException.php b/Domain/Exception/InvalidCountryException.php new file mode 100644 index 0000000..30d44ca --- /dev/null +++ b/Domain/Exception/InvalidCountryException.php @@ -0,0 +1,7 @@ + 'Afghanistan', + 'AX' => 'Åland Islands', + 'AL' => 'Albania', + 'DZ' => 'Algeria', + 'AS' => 'American Samoa', + 'AD' => 'Andorra', + 'AO' => 'Angola', + 'AI' => 'Anguilla', + 'AQ' => 'Antarctica', + 'AG' => 'Antigua and Barbuda', + 'AR' => 'Argentina', + 'AU' => 'Australia', + 'AT' => 'Austria', + 'AZ' => 'Azerbaijan', + 'BS' => 'Bahamas', + 'BH' => 'Bahrain', + 'BD' => 'Bangladesh', + 'BB' => 'Barbados', + 'BY' => 'Belarus', + 'BE' => 'Belgium', + 'BZ' => 'Belize', + 'BJ' => 'Benin', + 'BM' => 'Bermuda', + 'BT' => 'Bhutan', + 'BO' => 'Bolivia', + 'BA' => 'Bosnia and Herzegovina', + 'BW' => 'Botswana', + 'BV' => 'Bouvet Island', + 'BR' => 'Brazil', + 'IO' => 'British Indian Ocean Territory', + 'BN' => 'Brunei Darussalam', + 'BG' => 'Bulgaria', + 'BF' => 'Burkina Faso', + 'BI' => 'Burundi', + 'KH' => 'Cambodia', + 'CM' => 'Cameroon', + 'CA' => 'Canada', + 'CV' => 'Cape Verde', + 'KY' => 'Cayman Islands', + 'CF' => 'Central African Republic', + 'TD' => 'Chad', + 'CL' => 'Chile', + 'CN' => 'China', + 'CX' => 'Christmas Island', + 'CC' => 'Cocos (Keeling) Islands', + 'CO' => 'Colombia', + 'KM' => 'Comoros', + 'CG' => 'Congo', + 'CD' => 'Zaire', + 'CK' => 'Cook Islands', + 'CR' => 'Costa Rica', + 'CI' => 'Côte D\'Ivoire', + 'HR' => 'Croatia', + 'CU' => 'Cuba', + 'CY' => 'Cyprus', + 'CZ' => 'Czech Republic', + 'DK' => 'Denmark', + 'DJ' => 'Djibouti', + 'DM' => 'Dominica', + 'DO' => 'Dominican Republic', + 'EC' => 'Ecuador', + 'EG' => 'Egypt', + 'SV' => 'El Salvador', + 'GQ' => 'Equatorial Guinea', + 'ER' => 'Eritrea', + 'EE' => 'Estonia', + 'ET' => 'Ethiopia', + 'FK' => 'Falkland Islands (Malvinas)', + 'FO' => 'Faroe Islands', + 'FJ' => 'Fiji', + 'FI' => 'Finland', + 'FR' => 'France', + 'GF' => 'French Guiana', + 'PF' => 'French Polynesia', + 'TF' => 'French Southern Territories', + 'GA' => 'Gabon', + 'GM' => 'Gambia', + 'GE' => 'Georgia', + 'DE' => 'Germany', + 'GH' => 'Ghana', + 'GI' => 'Gibraltar', + 'GR' => 'Greece', + 'GL' => 'Greenland', + 'GD' => 'Grenada', + 'GP' => 'Guadeloupe', + 'GU' => 'Guam', + 'GT' => 'Guatemala', + 'GG' => 'Guernsey', + 'GN' => 'Guinea', + 'GW' => 'Guinea-Bissau', + 'GY' => 'Guyana', + 'HT' => 'Haiti', + 'HM' => 'Heard Island and Mcdonald Islands', + 'VA' => 'Vatican City State', + 'HN' => 'Honduras', + 'HK' => 'Hong Kong', + 'HU' => 'Hungary', + 'IS' => 'Iceland', + 'IN' => 'India', + 'ID' => 'Indonesia', + 'IR' => 'Iran, Islamic Republic of', + 'IQ' => 'Iraq', + 'IE' => 'Ireland', + 'IM' => 'Isle of Man', + 'IL' => 'Israel', + 'IT' => 'Italy', + 'JM' => 'Jamaica', + 'JP' => 'Japan', + 'JE' => 'Jersey', + 'JO' => 'Jordan', + 'KZ' => 'Kazakhstan', + 'KE' => 'KENYA', + 'KI' => 'Kiribati', + 'KP' => 'Korea, Democratic People\'s Republic of', + 'KR' => 'Korea, Republic of', + 'KW' => 'Kuwait', + 'KG' => 'Kyrgyzstan', + 'LA' => 'Lao People\'s Democratic Republic', + 'LV' => 'Latvia', + 'LB' => 'Lebanon', + 'LS' => 'Lesotho', + 'LR' => 'Liberia', + 'LY' => 'Libyan Arab Jamahiriya', + 'LI' => 'Liechtenstein', + 'LT' => 'Lithuania', + 'LU' => 'Luxembourg', + 'MO' => 'Macao', + 'MK' => 'Macedonia, the Former Yugoslav Republic of', + 'MG' => 'Madagascar', + 'MW' => 'Malawi', + 'MY' => 'Malaysia', + 'MV' => 'Maldives', + 'ML' => 'Mali', + 'MT' => 'Malta', + 'MH' => 'Marshall Islands', + 'MQ' => 'Martinique', + 'MR' => 'Mauritania', + 'MU' => 'Mauritius', + 'YT' => 'Mayotte', + 'MX' => 'Mexico', + 'FM' => 'Micronesia, Federated States of', + 'MD' => 'Moldova, Republic of', + 'MC' => 'Monaco', + 'MN' => 'Mongolia', + 'ME' => 'Montenegro', + 'MS' => 'Montserrat', + 'MA' => 'Morocco', + 'MZ' => 'Mozambique', + 'MM' => 'Myanmar', + 'NA' => 'Namibia', + 'NR' => 'Nauru', + 'NP' => 'Nepal', + 'NL' => 'Netherlands', + 'AN' => 'Netherlands Antilles', + 'NC' => 'New Caledonia', + 'NZ' => 'New Zealand', + 'NI' => 'Nicaragua', + 'NE' => 'Niger', + 'NG' => 'Nigeria', + 'NU' => 'Niue', + 'NF' => 'Norfolk Island', + 'MP' => 'Northern Mariana Islands', + 'NO' => 'Norway', + 'OM' => 'Oman', + 'PK' => 'Pakistan', + 'PW' => 'Palau', + 'PS' => 'Palestinian Territory, Occupied', + 'PA' => 'Panama', + 'PG' => 'Papua New Guinea', + 'PY' => 'Paraguay', + 'PE' => 'Peru', + 'PH' => 'Philippines', + 'PN' => 'Pitcairn', + 'PL' => 'Poland', + 'PT' => 'Portugal', + 'PR' => 'Puerto Rico', + 'QA' => 'Qatar', + 'RE' => 'Réunion', + 'RO' => 'Romania', + 'RU' => 'Russian Federation', + 'RW' => 'Rwanda', + 'SH' => 'Saint Helena', + 'KN' => 'Saint Kitts and Nevis', + 'LC' => 'Saint Lucia', + 'PM' => 'Saint Pierre and Miquelon', + 'VC' => 'Saint Vincent and the Grenadines', + 'WS' => 'Samoa', + 'SM' => 'San Marino', + 'ST' => 'Sao Tome and Principe', + 'SA' => 'Saudi Arabia', + 'SN' => 'Senegal', + 'RS' => 'Serbia', + 'SC' => 'Seychelles', + 'SL' => 'Sierra Leone', + 'SG' => 'Singapore', + 'SK' => 'Slovakia', + 'SI' => 'Slovenia', + 'SB' => 'Solomon Islands', + 'SO' => 'Somalia', + 'ZA' => 'South Africa', + 'GS' => 'South Georgia and the South Sandwich Islands', + 'ES' => 'Spain', + 'LK' => 'Sri Lanka', + 'SD' => 'Sudan', + 'SR' => 'Suriname', + 'SJ' => 'Svalbard and Jan Mayen', + 'SZ' => 'Swaziland', + 'SE' => 'Sweden', + 'CH' => 'Switzerland', + 'SY' => 'Syrian Arab Republic', + 'TW' => 'Taiwan, Province of China', + 'TJ' => 'Tajikistan', + 'TZ' => 'Tanzania, United Republic of', + 'TH' => 'Thailand', + 'TL' => 'Timor-Leste', + 'TG' => 'Togo', + 'TK' => 'Tokelau', + 'TO' => 'Tonga', + 'TT' => 'Trinidad and Tobago', + 'TN' => 'Tunisia', + 'TR' => 'Turkey', + 'TM' => 'Turkmenistan', + 'TC' => 'Turks and Caicos Islands', + 'TV' => 'Tuvalu', + 'UG' => 'Uganda', + 'UA' => 'Ukraine', + 'AE' => 'United Arab Emirates', + 'GB' => 'United Kingdom', + 'US' => 'United States', + 'UM' => 'United States Minor Outlying Islands', + 'UY' => 'Uruguay', + 'UZ' => 'Uzbekistan', + 'VU' => 'Vanuatu', + 'VE' => 'Venezuela', + 'VN' => 'Viet Nam', + 'VG' => 'Virgin Islands, British', + 'VI' => 'Virgin Islands, U.S.', + 'WF' => 'Wallis and Futuna', + 'EH' => 'Western Sahara', + 'YE' => 'Yemen', + 'ZM' => 'Zambia', + 'ZW' => 'Zimbabwe', + ); + + private $_country; + + public function __construct($country) { + if(!in_array($country, $this->_countryCodes)) { + throw new InvalidCountryException('Invalid country'); + } + + $this->_country = $country; + } + + public function getCountryName() { + return $this->_country; + } + + public function getCountryCode() { + return array_search($this->_country, $this->_countryCodes); + } +} \ No newline at end of file diff --git a/Domain/VOs/ICountry.php b/Domain/VOs/ICountry.php new file mode 100644 index 0000000..d20639f --- /dev/null +++ b/Domain/VOs/ICountry.php @@ -0,0 +1,9 @@ +_firstName = $firstName; + $this->_lastName = $lastName; + } + + public function getFirstName() { + return $this->_firstName; + } + + public function getLastName() { + return $this->_lastName; + } + + public function getFullName() { + return sprintf('%s %s', $this->_firstName, $this->_lastName); + } +} \ No newline at end of file diff --git a/Domain/VOs/StepMania/Artist.php b/Domain/VOs/StepMania/Artist.php new file mode 100644 index 0000000..bd05386 --- /dev/null +++ b/Domain/VOs/StepMania/Artist.php @@ -0,0 +1,17 @@ +_name = $name; + } + + public function getName() { + return $this->_name; + } +} \ No newline at end of file diff --git a/Domain/VOs/StepMania/BPM.php b/Domain/VOs/StepMania/BPM.php new file mode 100644 index 0000000..4e37455 --- /dev/null +++ b/Domain/VOs/StepMania/BPM.php @@ -0,0 +1,25 @@ +high = $high; + $this->low = $low; + } + + public function getHigh() + { + return $this->high; + } + + public function getLow() + { + return $this->low; + } +} diff --git a/Domain/VOs/StepMania/DanceMode.php b/Domain/VOs/StepMania/DanceMode.php new file mode 100644 index 0000000..c54efe1 --- /dev/null +++ b/Domain/VOs/StepMania/DanceMode.php @@ -0,0 +1,34 @@ + 'Single', + 'dance-double' => 'Double' + ); + + public function __construct($stepManiaName) + { + if(array_key_exists($stepManiaName, $this->_nameMap)) { + $this->stepManiaName = $stepManiaName; + $this->prettyName = $this->_nameMap[$stepManiaName]; + } else { + throw new InvalidDanceModeException(sprintf('Invalid dance mode %s', $stepManiaName)); + } + } + + public function getStepManiaName() { + return $this->stepManiaName; + } + + public function getPrettyName() { + return $this->prettyName; + } +} diff --git a/Domain/VOs/StepMania/Difficulty.php b/Domain/VOs/StepMania/Difficulty.php new file mode 100644 index 0000000..74f836c --- /dev/null +++ b/Domain/VOs/StepMania/Difficulty.php @@ -0,0 +1,38 @@ + 'Novice', + 'beginner' => 'Novice', + 'easy' => 'Easy', + 'medium' => 'Medium', + 'hard' => 'Hard', + 'challenge' => 'Expert', + 'edit' => 'Edit' + ); + + public function __construct($stepManiaName) { + if(array_key_exists($stepManiaName, $this->_nameMap)) { + $this->stepManiaName = $stepManiaName; + $this->itgName = $this->_nameMap[$stepManiaName]; + } else { + throw new InvalidDifficultyException(sprintf('Invalid difficulty: %s', $stepManiaName)); + } + } + + public function getITGName() { + return $this->itgName; + } + + public function getStepManiaName() { + return $this->stepManiaName; + } +} diff --git a/Domain/VOs/StepMania/IArtist.php b/Domain/VOs/StepMania/IArtist.php new file mode 100644 index 0000000..b2bfdb4 --- /dev/null +++ b/Domain/VOs/StepMania/IArtist.php @@ -0,0 +1,9 @@ +tag = $tag; + } + + public function getTag() + { + return $this->tag; + } +} \ No newline at end of file diff --git a/Domain/VOs/StepMania/StepChart.php b/Domain/VOs/StepMania/StepChart.php new file mode 100644 index 0000000..8951b11 --- /dev/null +++ b/Domain/VOs/StepMania/StepChart.php @@ -0,0 +1,50 @@ +mode = $mode; + $this->difficulty = $difficulty; + $this->artist = $artist; + $this->rating = $rating; + } + + public function getMode() + { + return $this->mode; + } + + public function getRating() + { + return $this->rating; + } + + public function getDifficulty() + { + return $this->difficulty; + } + + public function getArtist() + { + return $this->artist; + } +} \ No newline at end of file diff --git a/Domain/VOs/StepMania/Tag.php b/Domain/VOs/StepMania/Tag.php new file mode 100644 index 0000000..f67a411 --- /dev/null +++ b/Domain/VOs/StepMania/Tag.php @@ -0,0 +1,20 @@ +_tag = $tag; + } + + public function getTag() + { + return $this->_tag; + } +} + diff --git a/README.md b/README.md index b52e774..fbe5c2b 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -rock n roll +Project depends on PHP-DI for dependency injection. Run `composer install` to sort that out. \ No newline at end of file diff --git a/Services/Bar.php b/Services/Bar.php new file mode 100644 index 0000000..47b916d --- /dev/null +++ b/Services/Bar.php @@ -0,0 +1,25 @@ + + */ +class Bar implements BarInterface +{ + + /** + * just returns a hardcoded string + * + * @return string + */ + public function returnMe() + { + return 'This is from the bar class'; + } +} \ No newline at end of file diff --git a/Services/BarInterface.php b/Services/BarInterface.php new file mode 100644 index 0000000..4f526e1 --- /dev/null +++ b/Services/BarInterface.php @@ -0,0 +1,22 @@ + + */ +interface BarInterface +{ + + /** + * returnMe + * + * @return string + */ + public function returnMe(); +} \ No newline at end of file diff --git a/Services/Foo.php b/Services/Foo.php new file mode 100644 index 0000000..abf8d25 --- /dev/null +++ b/Services/Foo.php @@ -0,0 +1,42 @@ + + */ +class Foo +{ + + /** + * This just holds the injected instance of Bar + * @var MikeFunk\Test\Bar + */ + protected $bar; + + /** + * inject Bar into $bar and then $this->bar with type hinting. The Bar + * class is bound to the BarInterface in config/di.yml + * + * @return void + */ + public function __construct(BarInterface $bar) + { + $this->bar = $bar; + } + + /** + * Just return the string from Bar::returnMe() + * + * @return string + */ + public function returnMe() + { + return $this->bar->returnMe(); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2ecc563 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "require": { + "mnapoli/php-di": "~4.0" + }, + "autoload": { + "psr-0": { + "Domain" : "./", + "DataAccess" : "./" + }, + "files" : ["./DataAccess/functions.php"] + } +} \ No newline at end of file diff --git a/config/DI.php b/config/DI.php new file mode 100644 index 0000000..915ff7c --- /dev/null +++ b/config/DI.php @@ -0,0 +1,5 @@ + DI\object('Domain\Entities\Bar'), +]; diff --git a/config/DataMaps.php b/config/DataMaps.php new file mode 100644 index 0000000..6de6c02 --- /dev/null +++ b/config/DataMaps.php @@ -0,0 +1,114 @@ + [ + 'class' => 'Domain\Entities\StepMania\Simfile', + 'table' => 'simfiles', + 'maps' => [ + //entity => table + 'title' => DataAccess\Varchar('title'), + 'artist' => DataAccess\VO('Artist'), + 'uploader' => DataAccess\Entity('User', 'getUploader'), + 'bpm' => DataAccess\VO('BPM'), + 'bpmChanges' => DataAccess\Int('bpm_changes', 'hasBPMChanges'), + 'stops' => DataAccess\Int('stops', 'hasStops'), + 'fgChanges' => DataAccess\Int('fg_changes', 'hasFgChanges'), + 'bgChanges' => DataAccess\Int('bg_changes', 'hasBgChanges'), + 'steps' => DataAccess\VOArray('StepChart', 'getSteps') + ] + ], + + 'BPM' => [ + 'class' => 'Domain\VOs\StepMania\BPM', + 'table' => 'simfiles', + 'maps' => [ + 'high' => DataAccess\Int('bpm_high', 'getHigh'), + 'low' => DataAccess\Int('bpm_low', 'getLow') + ] + ], + + 'User' => [ + 'class' => 'Domain\Entities\User', + 'table' => 'users', + 'maps' => [ + 'country' => DataAccess\VO('Country'), + 'displayName' => DataAccess\Varchar('display_name'), + 'name' => DataAccess\VO('Name'), + 'tags' => DataAccess\VOArray('Tag', 'getTags') // TODO: Make VarcharArray class + ] + ], + + 'Name' => [ + 'class' => 'Domain\VOs\Name', + 'table' => 'users_meta', + 'maps' => [ + 'firstname' => DataAccess\Varchar('firstname'), + 'lastname' => DataAccess\Varchar('lastname') + ] + ], + + 'Country' => [ + 'class' => 'Domain\VOs\Country', + 'table' => 'users_meta', + 'maps' => [ + 'country' => DataAccess\Varchar('country', 'getCountryName') + ] + ], + + 'Tag' => [ + 'class' => 'Domain\VOs\StepMania\Tag', + 'table' => 'step_artists', + 'maps' => [ + 'tag' => DataAccess\Varchar('tag') + ] + ], + + 'Artist' => [ + 'class' => 'Domain\VOs\StepMania\Artist', + 'table' => 'artists', + 'maps' => [ + 'name' => DataAccess\Varchar('name') + ] + ], + + 'StepChart' => [ + 'class' => 'Domain\VOs\StepMania\StepChart', + 'table' => 'steps', + 'maps' => [ + 'mode' => DataAccess\VO('DanceMode', 'getMode'), + 'difficulty' => DataAccess\VO('Difficulty'), + 'artist' => DataAccess\VO('StepArtist', 'getArtist', 'step_artist'), + 'rating' => DataAccess\Int('rating') + ] + ], + + 'DanceMode' => [ + 'class' => 'Domain\VOs\StepMania\DanceMode', + 'table' => 'steps', + 'maps' => [ + 'stepManiaName' => DataAccess\Varchar('mode', 'getStepManiaName') + ] + ], + + 'StepArtist' => [ + 'class' => 'Domain\VOs\StepMania\StepArtist', + 'table' => 'step_artists', + 'maps' => [ + 'tag' => DataAccess\Varchar('tag') + ] + ], + + 'Difficulty' => [ + 'class' => 'Domain\VOs\StepMania\Difficulty', + 'table' => 'steps', + 'maps' => [ + 'stepManiaName' => DataAccess\Varchar('difficulty', 'getStepManiaName') + ] + ] +]; diff --git a/divinelegy.sql b/divinelegy.sql new file mode 100644 index 0000000..1dab053 --- /dev/null +++ b/divinelegy.sql @@ -0,0 +1,188 @@ +-- MySQL dump 10.13 Distrib 5.6.12, for Win64 (x86_64) +-- +-- Host: localhost Database: divinelegy +-- ------------------------------------------------------ +-- Server version 5.6.12 + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `artists` +-- + +DROP TABLE IF EXISTS `artists`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `artists` ( + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `artists` +-- + +LOCK TABLES `artists` WRITE; +/*!40000 ALTER TABLE `artists` DISABLE KEYS */; +INSERT INTO `artists` VALUES (1,'atpunk01'),(2,'FaggotForce'); +/*!40000 ALTER TABLE `artists` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `simfiles` +-- + +DROP TABLE IF EXISTS `simfiles`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `simfiles` ( + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, + `title` varchar(255) NOT NULL, + `artist_id` mediumint(8) unsigned NOT NULL, + `user_id` mediumint(8) unsigned NOT NULL, + `bpm_high` int(11) NOT NULL, + `bpm_low` int(11) NOT NULL, + `bpm_changes` bit(1) NOT NULL, + `stops` bit(1) NOT NULL, + `fg_changes` bit(1) NOT NULL, + `bg_changes` bit(1) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `simfiles` +-- + +LOCK TABLES `simfiles` WRITE; +/*!40000 ALTER TABLE `simfiles` DISABLE KEYS */; +INSERT INTO `simfiles` VALUES (1,'BaBoom',1,1,177,177,'','\0','\0','\0'),(2,'Some Faggot Chart',2,2,230,230,'\0','\0','\0','\0'),(3,'More Bullshit',2,2,210,210,'\0','\0','\0','\0'); +/*!40000 ALTER TABLE `simfiles` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `step_artists` +-- + +DROP TABLE IF EXISTS `step_artists`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `step_artists` ( + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, + `tag` varchar(255) NOT NULL, + `user_id` mediumint(8) unsigned DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `step_artists` +-- + +LOCK TABLES `step_artists` WRITE; +/*!40000 ALTER TABLE `step_artists` DISABLE KEYS */; +INSERT INTO `step_artists` VALUES (1,'(-[Jayce]-)',1),(2,'Zaia',2),(3,'Serenade',2); +/*!40000 ALTER TABLE `step_artists` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `steps` +-- + +DROP TABLE IF EXISTS `steps`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `steps` ( + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, + `simfile_id` mediumint(8) unsigned NOT NULL, + `mode` enum('dance-single','dance-double') NOT NULL, + `rating` int(10) unsigned NOT NULL, + `difficulty` enum('beginner','easy','medium','hard','challenge','edit') NOT NULL, + `step_artist_id` mediumint(8) unsigned NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `steps` +-- + +LOCK TABLES `steps` WRITE; +/*!40000 ALTER TABLE `steps` DISABLE KEYS */; +INSERT INTO `steps` VALUES (1,1,'dance-single',10,'challenge',1),(2,1,'dance-single',9,'hard',1),(3,1,'dance-single',7,'medium',1),(4,1,'dance-single',4,'easy',1),(5,1,'dance-single',2,'beginner',1),(6,2,'dance-single',15,'beginner',2),(7,3,'dance-single',16,'beginner',3); +/*!40000 ALTER TABLE `steps` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `users` +-- + +DROP TABLE IF EXISTS `users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `users` ( + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, + `email` varchar(255) NOT NULL, + `display_name` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `users` +-- + +LOCK TABLES `users` WRITE; +/*!40000 ALTER TABLE `users` DISABLE KEYS */; +INSERT INTO `users` VALUES (1,'jayce@divinelegy.com','Jayce'),(2,'chino@chino.net','Chino'); +/*!40000 ALTER TABLE `users` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `users_meta` +-- + +DROP TABLE IF EXISTS `users_meta`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `users_meta` ( + `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, + `user_id` mediumint(8) unsigned NOT NULL, + `firstname` varchar(255) DEFAULT NULL, + `lastname` varchar(255) DEFAULT NULL, + `country` varchar(255) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `users_meta` +-- + +LOCK TABLES `users_meta` WRITE; +/*!40000 ALTER TABLE `users_meta` DISABLE KEYS */; +INSERT INTO `users_meta` VALUES (1,1,'Jayce','Newton','Australia'),(2,2,'Chino','Wood','Australia'); +/*!40000 ALTER TABLE `users_meta` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2014-08-31 19:53:16 diff --git a/public_html/index.php b/public_html/index.php new file mode 100644 index 0000000..bef36f7 --- /dev/null +++ b/public_html/index.php @@ -0,0 +1,70 @@ +addDefinitions('../config/DI.php'); + +$container = $containerBuilder->build(); + +/* @var $foo Domain\Entities\Foo */ +//$foo = $container->get('Domain\Entities\Foo'); +//$foo->returnMe(); + +$DataMapper = new \DataAccess\DataMapper\DataMapper('../config/DataMaps.php'); +$user = $DataMapper->find(1,'User'); + +$simfileFactory = new SimfileFactory(); +$simfileBuilder = new SimfileBuilder($simfileFactory); +$simfileStepByStepBuilder = new SimfileStepByStepBuilder($simfileBuilder); + +$danceMode = new DanceMode('dance-single'); +$difficulty = new Difficulty('challenge'); +$stepArtist = new StepArtist('Someone new fuck'); +$artist = new Artist('A brand new awesome artist!'); +$rating = '10'; + +$bpm = new BPM('256', '128'); +$stepChart = new StepChart($danceMode, + $difficulty, + $stepArtist, + $rating); + +$steps = array($stepChart); + + +$simfile = $simfileStepByStepBuilder->With_Title('Brand New Simfile') + ->With_Artist($artist) + ->With_Uploader($user) + ->With_BPM($bpm) + ->With_BpmChanges(SimfileConstants::NO_BPM_CHANGES) + ->With_Stops(SimfileConstants::NO_STOPS) + ->With_FgChanges(SimfileConstants::NO_FG_CHANGES) + ->With_BgChanges(SimfileConstants::NO_BG_CHANGES) + ->With_Steps($steps) + ->build(); + + +//$user->setId(NULL); + +$DataMapper->save($simfile); + + + + +//$stepchart = $simfile->getSteps(); +//$stepchart = $stepchart[0]; +//$maps = include '../config/DataMaps.php'; +// +//$DataMapper->save($user); + -- 2.11.0