|
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\Query; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Flipbox Factory <[email protected]> |
|
13
|
|
|
* @since 3.0.0 |
|
14
|
|
|
*/ |
|
15
|
|
|
trait QueryBuilderAttributeTrait |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var QueryBuilderInterface|string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $query = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param array $config |
|
24
|
|
|
* @return QueryBuilderInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getQuery(array $config = []): QueryBuilderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$this->query instanceof QueryBuilderInterface) { |
|
29
|
|
|
$this->query = $this->createQueryBuilder($this->query); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (!empty($config)) { |
|
33
|
|
|
$this->populateQuery( |
|
34
|
|
|
$this->query, |
|
35
|
|
|
$config |
|
36
|
|
|
); |
|
37
|
|
|
}; |
|
38
|
|
|
|
|
39
|
|
|
return $this->query; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $query |
|
44
|
|
|
*/ |
|
45
|
|
|
public function setQuery($query) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($query instanceof QueryBuilderInterface) { |
|
48
|
|
|
$this->query = $query; |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->getQuery($this->normalizeQueryConfig($query)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param QueryBuilderInterface $query |
|
57
|
|
|
* @param array $config |
|
58
|
|
|
*/ |
|
59
|
|
|
private function populateQuery(QueryBuilderInterface $query, array $config = []) |
|
60
|
|
|
{ |
|
61
|
|
|
foreach ($config as $name => $value) { |
|
62
|
|
|
$setter = 'set' . $name; |
|
63
|
|
|
if (method_exists($this, $setter)) { |
|
64
|
|
|
$this->$setter($value); |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (property_exists($query, $name)) { |
|
69
|
|
|
$query->{$name} = $value; |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $config |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
private function normalizeQueryConfig($config): array |
|
80
|
|
|
{ |
|
81
|
|
|
if (!is_array($config)) { |
|
82
|
|
|
if (is_string($config)) { |
|
83
|
|
|
$config = ['soql' => $config]; |
|
84
|
|
|
} else { |
|
85
|
|
|
$config = [$config]; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return array_filter($config); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $query |
|
94
|
|
|
* @return QueryBuilderInterface |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function createQueryBuilder($query): QueryBuilderInterface |
|
97
|
|
|
{ |
|
98
|
|
|
if ($query instanceof QueryBuilderInterface) { |
|
99
|
|
|
return $query; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$builder = new DynamicQueryBuilder(); |
|
103
|
|
|
|
|
104
|
|
|
$this->populateQuery( |
|
105
|
|
|
$builder, |
|
106
|
|
|
$this->normalizeQueryConfig($query) |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
return $builder; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|