@@ -22,7 +22,7 @@ class Builder
22
22
protected string $ objectClass ;
23
23
24
24
protected array $ properties = [];
25
- protected array $ associations = [];
25
+ protected array $ with = [];
26
26
27
27
public function __construct (protected Client $ client )
28
28
{}
@@ -46,7 +46,7 @@ public function include($properties): static
46
46
47
47
public function with ($ associations ): static
48
48
{
49
- $ this ->associations = is_array ($ associations )
49
+ $ this ->with = is_array ($ associations )
50
50
? $ associations
51
51
: func_get_args ();
52
52
@@ -55,16 +55,16 @@ public function with($associations): static
55
55
56
56
public function find ($ id , $ idProperty = null ): Model
57
57
{
58
- $ response = $ this ->client ->get (
58
+ $ response = $ this ->client () ->get (
59
59
$ this ->object ->endpoint ('read ' , ['id ' => $ id ]),
60
60
[
61
- 'properties ' => implode (", " , $ this ->properties ()),
62
- 'associations ' => implode (", " , $ this ->associations ()),
61
+ 'properties ' => implode (", " , $ this ->includeProperties ()),
62
+ 'associations ' => implode (", " , $ this ->includeAssociations ()),
63
63
'idProperty ' => $ idProperty
64
64
]
65
65
)->json ();
66
66
67
- return new $ this ->objectClass ($ response );
67
+ return ( new $ this ->objectClass ($ response))-> has ( $ this -> includeAssociations () );
68
68
}
69
69
70
70
public function findMany (array $ ids , $ idProperty = null ): Collection
@@ -79,10 +79,10 @@ public function findMany(array $ids, $idProperty = null): Collection
79
79
return new Collection ();
80
80
}
81
81
82
- $ response = $ this ->client ->post (
82
+ $ response = $ this ->client () ->post (
83
83
$ this ->object ->endpoint ('batchRead ' ),
84
84
[
85
- 'properties ' => $ this ->properties (),
85
+ 'properties ' => $ this ->includeProperties (),
86
86
'idProperty ' => $ idProperty ,
87
87
'inputs ' => array_map (fn ($ id ) => ['id ' => $ id ], $ ids )
88
88
]
@@ -131,13 +131,13 @@ public function search($input): static
131
131
132
132
public function fetch ($ after = null , $ limit = null ): array
133
133
{
134
- return $ this ->client ->post (
134
+ return $ this ->client () ->post (
135
135
$ this ->object ->endpoint ('search ' ),
136
136
[
137
137
'limit ' => $ limit ?? $ this ->limit ,
138
138
'after ' => $ after ?? $ this ->after ?? null ,
139
139
'query ' => $ this ->query ?? null ,
140
- 'properties ' => $ this ->properties (),
140
+ 'properties ' => $ this ->includeProperties (),
141
141
'sorts ' => isset ($ this ->sort ) ? [$ this ->sort ] : null ,
142
142
'filterGroups ' => [[
143
143
'filters ' => array_map (fn ($ filter ) => $ filter ->toArray (), $ this ->filters )
@@ -192,19 +192,32 @@ public function count(): int
192
192
return Arr::get ($ this ->get (1 , 0 , false ), 'total ' , 0 );
193
193
}
194
194
195
- protected function properties (): array
195
+ public function client (): Client
196
+ {
197
+ return $ this ->client ;
198
+ }
199
+
200
+ public function associations ($ association ): array
201
+ {
202
+ return $ this ->client ()->get (
203
+ $ this ->object ->endpoint ('associations ' , ['id ' => $ this ->object ->id , 'association ' => $ association ])
204
+ )->json ()['results ' ];
205
+ }
206
+
207
+ protected function includeProperties (): array
196
208
{
197
209
return array_merge (
198
- config ("hubspot. {$ this ->object ->type ()}.include_properties " ),
210
+ config ("hubspot. {$ this ->object ->type ()}.include_properties " , [] ),
199
211
$ this ->properties
200
212
);
201
213
}
202
214
203
- protected function associations (): array
215
+ protected function includeAssociations (): array
204
216
{
205
217
return array_merge (
206
- config ("hubspot. {$ this ->object ->type ()}.include_associations " ),
207
- $ this ->associations
218
+ config ("hubspot. {$ this ->object ->type ()}.include_associations " , [] ),
219
+ $ this ->with
208
220
);
209
221
}
222
+
210
223
}
0 commit comments