-
Notifications
You must be signed in to change notification settings - Fork 27
/
CrosswalkQuery.php
executable file
·143 lines (129 loc) · 3.56 KB
/
CrosswalkQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Factual;
/**
* Represents a Factual Crosswalk query.
* This is a refactoring of the Factual Driver by Aaron: https://github.com/Factual/factual-java-driver
* @author Tyler
* @package Factual
* @license Apache 2.0
*/
class CrosswalkQuery extends FactualQuery {
private $factualId; //string
private $limit; //int
private $_namespace; //string
private $namespaceId; //string
private $only = array ();
const RESPONSETYPE = "CrosswalkResponse";
/**
* Whether this lib must perform URL encoding.
* Set to avoid double or absent encoding
*/
const URLENCODE = true;
/**
* Adds the specified Factual ID to this Query. Returned Crosswalk data will
* be for only the entity associated with the Factual ID.
* @param string factualId A unique Factual ID.
* @return object This CrosswalkQuery
*/
public function factualId($factualId) {
$this->factualId = $factualId;
return $this;
}
//Overides inherited function
public function sortDesc($field){
return $this;
}
//Overides inherited function
public function sortAsc($field) {
return $this;
}
/**
* Adds the specified <tt>limit</tt> to this Query. The amount of returned
* Crosswalk records will not exceed this limit.
* @param int limit Number of records to return
* @return object This CrosswalkQuery
*/
public function limit($limit) {
$this->limit = $limit;
return $this;
}
/**
* The namespace to search for a third party ID within.
* @param string namespace The namespace to search for a third party ID within.
* @return object This CrosswalkQuery
* @internal renamed from 'namespace' due to reserved word
*/
public function _namespace($namespace) {
$this->_namespace = $namespace;
return $this;
}
/**
* The id used by a third party to identify a place.
* You must also supply <tt>namespace</tt> via {@link #namespace(String)}.
* @param string namespaceId The id used by a third party to identify a place.
* @return object This CrosswalkQuery
*/
public function namespaceId($namespaceId) {
$this->namespaceId = $namespaceId;
return $this;
}
/**
* Restricts the results to only return ids for the specified namespace(s).
* @param mixed namespaces as comma-delineated strings, or array of namespace names
* @return object This CrosswalkQuery
*/
public function only($namespaces) {
if (!is_array($namespaces)) {
$namespaces = explode(",", $namespaces);
}
foreach ($namespaces as $ns) {
$this->only[] = $ns;
}
return $this;
}
/**
* Converts this entity to a URL
* @return string
*/
public function toUrlQuery() {
$temp[] = $this->urlPair("factual_id", $this->factualId);
$temp[] = ($this->limit > 0 ? $this->urlPair("limit", $this->limit) : null);
$temp[] = $this->urlPair("namespace", $this->_namespace);
$temp[] = $this->urlPair("namespace_id", $this->namespaceId);
$temp[] = $this->urlPair("only", $this->onlysOrNull());
//remove nulls
$temp = array_filter($temp);
//join and return
return implode("&", $temp);
}
private function onlysOrNull() {
if (!empty ($this->only)) {
$this->only = array_filter($this->only);
return implode(",", $this->only);
} else {
return null;
}
}
/**
* @param string name Name
* @param string val Value
* @return string
* @internal Not sure why val is obj in Java version in return line
*/
private function urlPair($name, $val) {
if ($val != null) {
try {
if (self :: URLENCODE) {
return $name . "=" . urlencode($val);
} else {
return $name . "=" . $val;
}
} catch (Exception $e) {
throw $e;
}
} else {
return null;
}
}
}
?>