Implement basic router and a controller for simfile API. Need to make some changes...
[rock.divinelegy.git] / DataAccess / DataMapper / DataMapper.php
1 <?php
2
3 namespace DataAccess\DataMapper;
4
5 use Domain\Entities\IDivineEntity;
6 use DataAccess\DataMapper\IDataMapper;
7 use DataAccess\DataMapper\Helpers\AbstractPopulationHelper;
8 use ReflectionClass;
9 use PDO;
10
11 class DataMapper implements IDataMapper
12 {
13 private $_db;
14 private $_maps;
15
16 public function __construct($maps)
17 {
18 //TODO: should probably do all this through a configuration object or something
19 $dsn = 'mysql:host=localhost;dbname=divinelegy;charset=utf8';
20 $username = 'root';
21 $password = 'toor';
22 $options = array(PDO::ATTR_EMULATE_PREPARES => false,
23 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
24
25 $this->_db = new PDO($dsn, $username, $password, $options);
26 $this->_maps = include $maps;
27 }
28
29 public function find($id, $entity)
30 {
31 $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id=%u',
32 $this->_maps[$entity]['table'],
33 $id));
34 $statement->execute();
35 $rows = $statement->fetchAll();
36
37 foreach($rows as $row)
38 {
39 $className = $this->_maps[$entity]['class']; //the entity to instantiate and return
40 $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db);
41
42 if(count($constructors) == 0)
43 {
44 $class = new $className;
45 } else {
46 $r = new ReflectionClass($className);
47 $class = $r->newInstanceArgs($constructors);
48 }
49
50 $class->setId($row['id']);
51 return $class;
52 }
53 }
54
55 public function save(IDivineEntity $entity)
56 {
57 $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);
58
59 // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)
60 // {
61 unset($queries['TYPE']);
62 $idMap = [];
63 foreach($queries as $index => $query)
64 {
65 $runQuery = true;
66 if (preg_match_all('/'.preg_quote('%').'(.*?)'.preg_quote('%').'/s', $query, $matches)) {
67 foreach($matches[1] as $index_ref)
68 {
69 if($index_ref != 'MAIN_QUERY_ID')
70 {
71 $index_id = str_replace('INDEX_REF_', '', $index_ref);
72 $query = str_replace('%INDEX_REF_' . $index_id . '%', $idMap['INDEX_REF_' . $index_id], $query);
73 } else {
74 $runQuery = false;
75 }
76 }
77 }
78
79 if($runQuery)
80 {
81 $statement = $this->_db->prepare($query);
82 $statement->execute();
83 $refIndex = $index+1;
84 $idMap['INDEX_REF_' . $refIndex] = $this->_db->lastInsertId();
85 unset($queries[$index]);
86 } else {
87 //update query so that other references are resolved.
88 $queries[$index] = $query;
89 }
90 }
91
92 //at this point we have queries left that depend on the main query id
93 foreach($queries as $query)
94 {
95 $query = str_replace('%MAIN_QUERY_ID%', end($idMap), $query);
96 $statement = $this->_db->prepare($query);
97 $statement->execute();
98 }
99 //}
100
101 echo '<pre>';
102 print_r($queries);
103 echo '</pre>';
104 }
105
106 //TODO: Implement
107 public function remove(IDivineEntity $entity) {
108 ;
109 }
110 }