@@ -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; |
@@ -24,8 +24,8 @@ discard block |
||
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | public function offsetGet($key) { |
| 27 | - $depth = $this->getDepth()+1; |
|
| 28 | - if (count($this->primaryKey)-1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
| 27 | + $depth = $this->getDepth() + 1; |
|
| 28 | + if (count($this->primaryKey) - 1 == $depth) return $this->mapper->filter([$this->primaryKey[$depth] => $key])->item(0); |
|
| 29 | 29 | else return new MultiPk($this->mapper, $key, $this->primaryKey, $this); |
| 30 | 30 | } |
| 31 | 31 | |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | |
| 45 | 45 | public function offsetUnset($key) { |
| 46 | 46 | $keys = $this->primaryKey; |
| 47 | - $this->mapper->filter([ array_pop($keys) => $key])->delete(); |
|
| 47 | + $this->mapper->filter([array_pop($keys) => $key])->delete(); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | public function offsetExists($key) { |
@@ -2,30 +2,30 @@ discard block |
||
| 2 | 2 | namespace Maphper\DataSource; |
| 3 | 3 | |
| 4 | 4 | class DatabaseCrud { |
| 5 | - private $crudBuilder; |
|
| 6 | - private $whereBuilder; |
|
| 7 | - private $adapter; |
|
| 8 | - private $databaseModify; |
|
| 9 | - private $databaseSelect; |
|
| 10 | - private $table; |
|
| 11 | - private $primaryKey; |
|
| 12 | - |
|
| 13 | - public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) { |
|
| 14 | - $this->adapter = $adapter; |
|
| 15 | - $this->databaseModify = $databaseModify; |
|
| 16 | - $this->databaseSelect = $databaseSelect; |
|
| 17 | - $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
|
| 18 | - $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 19 | - $this->table = $table; |
|
| 20 | - $this->primaryKey = $primaryKey; |
|
| 21 | - } |
|
| 22 | - |
|
| 23 | - public function deleteById($id) { |
|
| 24 | - $this->adapter->query($this->crudBuilder->delete($this->table, $this->primaryKey[0] . ' = :id', [':id' => $id])); |
|
| 25 | - $this->databaseSelect->deleteIDFromCache($id); |
|
| 5 | + private $crudBuilder; |
|
| 6 | + private $whereBuilder; |
|
| 7 | + private $adapter; |
|
| 8 | + private $databaseModify; |
|
| 9 | + private $databaseSelect; |
|
| 10 | + private $table; |
|
| 11 | + private $primaryKey; |
|
| 12 | + |
|
| 13 | + public function __construct(DatabaseAdapter $adapter, DatabaseModify $databaseModify, DatabaseSelect $databaseSelect, $table, $primaryKey) { |
|
| 14 | + $this->adapter = $adapter; |
|
| 15 | + $this->databaseModify = $databaseModify; |
|
| 16 | + $this->databaseSelect = $databaseSelect; |
|
| 17 | + $this->crudBuilder = new \Maphper\Lib\CrudBuilder(); |
|
| 18 | + $this->whereBuilder = new \Maphper\Lib\Sql\WhereBuilder(); |
|
| 19 | + $this->table = $table; |
|
| 20 | + $this->primaryKey = $primaryKey; |
|
| 26 | 21 | } |
| 27 | 22 | |
| 28 | - public function deleteByField(array $fields, array $options = []) { |
|
| 23 | + public function deleteById($id) { |
|
| 24 | + $this->adapter->query($this->crudBuilder->delete($this->table, $this->primaryKey[0] . ' = :id', [':id' => $id])); |
|
| 25 | + $this->databaseSelect->deleteIDFromCache($id); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + public function deleteByField(array $fields, array $options = []) { |
|
| 29 | 29 | $query = $this->whereBuilder->createSql($fields); |
| 30 | 30 | $this->adapter->query($this->crudBuilder->delete($this->table, $query['sql'], $query['args'], $options['limit'], null, $options['order'])); |
| 31 | 31 | $this->databaseModify->addIndex(array_keys($query['args'])); |
@@ -35,22 +35,22 @@ discard block |
||
| 35 | 35 | $this->databaseSelect->clearResultCache(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - private function getIfNew($data) { |
|
| 39 | - $new = false; |
|
| 40 | - foreach ($this->primaryKey as $k) { |
|
| 41 | - if (empty($data->$k)) { |
|
| 42 | - $data->$k = null; |
|
| 43 | - $new = true; |
|
| 44 | - } |
|
| 45 | - } |
|
| 46 | - return $new; |
|
| 47 | - } |
|
| 38 | + private function getIfNew($data) { |
|
| 39 | + $new = false; |
|
| 40 | + foreach ($this->primaryKey as $k) { |
|
| 41 | + if (empty($data->$k)) { |
|
| 42 | + $data->$k = null; |
|
| 43 | + $new = true; |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | + return $new; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - public function save($data, $tryagain = true) { |
|
| 50 | - $new = $this->getIfNew($data); |
|
| 49 | + public function save($data, $tryagain = true) { |
|
| 50 | + $new = $this->getIfNew($data); |
|
| 51 | 51 | |
| 52 | 52 | try { |
| 53 | - $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 53 | + $result = $this->insert($this->table, $this->primaryKey, $data); |
|
| 54 | 54 | |
| 55 | 55 | //If there was an error but PDO is silent, trigger the catch block anyway |
| 56 | 56 | if ($result->errorCode() !== '00000') throw new \Exception('Could not insert into ' . $this->table); |
@@ -68,15 +68,15 @@ discard block |
||
| 68 | 68 | $this->databaseSelect->updateCache($data, $data->{$this->primaryKey[0]}); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - private function updatePK($data, $new) { |
|
| 72 | - if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 73 | - } |
|
| 71 | + private function updatePK($data, $new) { |
|
| 72 | + if ($new && count($this->primaryKey) == 1) $data->{$this->primaryKey[0]} = $this->adapter->lastInsertId(); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - private function checkIfUpdateWorked($data) { |
|
| 76 | - $updateWhere = $this->whereBuilder->createSql($data); |
|
| 77 | - $matched = $this->databaseSelect->findByField($updateWhere['args']); |
|
| 78 | - if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 79 | - } |
|
| 75 | + private function checkIfUpdateWorked($data) { |
|
| 76 | + $updateWhere = $this->whereBuilder->createSql($data); |
|
| 77 | + $matched = $this->databaseSelect->findByField($updateWhere['args']); |
|
| 78 | + if (count($matched) == 0) throw new \InvalidArgumentException('Record inserted into table ' . $this->table . ' fails table constraints'); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | 81 | private function insert($table, array $primaryKey, $data) { |
| 82 | 82 | $error = 0; |
@@ -88,16 +88,16 @@ discard block |
||
| 88 | 88 | } |
| 89 | 89 | |
| 90 | 90 | if ($error || $result->errorCode() !== '00000') { |
| 91 | - $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 92 | - } |
|
| 91 | + $result = $this->tryUpdate($table, $primaryKey, $data); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | 94 | return $result; |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - private function tryUpdate($table, array $primaryKey, $data) { |
|
| 98 | - $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 99 | - if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 97 | + private function tryUpdate($table, array $primaryKey, $data) { |
|
| 98 | + $result = $this->adapter->query($this->crudBuilder->update($table, $primaryKey, $data)); |
|
| 99 | + if ($result->rowCount() === 0) $this->checkIfUpdateWorked($data); |
|
| 100 | 100 | |
| 101 | - return $result; |
|
| 102 | - } |
|
| 101 | + return $result; |
|
| 102 | + } |
|
| 103 | 103 | } |
@@ -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 | } |
@@ -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 | } |
@@ -17,7 +17,7 @@ |
||
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function optimizeColumns() { |
| 20 | - if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0,500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 20 | + if (Database::EDIT_OPTIMISE & $this->alterDb && rand(0, 500) == 1) $this->adapter->optimiseColumns($this->table); |
|
| 21 | 21 | } |
| 22 | 22 | |
| 23 | 23 | public function getTryInsertAgain($tryagain) { |
@@ -4,15 +4,15 @@ discard block |
||
| 4 | 4 | class SqliteAdapter implements DatabaseAdapter { |
| 5 | 5 | private $pdo; |
| 6 | 6 | private $stmtCache; |
| 7 | - private $generalEditor; |
|
| 7 | + private $generalEditor; |
|
| 8 | 8 | |
| 9 | 9 | public function __construct(\PDO $pdo) { |
| 10 | 10 | $this->pdo = $pdo; |
| 11 | - $this->stmtCache = new StmtCache($pdo); |
|
| 12 | - $this->generalEditor = new GeneralEditDatabase($this->pdo, [ |
|
| 13 | - 'int' => 'INTEGER', |
|
| 14 | - 'pk_default' => 'INTEGER NOT NULL', |
|
| 15 | - ]); |
|
| 11 | + $this->stmtCache = new StmtCache($pdo); |
|
| 12 | + $this->generalEditor = new GeneralEditDatabase($this->pdo, [ |
|
| 13 | + 'int' => 'INTEGER', |
|
| 14 | + 'pk_default' => 'INTEGER NOT NULL', |
|
| 15 | + ]); |
|
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | public function quote($str) { |
@@ -20,19 +20,19 @@ discard block |
||
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | public function query(\Maphper\Lib\Query $query) { |
| 23 | - $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
| 23 | + $stmt = $this->stmtCache->getCachedStmt($query->getSql()); |
|
| 24 | 24 | $args = $query->getArgs(); |
| 25 | 25 | |
| 26 | - //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
| 27 | - if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 26 | + //Handle SQLite when PDO_ERRMODE is set to SILENT |
|
| 27 | + if ($stmt === false) throw new \Exception('Invalid query'); |
|
| 28 | 28 | |
| 29 | - $stmt->execute($args); |
|
| 30 | - if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
| 29 | + $stmt->execute($args); |
|
| 30 | + if ($stmt->errorCode() !== '00000' && $stmt->errorInfo()[2] == 'database schema has changed') { |
|
| 31 | 31 | $this->stmtCache->deleteQueryFromCache($query->getSql()); |
| 32 | 32 | return $this->query($query); |
| 33 | - } |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - return $stmt; |
|
| 35 | + return $stmt; |
|
| 36 | 36 | } |
| 37 | 37 | |
| 38 | 38 | public function lastInsertId() { |
@@ -59,11 +59,11 @@ discard block |
||
| 59 | 59 | // SQLSTATE[HY000]: General error: 17 database schema has changed |
| 60 | 60 | $this->stmtCache->clearCache(); |
| 61 | 61 | |
| 62 | - // Create temp table to create a new structure |
|
| 62 | + // Create temp table to create a new structure |
|
| 63 | 63 | $affix = '_'.substr(md5($table), 0, 6); |
| 64 | - $tempTable = $table . $affix; |
|
| 64 | + $tempTable = $table . $affix; |
|
| 65 | 65 | $this->generalEditor->createTable($tempTable, $primaryKey, $data); |
| 66 | - $this->alterColumns($tempTable, $primaryKey, $data); |
|
| 66 | + $this->alterColumns($tempTable, $primaryKey, $data); |
|
| 67 | 67 | $this->copyTableData($table, $tempTable); |
| 68 | 68 | |
| 69 | 69 | $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
@@ -71,8 +71,8 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - private function copyTableData($tableFrom, $tableTo) { |
|
| 75 | - try { |
|
| 74 | + private function copyTableData($tableFrom, $tableTo) { |
|
| 75 | + try { |
|
| 76 | 76 | if ($this->tableExists($tableFrom)) { |
| 77 | 77 | $columns = implode(', ', $this->getColumns($tableFrom)); |
| 78 | 78 | |
@@ -83,19 +83,19 @@ discard block |
||
| 83 | 83 | // No data to copy |
| 84 | 84 | echo $e->getMessage(); |
| 85 | 85 | } |
| 86 | - } |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - private function alterColumns($table, array $primaryKey, $data) { |
|
| 89 | - foreach ($data as $key => $value) { |
|
| 88 | + private function alterColumns($table, array $primaryKey, $data) { |
|
| 89 | + foreach ($data as $key => $value) { |
|
| 90 | 90 | if ($this->generalEditor->isNotSavableType($value, $key, $primaryKey)) continue; |
| 91 | 91 | |
| 92 | 92 | $type = $this->generalEditor->getType($value); |
| 93 | 93 | |
| 94 | 94 | $this->pdo->query('ALTER TABLE ' . $table . ' ADD ' . $this->quote($key) . ' ' . $type); |
| 95 | 95 | } |
| 96 | - } |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - public function addIndex($table, array $fields) { |
|
| 98 | + public function addIndex($table, array $fields) { |
|
| 99 | 99 | if (empty($fields)) return false; |
| 100 | 100 | |
| 101 | 101 | //SQLite doesn't support ASC/DESC indexes, remove the keywords |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | private function tableExists($name) { |
| 43 | - $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="'. $name.'"'); |
|
| 43 | + $result = $this->pdo->query('SELECT name FROM sqlite_master WHERE type="table" and name="' . $name . '"'); |
|
| 44 | 44 | return count($result->fetchAll()) == 1; |
| 45 | 45 | } |
| 46 | 46 | |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | $this->stmtCache->clearCache(); |
| 61 | 61 | |
| 62 | 62 | // Create temp table to create a new structure |
| 63 | - $affix = '_'.substr(md5($table), 0, 6); |
|
| 63 | + $affix = '_' . substr(md5($table), 0, 6); |
|
| 64 | 64 | $tempTable = $table . $affix; |
| 65 | 65 | $this->generalEditor->createTable($tempTable, $primaryKey, $data); |
| 66 | 66 | $this->alterColumns($tempTable, $primaryKey, $data); |
| 67 | 67 | $this->copyTableData($table, $tempTable); |
| 68 | 68 | |
| 69 | - $this->pdo->query('DROP TABLE IF EXISTS ' . $table ); |
|
| 70 | - $this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO '. $table ); |
|
| 69 | + $this->pdo->query('DROP TABLE IF EXISTS ' . $table); |
|
| 70 | + $this->pdo->query('ALTER TABLE ' . $tempTable . ' RENAME TO ' . $table); |
|
| 71 | 71 | |
| 72 | 72 | } |
| 73 | 73 | |
@@ -1,19 +1,19 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace Maphper; |
| 3 | 3 | class Maphper implements \Countable, \ArrayAccess, \IteratorAggregate { |
| 4 | - const FIND_EXACT = 0x1; |
|
| 5 | - const FIND_LIKE = 0x2; |
|
| 6 | - const FIND_STARTS = 0x4; |
|
| 7 | - const FIND_ENDS = 0x8; |
|
| 8 | - const FIND_BIT = 0x10; |
|
| 9 | - const FIND_GREATER = 0x20; |
|
| 10 | - const FIND_LESS = 0x40; |
|
| 11 | - const FIND_EXPRESSION = 0x80; |
|
| 12 | - const FIND_AND = 0x100; |
|
| 13 | - const FIND_OR = 0x200; |
|
| 14 | - const FIND_NOT = 0x400; |
|
| 15 | - const FIND_BETWEEN = 0x800; |
|
| 16 | - const FIND_NOCASE = 0x1000; |
|
| 4 | + const FIND_EXACT = 0x1; |
|
| 5 | + const FIND_LIKE = 0x2; |
|
| 6 | + const FIND_STARTS = 0x4; |
|
| 7 | + const FIND_ENDS = 0x8; |
|
| 8 | + const FIND_BIT = 0x10; |
|
| 9 | + const FIND_GREATER = 0x20; |
|
| 10 | + const FIND_LESS = 0x40; |
|
| 11 | + const FIND_EXPRESSION = 0x80; |
|
| 12 | + const FIND_AND = 0x100; |
|
| 13 | + const FIND_OR = 0x200; |
|
| 14 | + const FIND_NOT = 0x400; |
|
| 15 | + const FIND_BETWEEN = 0x800; |
|
| 16 | + const FIND_NOCASE = 0x1000; |
|
| 17 | 17 | |
| 18 | 18 | private $dataSource; |
| 19 | 19 | private $relations = []; |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | $results = $this->dataSource->findByField($this->settings['filter'], |
| 60 | - ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset'] ]); |
|
| 60 | + ['order' => $this->settings['sort'], 'limit' => $this->settings['limit'], 'offset' => $this->settings['offset']]); |
|
| 61 | 61 | |
| 62 | 62 | $siblings = new \ArrayObject(); |
| 63 | 63 | |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 109 | 109 | return isset($data[0]); |
| 110 | 110 | } |
| 111 | - return (bool) $this->dataSource->findById($offset); |
|
| 111 | + return (bool)$this->dataSource->findById($offset); |
|
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | public function offsetUnset($id) { |
@@ -83,21 +83,21 @@ discard block |
||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | public function offsetSet($offset, $valueObj) { |
| 86 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
| 86 | + if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
| 87 | 87 | |
| 88 | - //Extract private properties from the object |
|
| 89 | - $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
|
| 90 | - $value = $visibilityOverride->getProperties($valueObj); |
|
| 88 | + //Extract private properties from the object |
|
| 89 | + $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
|
| 90 | + $value = $visibilityOverride->getProperties($valueObj); |
|
| 91 | 91 | |
| 92 | - $value = $this->processFilters($value); |
|
| 93 | - $pk = $this->dataSource->getPrimaryKey(); |
|
| 94 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
| 95 | - $valueCopy = $this->removeRelations(clone $value, $pk); |
|
| 96 | - $value = $this->entity->wrap($this->relations, $value); |
|
| 97 | - $this->dataSource->save($value); |
|
| 98 | - $visibilityOverride->write($value); |
|
| 99 | - $value = $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 100 | - $visibilityOverride->write($value); |
|
| 92 | + $value = $this->processFilters($value); |
|
| 93 | + $pk = $this->dataSource->getPrimaryKey(); |
|
| 94 | + if ($offset !== null) $value->{$pk[0]} = $offset; |
|
| 95 | + $valueCopy = $this->removeRelations(clone $value, $pk); |
|
| 96 | + $value = $this->entity->wrap($this->relations, $value); |
|
| 97 | + $this->dataSource->save($value); |
|
| 98 | + $visibilityOverride->write($value); |
|
| 99 | + $value = $this->entity->create((array_merge((array)$value, (array)$valueCopy)), $this->relations); |
|
| 100 | + $visibilityOverride->write($value); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
@@ -110,10 +110,10 @@ discard block |
||
| 110 | 110 | |
| 111 | 111 | public function offsetExists($offset) { |
| 112 | 112 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 113 | - if (!empty($this->settings['filter'])) { |
|
| 114 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 115 | - return isset($data[0]); |
|
| 116 | - } |
|
| 113 | + if (!empty($this->settings['filter'])) { |
|
| 114 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 115 | + return isset($data[0]); |
|
| 116 | + } |
|
| 117 | 117 | return (bool) $this->dataSource->findById($offset); |
| 118 | 118 | } |
| 119 | 119 | |
@@ -123,10 +123,10 @@ discard block |
||
| 123 | 123 | |
| 124 | 124 | public function offsetGet($offset) { |
| 125 | 125 | if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
| 126 | - if (!empty($this->settings['filter'])) { |
|
| 127 | - $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 128 | - return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 129 | - } |
|
| 126 | + if (!empty($this->settings['filter'])) { |
|
| 127 | + $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
|
| 128 | + return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
|
| 129 | + } |
|
| 130 | 130 | return $this->entity->create($this->dataSource->findById($offset), $this->relations); |
| 131 | 131 | } |
| 132 | 132 | |
@@ -7,17 +7,17 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | public function __construct($object) { |
| 9 | 9 | if ($object instanceof \stdclass) { |
| 10 | - $this->readClosure = function() use ($object) { return $object; }; |
|
| 11 | - $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
|
| 10 | + $this->readClosure = function() use ($object) { return $object; }; |
|
| 11 | + $this->writeClosure = function($field, $value) use ($object) { $object->$field = $value; }; |
|
| 12 | 12 | } |
| 13 | 13 | else { |
| 14 | 14 | $visOverride = $this; |
| 15 | 15 | $this->readClosure = function() use ($visOverride) { |
| 16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
| 16 | + return (object)array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
| 17 | 17 | }; |
| 18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
| 19 | 19 | |
| 20 | - $this->writeClosure = function ($field, $value) { $this->$field = $value; }; |
|
| 20 | + $this->writeClosure = function($field, $value) { $this->$field = $value; }; |
|
| 21 | 21 | $this->writeClosure = $this->writeClosure->bindTo($object, $object); |
| 22 | 22 | } |
| 23 | 23 | |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | public function write($data) { |
| 35 | 35 | if ($data != null) { |
| 36 | 36 | foreach ($data as $key => $value) { |
| 37 | - ($this->writeClosure)($key, $this->processDates($value)); |
|
| 37 | + ($this->writeClosure)($key, $this->processDates($value)); |
|
| 38 | 38 | } |
| 39 | 39 | } |
| 40 | 40 | } |
@@ -11,9 +11,9 @@ discard block |
||
| 11 | 11 | $this->writeClosure = function ($field, $value) use ($object) { $object->$field = $value; }; |
| 12 | 12 | } |
| 13 | 13 | else { |
| 14 | - $visOverride = $this; |
|
| 14 | + $visOverride = $this; |
|
| 15 | 15 | $this->readClosure = function() use ($visOverride) { |
| 16 | - return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
| 16 | + return (object) array_filter(get_object_vars($this), [$visOverride, 'isReturnableDataType']); |
|
| 17 | 17 | }; |
| 18 | 18 | $this->readClosure = $this->readClosure->bindTo($object, $object); |
| 19 | 19 | |
@@ -23,9 +23,9 @@ discard block |
||
| 23 | 23 | |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - public function isReturnableDataType($v) { |
|
| 27 | - return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface); |
|
| 28 | - } |
|
| 26 | + public function isReturnableDataType($v) { |
|
| 27 | + return is_scalar($v) || is_null($v) || (is_object($v) && $v instanceof \DateTimeInterface); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | public function getProperties() { |
| 31 | 31 | return ($this->readClosure)(); |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | private $parent; |
| 9 | 9 | |
| 10 | 10 | public function __construct(\Maphper\Maphper $parent, $className = null) { |
| 11 | - $this->parent = $parent; |
|
| 11 | + $this->parent = $parent; |
|
| 12 | 12 | $this->className = $className; |
| 13 | 13 | } |
| 14 | 14 | |
@@ -25,12 +25,12 @@ discard block |
||
| 25 | 25 | return $object; |
| 26 | 26 | } |
| 27 | 27 | |
| 28 | - private function addRelationData($object, $name, $relation, $siblings) { |
|
| 29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
| 30 | - //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
| 31 | - if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
| 32 | - } |
|
| 28 | + private function addRelationData($object, $name, $relation, $siblings) { |
|
| 29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
| 30 | + //After overwriting the relation, does the parent object ($object) need overwriting as well? |
|
| 31 | + if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - $object->$name = $relation->getData($object, $siblings); |
|
| 35 | - } |
|
| 34 | + $object->$name = $relation->getData($object, $siblings); |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -26,7 +26,7 @@ |
||
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | private function addRelationData($object, $name, $relation, $siblings) { |
| 29 | - if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation) ) { |
|
| 29 | + if (isset($object->$name) && !($object->$name instanceof \Maphper\Relation)) { |
|
| 30 | 30 | //After overwriting the relation, does the parent object ($object) need overwriting as well? |
| 31 | 31 | if ($relation->overwrite($object, $object->$name)) $this->parent[] = $object; |
| 32 | 32 | } |
@@ -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 | |
@@ -20,11 +20,11 @@ discard block |
||
| 20 | 20 | //For dates with times set, search on time, if the time is not set, search on date only. |
| 21 | 21 | //E.g. searching for all records posted on '2015-11-14' should return all records that day, not just the ones posted at 00:00:00 on that day |
| 22 | 22 | if ($value instanceof \DateTimeInterface) { |
| 23 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 23 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 24 | 24 | else $value = $value->format('Y-m-d H:i:s'); |
| 25 | 25 | } |
| 26 | 26 | if (is_object($value)) continue; |
| 27 | - if ($prependField){ |
|
| 27 | + if ($prependField) { |
|
| 28 | 28 | $sql[] = $this->quote($field) . ' = :' . $field; |
| 29 | 29 | } else { |
| 30 | 30 | $sql[] = ':' . $field; |
@@ -36,13 +36,13 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | public function insert($table, $data) { |
| 38 | 38 | $query = $this->buildSaveQuery($data); |
| 39 | - return new Query('INSERT INTO ' . $this->quote($table) . ' (' .implode(', ', array_keys($query['args'])).') VALUES ( ' . implode(', ', $query['sql']). ' )', $query['args']); |
|
| 39 | + return new Query('INSERT INTO ' . $this->quote($table) . ' (' . implode(', ', array_keys($query['args'])) . ') VALUES ( ' . implode(', ', $query['sql']) . ' )', $query['args']); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | public function update($table, array $primaryKey, $data) { |
| 43 | 43 | $query = $this->buildSaveQuery($data, true); |
| 44 | 44 | $where = []; |
| 45 | - foreach($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
| 46 | - return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']). ' WHERE '. implode(' AND ', $where), $query['args']); |
|
| 45 | + foreach ($primaryKey as $field) $where[] = $this->quote($field) . ' = :' . $field; |
|
| 46 | + return new Query('UPDATE ' . $this->quote($table) . ' SET ' . implode(', ', $query['sql']) . ' WHERE ' . implode(' AND ', $where), $query['args']); |
|
| 47 | 47 | } |
| 48 | 48 | } |