|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Trucker\Finders; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Trucker\Facades\AuthFactory; |
|
7
|
|
|
use Trucker\Facades\Config; |
|
8
|
|
|
use Trucker\Facades\RequestFactory; |
|
9
|
|
|
use Trucker\Facades\UrlGenerator; |
|
10
|
|
|
use Trucker\Finders\Conditions\QueryConditionInterface; |
|
11
|
|
|
use Trucker\Finders\Conditions\QueryResultOrderInterface; |
|
12
|
|
|
use Trucker\Resource\Model; |
|
13
|
|
|
use Trucker\Responses\Collection; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class for finding collections of models over the remote API. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Alessandro Manno <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class CollectionFinder |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* The IoC Container. |
|
24
|
|
|
* |
|
25
|
|
|
* @var Container |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $app; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Build a new CollectionFinder. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Container $app |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function __construct(Container $app) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->app = $app; |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Function to fetch a collection of Trucker\Resource\Model object |
|
41
|
|
|
* from the remote API. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Model $model Instance of entity type being fetched |
|
44
|
|
|
* @param QueryConditionInterface $condition Query conditions for the request |
|
45
|
|
|
* @param QueryResultOrderInterface $resultOrder Result ordering requirements for the request |
|
46
|
|
|
* @param array $getParams Additional GET parameters to send w/ request |
|
47
|
|
|
* |
|
48
|
|
|
* @return Collection |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function fetch( |
|
51
|
|
|
Model $model, |
|
52
|
|
|
QueryConditionInterface $condition = null, |
|
53
|
|
|
QueryResultOrderInterface $resultOrder = null, |
|
54
|
|
|
array $getParams = [] |
|
55
|
|
|
) { |
|
56
|
|
|
//get a request object |
|
57
|
4 |
|
$request = RequestFactory::build(); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
//init the request |
|
60
|
4 |
|
$request->createRequest( |
|
61
|
4 |
|
Config::get('request.base_uri'), |
|
|
|
|
|
|
62
|
4 |
|
UrlGenerator::getCollectionUri($model), |
|
|
|
|
|
|
63
|
4 |
|
'GET' |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
//add auth if it is needed |
|
67
|
4 |
|
if ($auth = AuthFactory::build()) { |
|
|
|
|
|
|
68
|
|
|
$request->authenticate($auth); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
//add query conditions if needed |
|
72
|
4 |
|
if ($condition) { |
|
73
|
1 |
|
$request->addQueryCondition($condition); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
//add result ordering if needed |
|
77
|
4 |
|
if ($resultOrder) { |
|
78
|
1 |
|
$request->addQueryResultOrder($resultOrder); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
//set any get parameters on the request |
|
82
|
4 |
|
$request->setGetParameters($getParams); |
|
83
|
|
|
|
|
84
|
|
|
//actually send the request |
|
85
|
4 |
|
$response = $request->sendRequest(); |
|
86
|
|
|
|
|
87
|
|
|
//get api response |
|
88
|
4 |
|
$data = $response->parseResponseToData(); |
|
89
|
|
|
|
|
90
|
|
|
//make an array to hold results |
|
91
|
4 |
|
$records = []; |
|
92
|
|
|
|
|
93
|
|
|
//figure out wether a collection key is used |
|
94
|
4 |
|
$collection_key = Config::get('resource.collection_key'); |
|
95
|
|
|
|
|
96
|
|
|
//set records array appropriatley |
|
97
|
4 |
|
$recordCollection = $data; |
|
98
|
|
|
|
|
99
|
4 |
|
if (isset($collection_key)) { |
|
100
|
1 |
|
$recordCollection = $data[$collection_key]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
//create an array of popuplated results |
|
104
|
4 |
|
foreach ($recordCollection as $values) { |
|
105
|
4 |
|
$instance = new $model($values); |
|
106
|
|
|
|
|
107
|
|
|
//inflate the ID property that should be guarded |
|
108
|
4 |
|
$id = $instance->getIdentityProperty(); |
|
109
|
4 |
|
if (array_key_exists($id, $values)) { |
|
110
|
4 |
|
$instance->{$id} = $values[$id]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
//add the instance to the records array |
|
114
|
4 |
|
$records[] = $instance; |
|
115
|
|
|
}//end foreach |
|
116
|
|
|
|
|
117
|
|
|
//create a collection object to return |
|
118
|
4 |
|
$collection = new Collection($records); |
|
119
|
|
|
|
|
120
|
|
|
// if there was a collection_key, put any extra data that was returned |
|
121
|
|
|
// outside the collection key in the metaData attribute |
|
122
|
4 |
|
if (isset($collection_key)) { |
|
123
|
1 |
|
$collection->metaData = array_diff_key($data, array_flip([$collection_key])); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
return $collection; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|