@@ -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 | } |
@@ -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 | } |