|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SevenShores\Hubspot\Resources; |
|
4
|
|
|
|
|
5
|
|
|
class HubDB extends Resource |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Get all tables |
|
9
|
|
|
* |
|
10
|
|
|
* @param int $portalId Hub/Portal ID |
|
11
|
|
|
* |
|
12
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
13
|
|
|
*/ |
|
14
|
|
|
public function tables($portalId) { |
|
15
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables'; |
|
16
|
|
|
|
|
17
|
|
|
$queryString = build_query_string(['portalId' => $portalId]); |
|
18
|
|
|
|
|
19
|
|
|
return $this->client->request('get', $endpoint, [], $queryString); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get details about a table |
|
24
|
|
|
* |
|
25
|
|
|
* @param int $portalId Hub ID |
|
26
|
|
|
* @param int $tableId Table ID |
|
27
|
|
|
* |
|
28
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
29
|
|
|
*/ |
|
30
|
|
View Code Duplication |
public function tableInfo($portalId, $tableId) { |
|
31
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables/'.$tableId; |
|
32
|
|
|
|
|
33
|
|
|
$queryString = build_query_string(['portalId' => $portalId]); |
|
34
|
|
|
|
|
35
|
|
|
return $this->client->request('get', $endpoint, [], $queryString); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Delete a table |
|
40
|
|
|
* |
|
41
|
|
|
* @param int $tableId Table ID |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
44
|
|
|
*/ |
|
45
|
|
|
public function deleteTable($tableId) { |
|
46
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables/'.$tableId; |
|
47
|
|
|
|
|
48
|
|
|
return $this->client->request('delete', $endpoint); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Delete a row |
|
53
|
|
|
* |
|
54
|
|
|
* @param int $tableId Table ID |
|
55
|
|
|
* @param int $rowId Row ID |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
58
|
|
|
*/ |
|
59
|
|
|
public function deleteRow($tableId, $rowId) { |
|
60
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables/'.$tableId.'/rows/'.$rowId; |
|
61
|
|
|
|
|
62
|
|
|
return $this->client->request('delete', $endpoint); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $name table name |
|
67
|
|
|
* @param array $columns column name and type should be represented as associative array, e.g. ["name" => "Name", "type" => "TEXT"], @see https://developers.hubspot.com/docs/methods/hubdb/create_table |
|
68
|
|
|
* @param bool $published whether to publish table |
|
69
|
|
|
* @param bool $useForPages use table for dynamic pages, see https://designers.hubspot.com/docs/tutorials/how-to-build-dynamic-pages-with-hubdb |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
72
|
|
|
*/ |
|
73
|
|
|
public function createTable($name, array $columns, $published = true, $useForPages = false) { |
|
74
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables'; |
|
75
|
|
|
$options['json'] = ['name' => $name, 'columns' => $columns]; |
|
|
|
|
|
|
76
|
|
|
if($published) { |
|
77
|
|
|
$options['json']['publishedAt'] = round(microtime(true) * 1000); |
|
78
|
|
|
} |
|
79
|
|
|
$options['json']['useForPages'] = $useForPages; |
|
80
|
|
|
|
|
81
|
|
|
return $this->client->request('post', $endpoint, $options); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get table rows |
|
86
|
|
|
* |
|
87
|
|
|
* @param int $portalId Hub/Portal ID |
|
88
|
|
|
* @param int $tableId table ID |
|
89
|
|
|
* @param array $params key-value array to filter and sort rows |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
92
|
|
|
*/ |
|
93
|
|
View Code Duplication |
public function rows($portalId, $tableId, array $params) |
|
94
|
|
|
{ |
|
95
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables/'.$tableId.'/rows'; |
|
96
|
|
|
|
|
97
|
|
|
$queryString = build_query_string(array_merge(['portalId' => $portalId], $params)); |
|
98
|
|
|
|
|
99
|
|
|
return $this->client->request('get', $endpoint, [], $queryString); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param int $tableId table ID |
|
104
|
|
|
* @param array $values |
|
105
|
|
|
* |
|
106
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
107
|
|
|
*/ |
|
108
|
|
|
public function addRow($tableId, array $values) { |
|
109
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables/'.$tableId.'/rows'; |
|
110
|
|
|
$options['json'] = ['values' => $values]; |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
return $this->client->request('post', $endpoint, $options); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param int $tableId table ID |
|
117
|
|
|
* @param array $values |
|
118
|
|
|
* @param string $title page title for dynamic page |
|
119
|
|
|
* @param string $path path to access page (appended to domain to form page URL) |
|
120
|
|
|
* |
|
121
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
122
|
|
|
*/ |
|
123
|
|
|
public function addRowForPage($tableId, array $values, $title = '', $path = '') { |
|
124
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables/'.$tableId.'/rows'; |
|
125
|
|
|
$options['json'] = ['values' => $values, 'name' => $title, 'path' => $path]; |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
return $this->client->request('post', $endpoint, $options); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* update database table row |
|
132
|
|
|
* |
|
133
|
|
|
* @param int $tableId |
|
134
|
|
|
* @param int $rowId |
|
135
|
|
|
* @param array $values |
|
136
|
|
|
* |
|
137
|
|
|
* @return \Psr\Http\Message\ResponseInterface|\SevenShores\Hubspot\Http\Response |
|
138
|
|
|
*/ |
|
139
|
|
|
public function updateRow($tableId, $rowId, array $values) { |
|
140
|
|
|
$endpoint = 'https://api.hubapi.com/hubdb/api/v1/tables/'.$tableId.'/rows/'.$rowId; |
|
141
|
|
|
$options['json'] = ['values' => $values]; |
|
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
return $this->client->request('put', $endpoint, $options); |
|
144
|
|
|
} |
|
145
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.