diff --git a/src/Search.php b/src/Search.php index ef15f69..17066d7 100644 --- a/src/Search.php +++ b/src/Search.php @@ -18,6 +18,7 @@ class Search { 'find' => '', 'searchGroup' => '', 'from' => null, + 'limit' => null, ]; /** @@ -47,6 +48,11 @@ class Search { */ protected $from = ''; + /** + * @var int + */ + protected $limit = null; + /** * HTTP Client Instance * @see \GuzzleHttp\Client @@ -93,6 +99,7 @@ public function resetComponents() { 'find' => '', 'searchGroup' => '', 'from' => null, + 'limit' => null, ]; } @@ -189,6 +196,10 @@ public function compiled() { . 'RETURNING ' . $this->components['from'] . '(' . $this->components['select'] . ')'; + if ($this->components['limit']) { + $query .= ' LIMIT ' . $this->components['limit']; + } + return $query; } /** @@ -200,7 +211,7 @@ public function compiled() { public function escape($string) { $search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a"); - $replace = array("\\\\","\\0","\\n", "\\r", "\\'", '\"', "\\Z"); + $replace = array("\\","\\0","\\n", "\\r", "\\'", '\"', "\\Z"); return str_replace($search, $replace, $string); } @@ -254,6 +265,11 @@ public function from($from) { return $this; } + public function limit($limit) { + $this->components['limit'] = (int)$limit; + return $this; + } + public function in($searchGroup) { $searchGroup = strtoupper($searchGroup); if (!in_array($searchGroup, ['ALL', 'NAME', 'EMAIL', 'PHONE', 'SIDEBAR'])) { @@ -285,6 +301,7 @@ public function find($value_or_array_or_callable, $join = 'and') { if (is_string($value_or_array_or_callable) || is_numeric($value_or_array_or_callable)) { $value = trim($value_or_array_or_callable); + $value = $this->escapeReservedCharacters($value); // Check if there's already a where clause created, unless we're at the start of a group if ($this->components['find'] && substr($this->components['find'], -1) !== '(') { @@ -342,4 +359,17 @@ public function notFind($value_or_array_or_callable) { return $this->find($value_or_array_or_callable, 'andnot'); } -} \ No newline at end of file + + /* + * Escape with a backslash reserved characters + * https://resources.docs.salesforce.com/sfdc/pdf/salesforce_soql_sosl.pdf + */ + private function escapeReservedCharacters($q) + { + $search = ["?", "&", "|", "!", "{", "}", "[", "]", "(", ")", "^", "~", "*", ":", "\\", '"', "'", "+", "-"]; + $replace = ["\?", "\&", "\|", "\!", "\{", "\}", "\[", "\]", "\(", "\)", "\^", "\~", "\*", "\:", "\\", '\"', "\'", "\+", "\-"]; + $q = str_replace($search, $replace, $q); + + return $q; + } +}