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
|
|
|
use InvalidArgumentException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Flipbox Factory <[email protected]> |
15
|
|
|
* @since 3.0.0 |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractSearchBuilder implements SearchBuilderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @return mixed|string |
21
|
|
|
*/ |
22
|
|
|
public function __toString() |
23
|
|
|
{ |
24
|
|
|
return $this->build(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function toConfig(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
'class' => get_class($this) |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $properties |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $properties = []) |
41
|
|
|
{ |
42
|
|
|
$this->populate($properties); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $properties |
47
|
|
|
* @return static |
48
|
|
|
*/ |
49
|
|
|
public function populate(array $properties = []) |
50
|
|
|
{ |
51
|
|
|
if (!empty($properties)) { |
52
|
|
|
foreach ($properties as $name => $value) { |
53
|
|
|
if ($this->canSetProperty($name)) { |
54
|
|
|
$this->{$name} = $value; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
|
|
public function hasProperty($name, $checkVars = true): bool |
66
|
|
|
{ |
67
|
|
|
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdoc |
72
|
|
|
*/ |
73
|
|
|
public function canGetProperty($name, $checkVars = true): bool |
74
|
|
|
{ |
75
|
|
|
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
|
|
public function canSetProperty($name, $checkVars = true): bool |
82
|
|
|
{ |
83
|
|
|
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public function hasMethod($name): bool |
90
|
|
|
{ |
91
|
|
|
return method_exists($this, $name); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $name |
96
|
|
|
* @return mixed |
97
|
|
|
* @throws InvalidArgumentException |
98
|
|
|
*/ |
99
|
|
|
public function __get($name) |
100
|
|
|
{ |
101
|
|
|
$getter = 'get' . $name; |
102
|
|
|
if (method_exists($this, $getter)) { |
103
|
|
|
return $this->$getter(); |
104
|
|
|
} elseif (method_exists($this, 'set' . $name)) { |
105
|
|
|
throw new InvalidArgumentException('Getting write-only property: ' . get_class($this) . '::' . $name); |
106
|
|
|
} else { |
107
|
|
|
throw new InvalidArgumentException('Getting unknown property: ' . get_class($this) . '::' . $name); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $name |
113
|
|
|
* @param $value |
114
|
|
|
* @throws InvalidArgumentException |
115
|
|
|
*/ |
116
|
|
|
public function __set($name, $value) |
117
|
|
|
{ |
118
|
|
|
$setter = 'set' . $name; |
119
|
|
|
if (method_exists($this, $setter)) { |
120
|
|
|
$this->$setter($value); |
121
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
122
|
|
|
throw new InvalidArgumentException('Setting read-only property: ' . get_class($this) . '::' . $name); |
123
|
|
|
} else { |
124
|
|
|
throw new InvalidArgumentException('Setting unknown property: ' . get_class($this) . '::' . $name); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param $name |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
|
|
public function __isset($name) |
133
|
|
|
{ |
134
|
|
|
$getter = 'get' . $name; |
135
|
|
|
if (method_exists($this, $getter)) { |
136
|
|
|
return $this->$getter() !== null; |
137
|
|
|
} else { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $name |
144
|
|
|
* @throws InvalidArgumentException |
145
|
|
|
*/ |
146
|
|
|
public function __unset($name) |
147
|
|
|
{ |
148
|
|
|
$setter = 'set' . $name; |
149
|
|
|
if (method_exists($this, $setter)) { |
150
|
|
|
$this->$setter(null); |
151
|
|
|
} elseif (method_exists($this, 'get' . $name)) { |
152
|
|
|
throw new InvalidArgumentException('Unsetting read-only property: ' . get_class($this) . '::' . $name); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $name |
158
|
|
|
* @param $params |
159
|
|
|
* @throws InvalidArgumentException |
160
|
|
|
*/ |
161
|
|
|
public function __call($name, $params) |
162
|
|
|
{ |
163
|
|
|
throw new InvalidArgumentException('Calling unknown method: ' . get_class($this) . "::$name()"); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|