@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | private $siblings = []; |
| 10 | 10 | |
| 11 | 11 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $criteria = []) { |
| 12 | - if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
| 12 | + if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
| 13 | 13 | $this->mapper = $mapper; |
| 14 | 14 | $this->parentField = $parentField; |
| 15 | 15 | $this->localField = $localField; |
@@ -48,11 +48,11 @@ discard block |
||
| 48 | 48 | //Fetch the results so they're in the cache for the corresponding maphper object |
| 49 | 49 | $results = $this->mapper->filter([$this->localField => $recordsToLoad]); |
| 50 | 50 | |
| 51 | - $this->loadDataIntoSiblings($results); |
|
| 51 | + $this->loadDataIntoSiblings($results); |
|
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - private function loadDataIntoSiblings($results) { |
|
| 55 | - $cache = []; |
|
| 54 | + private function loadDataIntoSiblings($results) { |
|
| 55 | + $cache = []; |
|
| 56 | 56 | foreach ($results as $result) { |
| 57 | 57 | $cache[$result->{$this->localField}] = $result; |
| 58 | 58 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | else $sibling->data = $sibling->mapper->filter([$sibling->localField => $sibling->parentObject->{$this->parentField}])->item(0); |
| 67 | 67 | } |
| 68 | 68 | */ |
| 69 | - } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | 71 | public function __call($func, array $args = []) { |
| 72 | 72 | if ($this->lazyLoad() == null) return ''; |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | |
| 76 | 76 | public function __get($name) { |
| 77 | 77 | if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
| 78 | - else return null; |
|
| 78 | + else return null; |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | public function __isset($name) { |
@@ -83,9 +83,9 @@ discard block |
||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | public function overwrite($parentObject, &$data) { |
| 86 | - $this->mapper[] = $data; |
|
| 86 | + $this->mapper[] = $data; |
|
| 87 | 87 | |
| 88 | - if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
| 88 | + if (!isset($parentObject->{$this->parentField}) || $parentObject->{$this->parentField} != $data->{$this->localField}) { |
|
| 89 | 89 | $parentObject->{$this->parentField} = $data->{$this->localField}; |
| 90 | 90 | //Trigger an update of the parent object |
| 91 | 91 | return true; |
@@ -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 | } |
@@ -2,87 +2,87 @@ |
||
| 2 | 2 | namespace Maphper\Lib\Sql; |
| 3 | 3 | |
| 4 | 4 | class WhereBuilder { |
| 5 | - private $conditionals = []; |
|
| 6 | - |
|
| 7 | - public function __construct() { |
|
| 8 | - $defaultConditionals = [ |
|
| 9 | - 'Maphper\Lib\Sql\Between', |
|
| 10 | - 'Maphper\Lib\Sql\In', |
|
| 11 | - 'Maphper\Lib\Sql\NullConditional', |
|
| 12 | - 'Maphper\Lib\Sql\Like', |
|
| 13 | - 'Maphper\Lib\Sql\GeneralOperator' |
|
| 14 | - ]; |
|
| 15 | - |
|
| 16 | - foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
| 17 | - } |
|
| 5 | + private $conditionals = []; |
|
| 6 | + |
|
| 7 | + public function __construct() { |
|
| 8 | + $defaultConditionals = [ |
|
| 9 | + 'Maphper\Lib\Sql\Between', |
|
| 10 | + 'Maphper\Lib\Sql\In', |
|
| 11 | + 'Maphper\Lib\Sql\NullConditional', |
|
| 12 | + 'Maphper\Lib\Sql\Like', |
|
| 13 | + 'Maphper\Lib\Sql\GeneralOperator' |
|
| 14 | + ]; |
|
| 15 | + |
|
| 16 | + foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - public function addConditional(WhereConditional $conditional) { |
|
| 20 | - $this->conditionals[] = $conditional; |
|
| 21 | - } |
|
| 19 | + public function addConditional(WhereConditional $conditional) { |
|
| 20 | + $this->conditionals[] = $conditional; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | 23 | public function createSql($fields, $mode = \Maphper\Maphper::FIND_EXACT | \Maphper\Maphper::FIND_AND) { |
| 24 | 24 | $args = []; |
| 25 | 25 | $sql = []; |
| 26 | 26 | |
| 27 | - foreach ($fields as $key => $value) { |
|
| 28 | - $value = $this->convertDates($value); |
|
| 27 | + foreach ($fields as $key => $value) { |
|
| 28 | + $value = $this->convertDates($value); |
|
| 29 | 29 | |
| 30 | - if (is_object($value)) continue; |
|
| 30 | + if (is_object($value)) continue; |
|
| 31 | 31 | $result = $this->getResult($key, $value, $mode); |
| 32 | - $result = $this->fixDuplicateArgs($args, $result); |
|
| 33 | - $sql = array_merge($sql, (array)$result['sql']); |
|
| 34 | - $args = array_merge($args, $result['args']); |
|
| 35 | - } |
|
| 32 | + $result = $this->fixDuplicateArgs($args, $result); |
|
| 33 | + $sql = array_merge($sql, (array)$result['sql']); |
|
| 34 | + $args = array_merge($args, $result['args']); |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | return ['args' => $args, 'sql' => $this->sqlArrayToString($sql, $mode)]; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - // Returns result with duplicate issues removed |
|
| 41 | - private function fixDuplicateArgs($origArgs, $result) { |
|
| 42 | - $duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args |
|
| 43 | - if (count($duplicates) === 0) return $result; |
|
| 40 | + // Returns result with duplicate issues removed |
|
| 41 | + private function fixDuplicateArgs($origArgs, $result) { |
|
| 42 | + $duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args |
|
| 43 | + if (count($duplicates) === 0) return $result; |
|
| 44 | 44 | |
| 45 | - foreach ($duplicates as $argKey => $argVal) { |
|
| 46 | - $valHash = substr(md5($argVal), 0, 5); |
|
| 47 | - $newKey = $argKey . $valHash; |
|
| 45 | + foreach ($duplicates as $argKey => $argVal) { |
|
| 46 | + $valHash = substr(md5($argVal), 0, 5); |
|
| 47 | + $newKey = $argKey . $valHash; |
|
| 48 | 48 | |
| 49 | - // Replace occurences of duplicate key with key + hash as arg |
|
| 50 | - $result['sql'] = str_replace(':' . $argKey, ':' . $newKey, $result['sql']); |
|
| 51 | - unset($result['args'][$argKey]); |
|
| 52 | - $result['args'][$newKey] = $argVal; |
|
| 53 | - } |
|
| 49 | + // Replace occurences of duplicate key with key + hash as arg |
|
| 50 | + $result['sql'] = str_replace(':' . $argKey, ':' . $newKey, $result['sql']); |
|
| 51 | + unset($result['args'][$argKey]); |
|
| 52 | + $result['args'][$newKey] = $argVal; |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - return $result; |
|
| 56 | - } |
|
| 55 | + return $result; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /* |
|
| 58 | + /* |
|
| 59 | 59 | * Either get sql from a conditional or call createSql again because the mode needs to be changed |
| 60 | 60 | */ |
| 61 | - private function getResult($key, $value, $mode) { |
|
| 62 | - if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
| 63 | - return $this->getConditional($key, $value, $mode); |
|
| 64 | - } |
|
| 61 | + private function getResult($key, $value, $mode) { |
|
| 62 | + if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
| 63 | + return $this->getConditional($key, $value, $mode); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - private function sqlArrayToString($sql, $mode) { |
|
| 67 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
| 66 | + private function sqlArrayToString($sql, $mode) { |
|
| 67 | + if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
| 68 | 68 | else $query = implode(' AND ', $sql); |
| 69 | 69 | if (!empty($query)) $query = '(' . $query . ')'; |
| 70 | - return $query; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - private function getConditional($key, $value, $mode) { |
|
| 74 | - foreach ($this->conditionals as $conditional) { |
|
| 75 | - if ($conditional->matches($key, $value, $mode)) |
|
| 76 | - return $conditional->getSql($key, $value, $mode); |
|
| 77 | - } |
|
| 78 | - throw new \Exception("Invalid WHERE query"); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - private function convertDates($value) { |
|
| 82 | - if ($value instanceof \DateTimeInterface) { |
|
| 83 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 84 | - else $value = $value->format('Y-m-d H:i:s'); |
|
| 85 | - } |
|
| 86 | - return $value; |
|
| 87 | - } |
|
| 70 | + return $query; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + private function getConditional($key, $value, $mode) { |
|
| 74 | + foreach ($this->conditionals as $conditional) { |
|
| 75 | + if ($conditional->matches($key, $value, $mode)) |
|
| 76 | + return $conditional->getSql($key, $value, $mode); |
|
| 77 | + } |
|
| 78 | + throw new \Exception("Invalid WHERE query"); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + private function convertDates($value) { |
|
| 82 | + if ($value instanceof \DateTimeInterface) { |
|
| 83 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 84 | + else $value = $value->format('Y-m-d H:i:s'); |
|
| 85 | + } |
|
| 86 | + return $value; |
|
| 87 | + } |
|
| 88 | 88 | } |
@@ -80,7 +80,7 @@ |
||
| 80 | 80 | |
| 81 | 81 | private function convertDates($value) { |
| 82 | 82 | if ($value instanceof \DateTimeInterface) { |
| 83 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 83 | + if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 84 | 84 | else $value = $value->format('Y-m-d H:i:s'); |
| 85 | 85 | } |
| 86 | 86 | return $value; |
@@ -13,7 +13,9 @@ discard block |
||
| 13 | 13 | 'Maphper\Lib\Sql\GeneralOperator' |
| 14 | 14 | ]; |
| 15 | 15 | |
| 16 | - foreach ($defaultConditionals as $conditional) $this->addConditional(new $conditional); |
|
| 16 | + foreach ($defaultConditionals as $conditional) { |
|
| 17 | + $this->addConditional(new $conditional); |
|
| 18 | + } |
|
| 17 | 19 | } |
| 18 | 20 | |
| 19 | 21 | public function addConditional(WhereConditional $conditional) { |
@@ -27,7 +29,9 @@ discard block |
||
| 27 | 29 | foreach ($fields as $key => $value) { |
| 28 | 30 | $value = $this->convertDates($value); |
| 29 | 31 | |
| 30 | - if (is_object($value)) continue; |
|
| 32 | + if (is_object($value)) { |
|
| 33 | + continue; |
|
| 34 | + } |
|
| 31 | 35 | $result = $this->getResult($key, $value, $mode); |
| 32 | 36 | $result = $this->fixDuplicateArgs($args, $result); |
| 33 | 37 | $sql = array_merge($sql, (array)$result['sql']); |
@@ -40,7 +44,9 @@ discard block |
||
| 40 | 44 | // Returns result with duplicate issues removed |
| 41 | 45 | private function fixDuplicateArgs($origArgs, $result) { |
| 42 | 46 | $duplicates = array_intersect_key($result['args'], $origArgs); // Holds all keys in results already in the args |
| 43 | - if (count($duplicates) === 0) return $result; |
|
| 47 | + if (count($duplicates) === 0) { |
|
| 48 | + return $result; |
|
| 49 | + } |
|
| 44 | 50 | |
| 45 | 51 | foreach ($duplicates as $argKey => $argVal) { |
| 46 | 52 | $valHash = substr(md5($argVal), 0, 5); |
@@ -59,29 +65,40 @@ discard block |
||
| 59 | 65 | * Either get sql from a conditional or call createSql again because the mode needs to be changed |
| 60 | 66 | */ |
| 61 | 67 | private function getResult($key, $value, $mode) { |
| 62 | - if (is_numeric($key) && is_array($value)) return $this->createSql($value, $key); |
|
| 68 | + if (is_numeric($key) && is_array($value)) { |
|
| 69 | + return $this->createSql($value, $key); |
|
| 70 | + } |
|
| 63 | 71 | return $this->getConditional($key, $value, $mode); |
| 64 | 72 | } |
| 65 | 73 | |
| 66 | 74 | private function sqlArrayToString($sql, $mode) { |
| 67 | - if (\Maphper\Maphper::FIND_OR & $mode) $query = implode(' OR ', $sql); |
|
| 68 | - else $query = implode(' AND ', $sql); |
|
| 69 | - if (!empty($query)) $query = '(' . $query . ')'; |
|
| 75 | + if (\Maphper\Maphper::FIND_OR & $mode) { |
|
| 76 | + $query = implode(' OR ', $sql); |
|
| 77 | + } else { |
|
| 78 | + $query = implode(' AND ', $sql); |
|
| 79 | + } |
|
| 80 | + if (!empty($query)) { |
|
| 81 | + $query = '(' . $query . ')'; |
|
| 82 | + } |
|
| 70 | 83 | return $query; |
| 71 | 84 | } |
| 72 | 85 | |
| 73 | 86 | private function getConditional($key, $value, $mode) { |
| 74 | 87 | foreach ($this->conditionals as $conditional) { |
| 75 | - if ($conditional->matches($key, $value, $mode)) |
|
| 76 | - return $conditional->getSql($key, $value, $mode); |
|
| 88 | + if ($conditional->matches($key, $value, $mode)) { |
|
| 89 | + return $conditional->getSql($key, $value, $mode); |
|
| 90 | + } |
|
| 77 | 91 | } |
| 78 | 92 | throw new \Exception("Invalid WHERE query"); |
| 79 | 93 | } |
| 80 | 94 | |
| 81 | 95 | private function convertDates($value) { |
| 82 | 96 | if ($value instanceof \DateTimeInterface) { |
| 83 | - if ($value->format('H:i:s') == '00:00:00') $value = $value->format('Y-m-d'); |
|
| 84 | - else $value = $value->format('Y-m-d H:i:s'); |
|
| 97 | + if ($value->format('H:i:s') == '00:00:00') { |
|
| 98 | + $value = $value->format('Y-m-d'); |
|
| 99 | + } else { |
|
| 100 | + $value = $value->format('Y-m-d H:i:s'); |
|
| 101 | + } |
|
| 85 | 102 | } |
| 86 | 103 | return $value; |
| 87 | 104 | } |
@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | return $obj; |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - private function tryToGetDateObjFromString($obj) { |
|
| 22 | - try { |
|
| 23 | - $date = new \DateTime($obj); |
|
| 21 | + private function tryToGetDateObjFromString($obj) { |
|
| 22 | + try { |
|
| 23 | + $date = new \DateTime($obj); |
|
| 24 | 24 | if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
| 25 | - } |
|
| 26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 27 | - } |
|
| 28 | - return $obj; |
|
| 29 | - } |
|
| 25 | + } |
|
| 26 | + catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 27 | + } |
|
| 28 | + return $obj; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | 31 | private function dateMatchesFormats($date, $str) { |
| 32 | 32 | foreach ($this->dateFormats as list($format, $len)) { |
@@ -35,17 +35,17 @@ discard block |
||
| 35 | 35 | return false; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - private function isIterable($obj) { |
|
| 39 | - return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
| 40 | - } |
|
| 38 | + private function isIterable($obj) { |
|
| 39 | + return is_array($obj) || (is_object($obj) && ($obj instanceof \Iterator)); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - private function isPossiblyDateString($obj) { |
|
| 43 | - return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
| 44 | - } |
|
| 42 | + private function isPossiblyDateString($obj) { |
|
| 43 | + return is_string($obj) && isset($obj[0]) && is_numeric($obj[0]) && strlen($obj) <= 20; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | 46 | private function checkCache($obj, $reset) { |
| 47 | 47 | if ($reset) $this->processCache = new \SplObjectStorage(); |
| 48 | - if (!is_object($obj)) return false; |
|
| 48 | + if (!is_object($obj)) return false; |
|
| 49 | 49 | |
| 50 | 50 | if ($this->processCache->contains($obj)) return $obj; |
| 51 | 51 | else $this->processCache->attach($obj, true); |
@@ -11,26 +11,35 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | public function replaceDates($obj, $reset = true) { |
| 13 | 13 | //prevent infinite recursion, only process each object once |
| 14 | - if ($this->checkCache($obj, $reset)) return $obj; |
|
| 14 | + if ($this->checkCache($obj, $reset)) { |
|
| 15 | + return $obj; |
|
| 16 | + } |
|
| 15 | 17 | |
| 16 | - if ($this->isIterable($obj)) foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
| 17 | - if ($this->isPossiblyDateString($obj)) $obj = $this->tryToGetDateObjFromString($obj); |
|
| 18 | + if ($this->isIterable($obj)) { |
|
| 19 | + foreach ($obj as &$o) $o = $this->replaceDates($o, false); |
|
| 20 | + } |
|
| 21 | + if ($this->isPossiblyDateString($obj)) { |
|
| 22 | + $obj = $this->tryToGetDateObjFromString($obj); |
|
| 23 | + } |
|
| 18 | 24 | return $obj; |
| 19 | 25 | } |
| 20 | 26 | |
| 21 | 27 | private function tryToGetDateObjFromString($obj) { |
| 22 | 28 | try { |
| 23 | 29 | $date = new \DateTime($obj); |
| 24 | - if ($this->dateMatchesFormats($date, $obj)) $obj = $date; |
|
| 25 | - } |
|
| 26 | - catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 30 | + if ($this->dateMatchesFormats($date, $obj)) { |
|
| 31 | + $obj = $date; |
|
| 32 | + } |
|
| 33 | + } catch (\Exception $e) { //Doesn't need to do anything as the try/catch is working out whether $obj is a date |
|
| 27 | 34 | } |
| 28 | 35 | return $obj; |
| 29 | 36 | } |
| 30 | 37 | |
| 31 | 38 | private function dateMatchesFormats($date, $str) { |
| 32 | 39 | foreach ($this->dateFormats as list($format, $len)) { |
| 33 | - if ($date->format($format) == substr($str, 0, $len)) return true; |
|
| 40 | + if ($date->format($format) == substr($str, 0, $len)) { |
|
| 41 | + return true; |
|
| 42 | + } |
|
| 34 | 43 | } |
| 35 | 44 | return false; |
| 36 | 45 | } |
@@ -44,11 +53,18 @@ discard block |
||
| 44 | 53 | } |
| 45 | 54 | |
| 46 | 55 | private function checkCache($obj, $reset) { |
| 47 | - if ($reset) $this->processCache = new \SplObjectStorage(); |
|
| 48 | - if (!is_object($obj)) return false; |
|
| 56 | + if ($reset) { |
|
| 57 | + $this->processCache = new \SplObjectStorage(); |
|
| 58 | + } |
|
| 59 | + if (!is_object($obj)) { |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 49 | 62 | |
| 50 | - if ($this->processCache->contains($obj)) return $obj; |
|
| 51 | - else $this->processCache->attach($obj, true); |
|
| 63 | + if ($this->processCache->contains($obj)) { |
|
| 64 | + return $obj; |
|
| 65 | + } else { |
|
| 66 | + $this->processCache->attach($obj, true); |
|
| 67 | + } |
|
| 52 | 68 | |
| 53 | 69 | return false; |
| 54 | 70 | } |