@@ -7,17 +7,17 @@ |
||
7 | 7 | private $intermediateName; |
8 | 8 | |
9 | 9 | public function __construct(\Maphper\Iterator $iterator, $intermediateName = null) { |
10 | - $this->iterator = $iterator; |
|
11 | - $this->intermediateName = $intermediateName; |
|
10 | + $this->iterator = $iterator; |
|
11 | + $this->intermediateName = $intermediateName; |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | public function current() { |
15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
15 | + if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
16 | 16 | return $this->iterator->current(); |
17 | 17 | } |
18 | 18 | |
19 | 19 | public function key() { |
20 | - return $this->iterator->key(); |
|
20 | + return $this->iterator->key(); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | public function next() { |
@@ -96,15 +96,15 @@ discard block |
||
96 | 96 | $value = $this->entity->wrap($this->relations, $value); |
97 | 97 | $this->dataSource->save($value); |
98 | 98 | $visibilityOverride->write($value); |
99 | - $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
99 | + $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | public function offsetExists($offset) { |
103 | 103 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
104 | - if (!empty($this->settings['filter'])) { |
|
105 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
106 | - return isset($data[0]); |
|
107 | - } |
|
104 | + if (!empty($this->settings['filter'])) { |
|
105 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
106 | + return isset($data[0]); |
|
107 | + } |
|
108 | 108 | return (bool) $this->dataSource->findById($offset); |
109 | 109 | } |
110 | 110 | |
@@ -114,10 +114,10 @@ discard block |
||
114 | 114 | |
115 | 115 | public function offsetGet($offset) { |
116 | 116 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
117 | - if (!empty($this->settings['filter'])) { |
|
118 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
119 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
120 | - } |
|
117 | + if (!empty($this->settings['filter'])) { |
|
118 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
119 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
120 | + } |
|
121 | 121 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
122 | 122 | } |
123 | 123 |
@@ -4,7 +4,7 @@ |
||
4 | 4 | private $parent; |
5 | 5 | private $primaryKey; |
6 | 6 | private $lookup; |
7 | - private $mapper; |
|
7 | + private $mapper; |
|
8 | 8 | |
9 | 9 | public function __construct(Maphper $mapper, $lookup, array $primaryKey, MultiPk $parent = null) { |
10 | 10 | $this->parent = $parent; |
@@ -8,7 +8,7 @@ |
||
8 | 8 | public function delete($table, $criteria, $args, $limit = null, $offset = null, $order = null) { |
9 | 9 | $limit = $limit ? ' LIMIT ' . $limit : ''; |
10 | 10 | $offset = $offset ? ' OFFSET ' . $offset : ''; |
11 | - $order = $order ? ' ORDER BY ' . $order : ''; |
|
11 | + $order = $order ? ' ORDER BY ' . $order : ''; |
|
12 | 12 | return new Query('DELETE FROM ' . $table . ' WHERE ' . ($criteria ?: '1 = 1 ') . $order . $limit . $offset, $args); |
13 | 13 | } |
14 | 14 |
@@ -1,14 +1,14 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | class StmtCache { |
4 | - private $pdo; |
|
5 | - private $queryCache = []; |
|
4 | + private $pdo; |
|
5 | + private $queryCache = []; |
|
6 | 6 | |
7 | - public function __construct(\PDO $pdo) { |
|
8 | - $this->pdo = $pdo; |
|
9 | - } |
|
7 | + public function __construct(\PDO $pdo) { |
|
8 | + $this->pdo = $pdo; |
|
9 | + } |
|
10 | 10 | |
11 | - public function getCachedStmt($sql) { |
|
11 | + public function getCachedStmt($sql) { |
|
12 | 12 | $queryId = $this->getQueryId($sql); |
13 | 13 | if (isset($this->queryCache[$queryId])) $stmt = $this->queryCache[$queryId]; |
14 | 14 | else { |
@@ -18,15 +18,15 @@ discard block |
||
18 | 18 | return $stmt; |
19 | 19 | } |
20 | 20 | |
21 | - private function getQueryId($sql) { |
|
22 | - return md5($sql); |
|
23 | - } |
|
21 | + private function getQueryId($sql) { |
|
22 | + return md5($sql); |
|
23 | + } |
|
24 | 24 | |
25 | - public function deleteQueryFromCache($sql) { |
|
26 | - unset($this->queryCache[$this->getQueryId($sql)]); |
|
27 | - } |
|
25 | + public function deleteQueryFromCache($sql) { |
|
26 | + unset($this->queryCache[$this->getQueryId($sql)]); |
|
27 | + } |
|
28 | 28 | |
29 | - public function clearCache() { |
|
30 | - $this->queryCache = []; |
|
31 | - } |
|
29 | + public function clearCache() { |
|
30 | + $this->queryCache = []; |
|
31 | + } |
|
32 | 32 | } |
@@ -7,8 +7,8 @@ discard block |
||
7 | 7 | |
8 | 8 | private $primaryKey; |
9 | 9 | private $fields = '*'; |
10 | - private $databaseSelect; |
|
11 | - private $databaseCrud; |
|
10 | + private $databaseSelect; |
|
11 | + private $databaseCrud; |
|
12 | 12 | |
13 | 13 | public function __construct($db, $table, $primaryKey = 'id', array $options = []) { |
14 | 14 | $options = new DatabaseOptions($db, $options); |
@@ -20,10 +20,10 @@ discard block |
||
20 | 20 | |
21 | 21 | $defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort') : implode(', ', $this->primaryKey); |
22 | 22 | |
23 | - $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table); |
|
23 | + $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table); |
|
24 | 24 | |
25 | - $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort); |
|
26 | - $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey); |
|
25 | + $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort); |
|
26 | + $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey); |
|
27 | 27 | |
28 | 28 | $databaseModify->optimizeColumns(); |
29 | 29 | } |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | } |
46 | 46 | |
47 | 47 | public function findByField(array $fields, $options = []) { |
48 | - return $this->databaseSelect->findByField($fields, $options); |
|
48 | + return $this->databaseSelect->findByField($fields, $options); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | public function deleteByField(array $fields, array $options = []) { |
@@ -53,6 +53,6 @@ discard block |
||
53 | 53 | } |
54 | 54 | |
55 | 55 | public function save($data) { |
56 | - $this->databaseCrud->save($data, true); |
|
56 | + $this->databaseCrud->save($data, true); |
|
57 | 57 | } |
58 | 58 | } |
@@ -2,25 +2,25 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | |
4 | 4 | class DatabaseModify { |
5 | - private $adapter; |
|
6 | - private $alterDb; |
|
7 | - private $table; |
|
5 | + private $adapter; |
|
6 | + private $alterDb; |
|
7 | + private $table; |
|
8 | 8 | |
9 | - public function __construct(DatabaseAdapter $adapter, $alterDb, $table) { |
|
10 | - $this->adapter = $adapter; |
|
11 | - $this->alterDb = $alterDb; |
|
12 | - $this->table = $table; |
|
13 | - } |
|
9 | + public function __construct(DatabaseAdapter $adapter, $alterDb, $table) { |
|
10 | + $this->adapter = $adapter; |
|
11 | + $this->alterDb = $alterDb; |
|
12 | + $this->table = $table; |
|
13 | + } |
|
14 | 14 | |
15 | - public function addIndex($args) { |
|
15 | + public function addIndex($args) { |
|
16 | 16 | if (Database::EDIT_INDEX & $this->alterDb) $this->adapter->addIndex($this->table, $args); |
17 | 17 | } |
18 | 18 | |
19 | - public function optimizeColumns() { |
|
20 | - if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
21 | - } |
|
19 | + public function optimizeColumns() { |
|
20 | + if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
21 | + } |
|
22 | 22 | |
23 | - public function getTryInsertAgain($tryagain) { |
|
24 | - return $tryagain && Database::EDIT_STRUCTURE & $this->alterDb; |
|
25 | - } |
|
23 | + public function getTryInsertAgain($tryagain) { |
|
24 | + return $tryagain && Database::EDIT_STRUCTURE & $this->alterDb; |
|
25 | + } |
|
26 | 26 | } |
@@ -2,31 +2,31 @@ discard block |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | |
4 | 4 | class DatabaseSelect { |
5 | - private $resultCache = []; |
|
6 | - private $idCache = []; |
|
7 | - private $selectBuilder; |
|
8 | - private $whereBuilder; |
|
9 | - private $adapter; |
|
10 | - private $databaseModify; |
|
11 | - private $defaultSort; |
|
12 | - private $table; |
|
13 | - |
|
14 | - public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort) { |
|
15 | - $this->adapter = $adapter; |
|
16 | - $this->databaseModify = $databaseModify; |
|
17 | - $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
18 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
19 | - $this->defaultSort = $defaultSort; |
|
20 | - $this->table = $table; |
|
21 | - } |
|
22 | - |
|
23 | - public function findById($id, $pk) { |
|
5 | + private $resultCache = []; |
|
6 | + private $idCache = []; |
|
7 | + private $selectBuilder; |
|
8 | + private $whereBuilder; |
|
9 | + private $adapter; |
|
10 | + private $databaseModify; |
|
11 | + private $defaultSort; |
|
12 | + private $table; |
|
13 | + |
|
14 | + public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, $table, $defaultSort) { |
|
15 | + $this->adapter = $adapter; |
|
16 | + $this->databaseModify = $databaseModify; |
|
17 | + $this->selectBuilder = new \Maphper\Lib\SelectBuilder(); |
|
18 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
19 | + $this->defaultSort = $defaultSort; |
|
20 | + $this->table = $table; |
|
21 | + } |
|
22 | + |
|
23 | + public function findById($id, $pk) { |
|
24 | 24 | if (!isset($this->idCache[$id])) { |
25 | 25 | try { |
26 | 26 | $result = $this->selectQuery($this->selectBuilder->select($this->table, $pk . ' = :id', [':id' => $id], ['limit' => 1])); |
27 | 27 | } |
28 | 28 | catch (\Exception $e) { |
29 | - // Don't issue an error if it cannot be found since we return null |
|
29 | + // Don't issue an error if it cannot be found since we return null |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | if (isset($result[0])) $this->idCache[$id] = $result[0]; |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | return $this->idCache[$id]; |
36 | 36 | } |
37 | 37 | |
38 | - public function findByField(array $fields, $options = []) { |
|
38 | + public function findByField(array $fields, $options = []) { |
|
39 | 39 | $cacheId = md5(serialize(func_get_args())); |
40 | 40 | if (!isset($this->resultCache[$cacheId])) { |
41 | 41 | $query = $this->whereBuilder->createSql($fields); |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | return $this->resultCache[$cacheId]; |
56 | 56 | } |
57 | 57 | |
58 | - public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
58 | + public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
|
59 | 59 | //Cannot count/sum/max multiple fields, pick the first one. This should only come into play when trying to count() a mapper with multiple primary keys |
60 | 60 | if (is_array($field)) $field = $field[0]; |
61 | 61 | $query = $this->whereBuilder->createSql($criteria); |
@@ -72,34 +72,34 @@ discard block |
||
72 | 72 | } |
73 | 73 | } |
74 | 74 | |
75 | - private function determineAggregateResult($result, $group, $field) { |
|
76 | - if ($group != null) { |
|
77 | - $ret = []; |
|
78 | - foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
79 | - return $ret; |
|
80 | - } |
|
81 | - else if (isset($result[0])) return $result[0]->val; |
|
82 | - else return 0; |
|
83 | - } |
|
84 | - |
|
85 | - private function selectQuery(\Maphper\Lib\Query $query) { |
|
86 | - return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
87 | - } |
|
88 | - |
|
89 | - public function clearResultCache() { |
|
90 | - $this->resultCache = []; |
|
91 | - } |
|
92 | - |
|
93 | - public function clearIDCache() { |
|
94 | - $this->idCache = []; |
|
95 | - } |
|
96 | - |
|
97 | - public function updateCache($data, $pkValue) { |
|
75 | + private function determineAggregateResult($result, $group, $field) { |
|
76 | + if ($group != null) { |
|
77 | + $ret = []; |
|
78 | + foreach ($result as $res) $ret[$res->$field] = $res->val; |
|
79 | + return $ret; |
|
80 | + } |
|
81 | + else if (isset($result[0])) return $result[0]->val; |
|
82 | + else return 0; |
|
83 | + } |
|
84 | + |
|
85 | + private function selectQuery(\Maphper\Lib\Query $query) { |
|
86 | + return $this->adapter->query($query)->fetchAll(\PDO::FETCH_OBJ); |
|
87 | + } |
|
88 | + |
|
89 | + public function clearResultCache() { |
|
90 | + $this->resultCache = []; |
|
91 | + } |
|
92 | + |
|
93 | + public function clearIDCache() { |
|
94 | + $this->idCache = []; |
|
95 | + } |
|
96 | + |
|
97 | + public function updateCache($data, $pkValue) { |
|
98 | 98 | if (isset($this->cache[$pkValue])) $this->cache[$pkValue] = (object) array_merge((array)$this->cache[$pkValue], (array)$data); |
99 | 99 | else $this->cache[$pkValue] = $data; |
100 | - } |
|
100 | + } |
|
101 | 101 | |
102 | - public function deleteIDFromCache($id) { |
|
103 | - unset($this->idCache[$id]); |
|
104 | - } |
|
102 | + public function deleteIDFromCache($id) { |
|
103 | + unset($this->idCache[$id]); |
|
104 | + } |
|
105 | 105 | } |
@@ -2,76 +2,76 @@ |
||
2 | 2 | namespace Maphper\DataSource; |
3 | 3 | use Maphper\Maphper; |
4 | 4 | class Mock implements \Maphper\DataSource { |
5 | - private $data; |
|
6 | - private $id; |
|
5 | + private $data; |
|
6 | + private $id; |
|
7 | 7 | |
8 | - public function __construct(\ArrayObject $data, $id) { |
|
9 | - $this->data = $data; |
|
10 | - $this->id = is_array($id) ? $id : [$id]; |
|
11 | - } |
|
8 | + public function __construct(\ArrayObject $data, $id) { |
|
9 | + $this->data = $data; |
|
10 | + $this->id = is_array($id) ? $id : [$id]; |
|
11 | + } |
|
12 | 12 | |
13 | - public function getPrimaryKey() { |
|
14 | - return $this->id; |
|
15 | - } |
|
13 | + public function getPrimaryKey() { |
|
14 | + return $this->id; |
|
15 | + } |
|
16 | 16 | |
17 | - public function findById($id) { |
|
18 | - return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | - } |
|
17 | + public function findById($id) { |
|
18 | + return isset($this->data[$id]) ? (array)$this->data[$id] : []; |
|
19 | + } |
|
20 | 20 | |
21 | - public function findByField(array $fields, $options = []) { |
|
22 | - $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
23 | - $filteredArray = $arrayFilter->filter($fields); |
|
24 | - if (isset($options['order'])) { |
|
25 | - list($columns, $order) = explode(' ', $options['order']); |
|
26 | - usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
27 | - } |
|
28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | - return $filteredArray; |
|
31 | - } |
|
21 | + public function findByField(array $fields, $options = []) { |
|
22 | + $arrayFilter = new \Maphper\Lib\ArrayFilter(iterator_to_array($this->data->getIterator())); |
|
23 | + $filteredArray = $arrayFilter->filter($fields); |
|
24 | + if (isset($options['order'])) { |
|
25 | + list($columns, $order) = explode(' ', $options['order']); |
|
26 | + usort($filteredArray, $this->getOrderFunction($order, $columns)); |
|
27 | + } |
|
28 | + if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
29 | + if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
30 | + return $filteredArray; |
|
31 | + } |
|
32 | 32 | |
33 | 33 | public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) { |
34 | - return $function($this->findByField($criteria)); |
|
35 | - } |
|
34 | + return $function($this->findByField($criteria)); |
|
35 | + } |
|
36 | 36 | |
37 | 37 | public function deleteById($id) { |
38 | - unset($this->data[$id]); |
|
39 | - } |
|
38 | + unset($this->data[$id]); |
|
39 | + } |
|
40 | 40 | |
41 | 41 | public function deleteByField(array $fields, array $options) { |
42 | - foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
43 | - } |
|
42 | + foreach ($this->findByField($fields, $options) as $val) unset($this->data[$val->{$this->id[0]}]); |
|
43 | + } |
|
44 | 44 | |
45 | - public function save($data) { |
|
46 | - if (isset($data->{$this->id[0]})) { |
|
47 | - $id = $data->{$this->id[0]}; |
|
48 | - } |
|
49 | - else { |
|
50 | - $id = count($this->data); |
|
51 | - $data->{$this->id[0]} = $id; |
|
52 | - } |
|
45 | + public function save($data) { |
|
46 | + if (isset($data->{$this->id[0]})) { |
|
47 | + $id = $data->{$this->id[0]}; |
|
48 | + } |
|
49 | + else { |
|
50 | + $id = count($this->data); |
|
51 | + $data->{$this->id[0]} = $id; |
|
52 | + } |
|
53 | 53 | |
54 | - $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
55 | - } |
|
54 | + $this->data[$id] = (object)array_merge($this->findById($id), (array)$data); |
|
55 | + } |
|
56 | 56 | |
57 | - public function getErrors() { |
|
58 | - return []; |
|
59 | - } |
|
57 | + public function getErrors() { |
|
58 | + return []; |
|
59 | + } |
|
60 | 60 | |
61 | - private function getOrderFunction($order, $columns) { |
|
62 | - return function($a, $b) use ($order, $columns) { |
|
63 | - foreach (explode(',', $columns) as $column) { |
|
64 | - $aColumn = $a->$column; |
|
65 | - $bColumn = $b->$column; |
|
66 | - if ($aColumn === $bColumn) { |
|
67 | - $sortVal = 0; |
|
68 | - continue; |
|
69 | - } |
|
70 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
71 | - break; |
|
72 | - } |
|
73 | - if ($order === 'desc') return -$sortVal; |
|
74 | - else return $sortVal; |
|
75 | - }; |
|
76 | - } |
|
61 | + private function getOrderFunction($order, $columns) { |
|
62 | + return function($a, $b) use ($order, $columns) { |
|
63 | + foreach (explode(',', $columns) as $column) { |
|
64 | + $aColumn = $a->$column; |
|
65 | + $bColumn = $b->$column; |
|
66 | + if ($aColumn === $bColumn) { |
|
67 | + $sortVal = 0; |
|
68 | + continue; |
|
69 | + } |
|
70 | + else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
71 | + break; |
|
72 | + } |
|
73 | + if ($order === 'desc') return -$sortVal; |
|
74 | + else return $sortVal; |
|
75 | + }; |
|
76 | + } |
|
77 | 77 | } |