Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Search {
'find' => '',
'searchGroup' => '',
'from' => null,
'limit' => null,
];

/**
Expand Down Expand Up @@ -47,6 +48,11 @@ class Search {
*/
protected $from = '';

/**
* @var int
*/
protected $limit = null;

/**
* HTTP Client Instance
* @see \GuzzleHttp\Client
Expand Down Expand Up @@ -93,6 +99,7 @@ public function resetComponents() {
'find' => '',
'searchGroup' => '',
'from' => null,
'limit' => null,
];
}

Expand Down Expand Up @@ -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;
}
/**
Expand All @@ -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);
}

Expand Down Expand Up @@ -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'])) {
Expand Down Expand Up @@ -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) !== '(') {
Expand Down Expand Up @@ -342,4 +359,17 @@ public function notFind($value_or_array_or_callable)
{
return $this->find($value_or_array_or_callable, 'andnot');
}
}

/*
* 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;
}
}