8ee898dabac054d7bd787c5b14a00065a61ca691
[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 $row = $statement->fetch();
36
37 $className = $this->_maps[$entity]['class']; //the entity to instantiate and return
38 $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db);
39
40 if(count($constructors) == 0)
41 {
42 $class = new $className;
43 } else {
44 $r = new ReflectionClass($className);
45 $class = $r->newInstanceArgs($constructors);
46 }
47
48 $class->setId($row['id']);
49 return $class;
50 }
51
52 public function save(IDivineEntity $entity)
53 {
54 $queries = AbstractPopulationHelper::generateUpdateSaveQuery($this->_maps, $entity, $entity->getId(), $this->_db);
55
56 // if($queries['TYPE'] == AbstractPopulationHelper::QUERY_TYPE_CREATE)
57 // {
58 unset($queries['TYPE']);
59 $idMap = [];
60 foreach($queries as $index => $query)
61 {
62 $runQuery = true;
63 if (preg_match_all('/'.preg_quote('%').'(.*?)'.preg_quote('%').'/s', $query, $matches)) {
64 foreach($matches[1] as $index_ref)
65 {
66 if($index_ref != 'MAIN_QUERY_ID')
67 {
68 $index_id = str_replace('INDEX_REF_', '', $index_ref);
69 $query = str_replace('%INDEX_REF_' . $index_id . '%', $idMap['INDEX_REF_' . $index_id], $query);
70 } else {
71 $runQuery = false;
72 }
73 }
74 }
75
76 if($runQuery)
77 {
78 $statement = $this->_db->prepare($query);
79 $statement->execute();
80 $refIndex = $index+1;
81 $idMap['INDEX_REF_' . $refIndex] = $this->_db->lastInsertId();
82 unset($queries[$index]);
83 } else {
84 //update query so that other references are resolved.
85 $queries[$index] = $query;
86 }
87 }
88
89 //at this point we have queries left that depend on the main query id
90 foreach($queries as $query)
91 {
92 $query = str_replace('%MAIN_QUERY_ID%', end($idMap), $query);
93 $statement = $this->_db->prepare($query);
94 $statement->execute();
95 }
96 //}
97
98 echo '<pre>';
99 print_r($queries);
100 echo '</pre>';
101 }
102
103 //TODO: Implement
104 public function remove(IDivineEntity $entity) {
105 ;
106 }
107 }