@@ -29,6 +29,9 @@ class RemoteType extends ParentType implements \Asseco\CustomFields\App\Contract
29
29
'mappings ' => 'array ' ,
30
30
];
31
31
32
+ const DEFAULT_IDENTIFIER_PROPERTY = 'id ' ;
33
+ const DEFAULT_SEARCH_QUERY_PARAMETER = 'q ' ;
34
+
32
35
protected static function newFactory ()
33
36
{
34
37
return RemoteTypeFactory::new ();
@@ -44,23 +47,55 @@ public function getNameAttribute()
44
47
return 'remote ' ;
45
48
}
46
49
47
- public function getRemoteData ( )
50
+ private function fetchData (? string $ value = null , bool $ search = false )
48
51
{
49
- $ cacheKey = 'remote_custom_field_ ' . $ this ->id ;
52
+ $ qParam = $ this ->identifier_property ?: self ::DEFAULT_IDENTIFIER_PROPERTY ;
53
+ if ($ search ) {
54
+ $ qParam = self ::DEFAULT_SEARCH_QUERY_PARAMETER ;
55
+ }
56
+
57
+ $ body = $ this ->body ;
58
+ $ url = $ this ->url ;
59
+
60
+ if ($ value ) {
61
+ // get by ID
62
+ if ($ this ->method == 'POST ' ) {
63
+ empty ($ body ) ? ($ body = [$ qParam => $ value ]) : ($ body [$ qParam ] = $ value );
64
+ } else {
65
+ $ parsed = parse_url ($ url );
66
+ parse_str ($ parsed ['query ' ] ?? '' , $ params );
67
+ $ params [$ qParam ] = $ value ;
68
+ $ url = $ parsed ['scheme ' ] . ':// ' . $ parsed ['host ' ];
69
+ if (!empty ($ parsed ['port ' ])) {
70
+ $ url .= ': ' . $ parsed ['port ' ];
71
+ }
72
+ $ url .= $ parsed ['path ' ] . '? ' . http_build_query ($ params );
73
+ }
74
+ }
75
+
76
+ return Http::withHeaders ($ this ->getHeaders () ?: [])
77
+ ->withBody ($ body , 'application/json ' )
78
+ ->{$ this ->method }($ url )->throw ()->json ();
79
+ }
50
80
81
+ public function getRemoteData (?string $ identifierValue = null )
82
+ {
83
+ $ cacheKey = 'remote_custom_field_ ' . $ this ->id ;
51
84
if (config ('asseco-custom-fields.should_cache_remote ' ) && Cache::has ($ cacheKey )) {
52
85
return Cache::get ($ cacheKey );
53
86
}
54
87
55
- $ response = Http::withHeaders ($ this ->getHeaders () ?: [])
56
- ->withBody ($ this ->body , 'application/json ' )
57
- ->{$ this ->method }($ this ->url )->throw ()->json ();
58
-
88
+ $ response = $ this ->fetchData ($ identifierValue , false );
59
89
Cache::put ($ cacheKey , $ response , config ('asseco-custom-fields.remote_cache_ttl ' ));
60
90
61
91
return $ response ;
62
92
}
63
93
94
+ public function searchRemoteData (string $ searchString )
95
+ {
96
+ return $ this ->fetchData ($ searchString , true );
97
+ }
98
+
64
99
protected function getHeaders ()
65
100
{
66
101
return $ this ->headers ;
0 commit comments