1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Picqer\Financials\Exact\Query; |
4
|
|
|
|
5
|
|
|
use Picqer\Financials\Exact\Connection; |
6
|
|
|
|
7
|
|
|
trait Findable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @return Connection |
11
|
|
|
*/ |
12
|
|
|
abstract public function connection(); |
13
|
|
|
|
14
|
|
|
abstract public function isFillable($key); |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
abstract public function url(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
abstract public function primaryKey(); |
25
|
|
|
|
26
|
|
|
public function find($id) |
27
|
|
|
{ |
28
|
|
|
$filter = $this->primaryKey() . " eq guid'$id'"; |
29
|
|
|
|
30
|
|
|
if ($this->primaryKey() === 'Code') { |
31
|
|
|
$filter = $this->primaryKey() . " eq $id"; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$records = $this->connection()->get($this->url(), [ |
35
|
|
|
'$filter' => $filter, |
36
|
|
|
'$top' => 1, // The result will always be 1 but on some entities Exact gives an error without it. |
37
|
|
|
]); |
38
|
|
|
|
39
|
|
|
$result = isset($records[0]) ? $records[0] : []; |
40
|
|
|
|
41
|
|
|
return new self($this->connection(), $result); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function findWithSelect($id, $select = '') |
45
|
|
|
{ |
46
|
|
|
//eg: $oAccounts->findWithSelect('5b7f4515-b7a0-4839-ac69-574968677d96', 'Code, Name'); |
47
|
|
|
$result = $this->connection()->get($this->url(), [ |
48
|
|
|
'$filter' => $this->primaryKey() . " eq guid'$id'", |
49
|
|
|
'$select' => $select, |
50
|
|
|
]); |
51
|
|
|
|
52
|
|
|
return new self($this->connection(), $result); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return the value of the primary key. |
57
|
|
|
* |
58
|
|
|
* @param string $code the value to search for |
59
|
|
|
* @param string $key the key being searched (defaults to 'Code') |
60
|
|
|
* |
61
|
|
|
* @return string (guid) |
62
|
|
|
*/ |
63
|
|
|
public function findId($code, $key = 'Code') |
64
|
|
|
{ |
65
|
|
|
if ($this->isFillable($key)) { |
66
|
|
|
$format = ($this->url() == 'crm/Accounts' && $key === 'Code') ? '%18s' : '%s'; |
67
|
|
|
if (preg_match('/^[\w]{8}-([\w]{4}-){3}[\w]{12}$/', $code)) { |
68
|
|
|
$format = "guid'$format'"; |
69
|
|
|
} elseif (is_string($code)) { |
|
|
|
|
70
|
|
|
$format = "'$format'"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$filter = sprintf("$key eq $format", $code); |
74
|
|
|
$request = [ |
75
|
|
|
'$filter' => $filter, |
76
|
|
|
'$top' => 1, |
77
|
|
|
'$select' => $this->primaryKey(), |
78
|
|
|
'$orderby' => $this->primaryKey(), |
79
|
|
|
]; |
80
|
|
|
if ($records = $this->connection()->get($this->url(), $request)) { |
81
|
|
|
return $records[0][$this->primaryKey()]; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function filter($filter, $expand = '', $select = '', $system_query_options = null, array $headers = []) |
87
|
|
|
{ |
88
|
|
|
$originalDivision = $this->connection()->getDivision(); |
89
|
|
|
|
90
|
|
|
if ($this->isFillable('Division') && preg_match("@Division[\t\r\n ]+eq[\t\r\n ]+([0-9]+)@i", $filter, $divisionId)) { |
91
|
|
|
$this->connection()->setDivision($divisionId[1]); // Fix division |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$request = [ |
95
|
|
|
'$filter' => $filter, |
96
|
|
|
]; |
97
|
|
|
if (strlen($expand) > 0) { |
98
|
|
|
$request['$expand'] = $expand; |
99
|
|
|
} |
100
|
|
|
if (strlen($select) > 0) { |
101
|
|
|
$request['$select'] = $select; |
102
|
|
|
} |
103
|
|
|
if (is_array($system_query_options)) { |
104
|
|
|
// merge in other options |
105
|
|
|
// no verification of proper system query options |
106
|
|
|
$request = array_merge($system_query_options, $request); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$result = $this->connection()->get($this->url(), $request, $headers); |
110
|
|
|
|
111
|
|
|
if (! empty($divisionId)) { |
112
|
|
|
$this->connection()->setDivision($originalDivision); // Restore division |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->collectionFromResult($result); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the first Financial model in by applying $top=1 to the query string. |
120
|
|
|
* |
121
|
|
|
* @return \Picqer\Financials\Exact\Model|null |
122
|
|
|
*/ |
123
|
|
|
public function first() |
124
|
|
|
{ |
125
|
|
|
$results = $this->filter('', '', '', ['$top'=> 1]); |
126
|
|
|
$result = is_array($results) && count($results) > 0 ? $results[0] : null; |
127
|
|
|
|
128
|
|
|
return $result; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getResultSet(array $params = []) |
132
|
|
|
{ |
133
|
|
|
return new Resultset($this->connection(), $this->url(), get_class($this), $params); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function get(array $params = []) |
137
|
|
|
{ |
138
|
|
|
$result = $this->connection()->get($this->url(), $params); |
139
|
|
|
|
140
|
|
|
return $this->collectionFromResult($result); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function collectionFromResult($result) |
144
|
|
|
{ |
145
|
|
|
// If we have one result which is not an assoc array, make it the first element of an array for the |
146
|
|
|
// collectionFromResult function so we always return a collection from filter |
147
|
|
|
if ((bool) count(array_filter(array_keys($result), 'is_string'))) { |
148
|
|
|
$result = [$result]; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
while ($this->connection()->nextUrl !== null) { |
152
|
|
|
$nextResult = $this->connection()->get($this->connection()->nextUrl); |
153
|
|
|
|
154
|
|
|
// If we have one result which is not an assoc array, make it the first element of an array for the array_merge function |
155
|
|
|
if ((bool) count(array_filter(array_keys($nextResult), 'is_string'))) { |
156
|
|
|
$nextResult = [$nextResult]; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$result = array_merge($result, $nextResult); |
160
|
|
|
} |
161
|
|
|
$collection = []; |
162
|
|
|
foreach ($result as $r) { |
163
|
|
|
$collection[] = new self($this->connection(), $r); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $collection; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.