use DataAccess\StepMania\ISimfileRepository;
use Services\Http\IHttpResponse;
use Services\Http\IHttpRequest;
-use Controllers\AbstractBaseController;
+use DataAccess\Queries\StepMania\SimfileQueryConstraints;
class IndexController implements IDivineController
{
-
- private $_content;
private $_simfileRepository;
private $_response;
private $_request;
$this->_simfileRepository = $repository;
}
- public function indexAction() {
- $this->_response->setHeader('Content-Type', 'application/json')
- ->setBody(json_encode(array('message' => 'nothing to see here')))
- ->sendResponse();
+ public function indexAction() {
+ $queryConstraints = new SimfileQueryConstraints();
+ $queryConstraints->stepsHaveRating(15);
+
+ $simfiles = $this->_simfileRepository->findByTitle('a', $queryConstraints);
+
+ foreach($simfiles as $simfile)
+ {
+ echo $simfile->getTitle();
+ }
+
+// $this->_response->setHeader('Content-Type', 'application/json')
+// ->setBody(json_encode(array('message' => 'nothing to see here')))
+// ->sendResponse();
}
}
$options = array(PDO::ATTR_EMULATE_PREPARES => false,\r
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);\r
\r
- $this->_db = new PDO($dsn, $username, $password, $options); \r
+ $this->_db = new PDO($dsn, $username, null, $options); \r
$this->_maps = include $maps;\r
}\r
\r
- public function find($entityName, $queryString)\r
+ public function map($entityName, $queryString = 'SELECT * FROM %s')\r
{\r
- $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE %s',\r
- $this->_maps[$entityName]['table'],\r
- $queryString\r
+ $statement = $this->_db->prepare(sprintf($queryString,\r
+ $this->_maps[$entityName]['table']\r
));\r
\r
$statement->execute();\r
$entities[$row['id']] = $class;\r
}\r
\r
- return count($entities) > 1 ? $entities : reset($entities);\r
- \r
- return $this->findRange($id, $entityName, 1);\r
+ return $entities;\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
+ \r
public function save(IDivineEntity $entity)\r
{\r
$queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);\r
\r
namespace DataAccess\DataMapper;\r
\r
+use DataAccess\Queries\IQueryConstraints;\r
use Domain\Entities\IDivineEntity;\r
\r
interface IDataMapper\r
{\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 findById($id, $entityName);\r
- //find rows with id >= id and stop at limit\r
- public function findRange($id, $entityName, $limit);\r
+ //find in table based on constraints and return it as entity\r
+ public function map($entityName, $queryString);\r
//insert/update entity in table\r
public function save(IDivineEntity $entity);\r
//remove entity from table\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
--- /dev/null
+<?php
+
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+namespace DataAccess\Queries;
+
+interface IQueryConstraints
+{
+ public function applyTo($queryString);
+ public function limit($start, $end);
+ public function where($column, $operator, $value);
+ public function join($type, $tableA, $columnA, $tableB, $columnB);
+}
\ No newline at end of file
--- /dev/null
+<?php
+
+namespace DataAccess\Queries;
+
+use DataAccess\Queries\IQueryConstraints;
+
+class QueryConstraints implements IQueryConstraints
+{
+ private $_queryString;
+
+ protected $_whereClauses = array();
+ protected $_limitClause;
+ protected $_joinClause;
+
+ public function applyTo($queryString)
+ {
+ $this->_queryString = $queryString;
+
+ $this->applyJoinClause()
+ ->applyWhereClauses()
+ ->applyLimitClause();
+
+ return $this->_queryString;
+ }
+
+ public function where($columnName, $operator, $value)
+ {
+ $this->_whereClauses[$columnName] = array('operator' => $operator, 'value' => $value);
+ return $this;
+ }
+
+ public function limit($start, $end = null)
+ {
+ if($end)
+ {
+ $this->_limitClause = sprintf(' LIMIT %u,%u', $start, $end);
+ return $this;
+ }
+
+ $this->_limitClause = sprintf(' LIMIT %u', $start);
+
+ return $this;
+ }
+
+ public function join($type, $tableA, $columnA, $tableB, $columnB)
+ {
+ $this->_joinClause = sprintf(' %s JOIN %s ON %s.%s = %s.%s', $type, $tableB, $tableA, $columnA, $tableB, $columnB);
+ return $this;
+ }
+
+ private function applyJoinClause()
+ {
+ $this->_queryString .= $this->_joinClause;
+ return $this;
+ }
+
+ private function applyWhereClauses()
+ {
+ $this->_queryString .= ' WHERE ';
+
+ foreach($this->_whereClauses as $columnName => $columnValue)
+ {
+ switch(gettype($columnValue['value']))
+ {
+ case 'integer':
+ $this->_queryString .= sprintf("%s%s%u", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND ';
+ break;
+ case 'string':
+ $this->_queryString .= sprintf("%s %s '%s'", $columnName, $columnValue['operator'], $columnValue['value']) . ' AND ';
+ break;
+ }
+
+ }
+
+ $this->_queryString = rtrim($this->_queryString, ' AND ');
+ return $this;
+ }
+
+ private function applyLimitClause()
+ {
+ $this->_queryString .= $this->_limitClause;
+ return $this;
+ }
+
+}
\ No newline at end of file
--- /dev/null
+<?php
+
+namespace DataAccess\Queries\StepMania;
+
+use DataAccess\Queries\IQueryConstraints;
+
+interface ISimfileQueryConstraints extends IQueryConstraints
+{
+ public function hasFgChanges($bool);
+ public function hasBgChanges($bool);
+ public function stepsHaveRating($rating);
+ public function hasDifficulty($difficulty);
+}
\ No newline at end of file
--- /dev/null
+<?php
+
+namespace DataAccess\Queries\StepMania;
+
+use DataAccess\Queries\QueryConstraints;
+use DataAccess\Queries\StepMania\ISimfileQueryConstraints;
+
+class SimfileQueryConstraints extends QueryConstraints implements ISimfileQueryConstraints
+{
+ public function hasFgChanges($bool)
+ {
+ return $this->where('fg_changes', '=', (int)$bool);
+ }
+ public function hasBgChanges($bool)
+ {
+ return $this->where('bg_changes', '=', (int)$bool);
+ }
+
+ public function stepsHaveRating($rating)
+ {
+ return $this->join('INNER', 'simfiles', 'id', 'steps', 'simfile_id')
+ ->where('steps.rating', '=', $rating);
+ }
+ public function hasDifficulty($difficulty){}
+
+ public function bpm($bpm)
+ {
+ return;
+ }
+}
namespace DataAccess\StepMania;
use DataAccess\IRepository;
+use DataAccess\Queries\StepMania\ISimfileQueryConstraints;
use Domain\Entities\StepMania\ISimfile;
interface ISimfileRepository extends IRepository
{
- public function findByTitle($title);
+ public function findByTitle($title, ISimfileQueryConstraints $constraints);
public function findByArtist($artist);
public function findByBpm($high, $low);
public function findByStepArtist($artistName);
\r
use DataAccess\StepMania\ISimfileRepository;\r
use DataAccess\DataMapper\IDataMapper;\r
+use DataAccess\Queries\StepMania\ISimfileQueryConstraints;\r
use Domain\Entities\StepMania\ISimfile;\r
\r
//TODO: Implement some sort of caching. Probably OK for now not to worry.\r
class SimfileRepository implements ISimfileRepository\r
{\r
- private $dataMapper;\r
+ private $_dataMapper;\r
\r
public function __construct(IDataMapper $dataMapper) {\r
- $this->dataMapper = $dataMapper;\r
+ $this->_dataMapper = $dataMapper;\r
}\r
\r
public function findById($id) {\r
- return $this->dataMapper->findById($id, 'Simfile');\r
+ return $this->_dataMapper->map(\r
+ 'Simfile',\r
+ 'SELECT * FROM %s WHERE id=' . $id\r
+ );\r
}\r
\r
public function findRange($id, $limit)\r
{\r
- return $this->dataMapper->findRange($id, 'Simfile', $limit);\r
+ return $this->_dataMapper->findRange(\r
+ 'Simfile',\r
+ 'SELECT * FROM %s WHERE id>=' . $id . ' LIMIT ' . $limit\r
+ );\r
}\r
\r
public function save(ISimfile $entity) {\r
- $this->dataMapper->save($entity);\r
+ $this->_dataMapper->save($entity);\r
}\r
\r
//TODO: Implement\r
public function remove(ISimfile $entity) {\r
;\r
}\r
+ \r
+ public function findByTitle($title, ISimfileQueryConstraints $constraints = NULL)\r
+ {\r
+ //TODO: Should I inject a factory, and then make $constraints if it isn't given?\r
+ if($constraints)\r
+ {\r
+ $queryString = $constraints->where('title', 'LIKE', "%%$title%%") //TODO: Should I make a like method that handles adding the %% ?\r
+ ->applyTo('SELECT * from %s');\r
+ } else {\r
+ //It would avoid this, or rather I could put this in the constraints class\r
+ $queryString = "SELECT * FROM %s WHERE title LIKE '%$title%'";\r
+ }\r
+ \r
+ //is it better to pass in constraints object?\r
+ //could have a default "select * from %s" in the constraints object which could be overwritten via a method.\r
+ //-no more need for applyTo, just go $constratints->getQuery\r
+ //maybe it should no longer be constraints but instead queryBuilder\r
+ \r
+ /**\r
+ * have this class contain a queryBuilderFactory and then have constraintsClass \r
+ * go in through methods which act on the query, adding in constraints.\r
+ */\r
+ return $this->_dataMapper->map('Simfile', $queryString);\r
+ }\r
+ \r
+ public function findByArtist($artist){}\r
+ public function findByBpm($high, $low){}\r
+ public function findByStepArtist($artistName){}\r
+ public function findByLightMeter($feet){}\r
+ public function findByBeginnerMeter($feet){}\r
+ public function findByMediumMeter($feet){}\r
+ public function findByHardMeter($feet){}\r
+ public function findByExpertMeter($feet){}\r
}\r
$controllerAction = $router->getActionName();\r
$controllerActionArgs = $router->getActionArgs();\r
\r
-$controller = $container->get('Controllers\\' . $controllerName . 'Controller' );\r
+$controller = $container->get('Controllers\\' . ucfirst($controllerName) . 'Controller' );\r
\r
// Last thing to do, call the action on the specified controller.\r
call_user_func(array($controller, $controllerAction . 'Action'), $controllerActionArgs);\r