More work on simfile repository.
authorCameron Ball <c.ball1729@gmail.com>
Wed, 17 Sep 2014 16:47:33 +0000 (00:47 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Wed, 17 Sep 2014 16:47:33 +0000 (00:47 +0800)
DataAccess/DataMapper/DataMapper.php
DataAccess/DataMapper/IDataMapper.php
DataAccess/IRepository.php [new file with mode: 0644]
DataAccess/StepMania/ISimfileRepository.php
DataAccess/StepMania/SimfileRepository.php

index 6401f19..1e49010 100644 (file)
@@ -26,17 +26,13 @@ class DataMapper implements IDataMapper
         $this->_maps = include $maps;\r
     }\r
     \r
-    public function find($id, $entity)\r
+    public function find($entityName, $queryString)\r
     {\r
-        return $this->findRange($id, $entity, 1);\r
-    }\r
-    \r
-    public function findRange($id, $entity, $limit)\r
-    {\r
-        $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id>=%u LIMIT %u',\r
-            $this->_maps[$entity]['table'],\r
-            $id,\r
-            $limit));\r
+        $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE %s',\r
+            $this->_maps[$entityName]['table'],\r
+            $queryString\r
+        ));\r
+        \r
         $statement->execute();\r
         $rows = $statement->fetchAll();\r
         \r
@@ -44,8 +40,8 @@ class DataMapper implements IDataMapper
         \r
         foreach($rows as $row)\r
         {\r
-            $className = $this->_maps[$entity]['class']; //the entity to instantiate and return\r
-            $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db);\r
+            $className = $this->_maps[$entityName]['class']; //the entity to instantiate and return\r
+            $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entityName, $row, $this->_db);\r
 \r
             if(count($constructors) == 0)\r
             {\r
@@ -60,6 +56,20 @@ class DataMapper implements IDataMapper
         }\r
         \r
         return count($entities) > 1 ? $entities : reset($entities);\r
+        \r
+        return $this->findRange($id, $entityName, 1);\r
+    }\r
+    \r
+    public function findById($id, $entity)\r
+    {\r
+         $queryString = sprintf('id=%u', $id);\r
+         return $this->find($entity, $queryString);\r
+    }\r
+    \r
+    public function findRange($id, $entity, $limit)\r
+    {\r
+        $queryString = sprintf('id>=%u LIMIT %u', $id, $limit);\r
+        return $this->find($entity, $queryString);\r
     }\r
     \r
     public function save(IDivineEntity $entity)\r
index 987bd82..ee90de0 100644 (file)
@@ -8,10 +8,12 @@ interface IDataMapper
 {\r
     //TODO: Table is the wrong name. We actually give the implementation the entity name and it finds the table from the maps.\r
     \r
+    //find in table based on criteria in queryString\r
+    public function find($entityName, $queryString);\r
     //find id in table and return it as an entity\r
-    public function find($id, $table);\r
+    public function findById($id, $entityName);\r
     //find rows with id >= id and stop at limit\r
-    public function findRange($id, $table, $limit);\r
+    public function findRange($id, $entityName, $limit);\r
     //insert/update entity in table\r
     public function save(IDivineEntity $entity);\r
     //remove entity from table\r
diff --git a/DataAccess/IRepository.php b/DataAccess/IRepository.php
new file mode 100644 (file)
index 0000000..34765cb
--- /dev/null
@@ -0,0 +1,13 @@
+<?php\r
+\r
+namespace DataAccess;\r
+\r
+use Domain\Entities\IDivineEntity;\r
+\r
+interface IRepository\r
+{\r
+    public function findById($id);\r
+    public function findRange($id, $limit);\r
+    public function save(IDivineEntity $entity);\r
+    public function remove(IDivineEntity $entity);\r
+}
\ No newline at end of file
index 30a8264..eea84c5 100644 (file)
@@ -2,12 +2,22 @@
 
 namespace DataAccess\StepMania;
 
+use DataAccess\IRepository;
 use Domain\Entities\StepMania\ISimfile;
 
-interface ISimfileRepository
+interface ISimfileRepository extends IRepository
 {
-    public function find($id);
+    public function findByTitle($title);
+    public function findByArtist($artist);
+    public function findByBpm($high, $low);
+    public function findByStepArtist($artistName);
+    public function findByLightMeter($feet);
+    public function findByBeginnerMeter($feet);
+    public function findByMediumMeter($feet);
+    public function findByHardMeter($feet);
+    public function findByExpertMeter($feet);
     public function save(ISimfile $entity);
     public function remove(ISimfile $entity);
 }
+
     
index 26c8964..7e1a8fa 100644 (file)
@@ -15,8 +15,8 @@ class SimfileRepository implements ISimfileRepository
         $this->dataMapper = $dataMapper;\r
     }\r
     \r
-    public function find($id) {\r
-        return $this->dataMapper->find($id, 'Simfile');\r
+    public function findById($id) {\r
+        return $this->dataMapper->findById($id, 'Simfile');\r
     }\r
     \r
     public function findRange($id, $limit)\r