|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/salesforce/blob/master/LICENSE.md |
|
6
|
|
|
* @link https://github.com/flipbox/salesforce |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Flipbox\Salesforce\Search; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Flipbox Factory <[email protected]> |
|
13
|
|
|
* @since 3.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
class DynamicSearchBuilder extends RawSearchBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
use DynamicVariablesAttributeTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The opening variable character |
|
21
|
|
|
*/ |
|
22
|
|
|
const VARIABLE_OPENING = '{{'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The closing variable character |
|
26
|
|
|
*/ |
|
27
|
|
|
const VARIABLE_CLOSING = '}}'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritdoc |
|
31
|
|
|
*/ |
|
32
|
|
|
public function build(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->prepareSearch($this->search); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $soql |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
private function prepareSearch(string $soql): string |
|
42
|
|
|
{ |
|
43
|
|
|
if (false === (preg_match_all( |
|
44
|
|
|
'/' . self::VARIABLE_OPENING . '(.*?)' . self::VARIABLE_CLOSING . '/', |
|
45
|
|
|
$soql, |
|
46
|
|
|
$matches |
|
47
|
|
|
))) { |
|
48
|
|
|
return $soql; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$replace = $this->getReplacingAttributes($matches[1]); |
|
52
|
|
|
|
|
53
|
|
|
return str_ireplace(array_keys($replace), array_values($replace), $soql); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $variables |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
private function getReplacingAttributes(array $variables = []) |
|
61
|
|
|
{ |
|
62
|
|
|
$attributes = $this->getVariables(); |
|
63
|
|
|
|
|
64
|
|
|
$values = []; |
|
65
|
|
|
|
|
66
|
|
|
foreach ($variables as $variable) { |
|
67
|
|
|
$values[self::VARIABLE_OPENING . $variable . self::VARIABLE_CLOSING] = |
|
68
|
|
|
($attributes[$variable] ?? $variable); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $values; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
public function toConfig(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return array_merge( |
|
80
|
|
|
parent::toConfig(), |
|
81
|
|
|
[ |
|
82
|
|
|
'variables' => $this->getVariables() |
|
83
|
|
|
] |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|