Skip to content

Commit 81030ca

Browse files
authored
Merge pull request #4 from MacsiDigital/3.0
Update 3.0 to master
2 parents 31f14e6 + 1e6335b commit 81030ca

File tree

5 files changed

+79
-58
lines changed

5 files changed

+79
-58
lines changed

src/Support/PersistResource.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function fill(object $object, $type="insert")
2828
}
2929
}
3030
$this->processEmpties();
31+
3132
return $this;
3233
}
3334

@@ -231,9 +232,18 @@ public function getRelation($name)
231232
return $this->relations[$name];
232233
}
233234

234-
public function getAttributes()
235+
public function getAttributes($wrapped='', $emptyArray = false)
235236
{
236-
return $this->attributes;
237+
if($emptyArray){
238+
$return = [$this->attributes];
239+
} else {
240+
$return = $this->attributes;
241+
}
242+
243+
if($wrapped == ''){
244+
return $return;
245+
}
246+
return [$wrapped => $return];
237247
}
238248

239249
public function validate()

src/Support/ResultSet.php

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ResultSet implements Arrayable, ArrayAccess, Countable, IteratorAggregate,
4747
protected $resource;
4848

4949
public function __construct($builder, $response, $resource, $all=false)
50-
{
50+
{
5151
$this->all = $all;
5252
$this->builder = $builder;
5353
$this->resource = $resource;
@@ -73,15 +73,15 @@ protected function setRawStatus(bool $status=false)
7373
$this->raw = $status;
7474
}
7575

76-
protected function processResponse($response)
76+
protected function processResponse($response)
7777
{
7878
$this->addResponse($response);
7979
$this->populate($response->json());
8080
$this->processMeta($response->json());
8181
$this->incrementQueries();
8282
}
8383

84-
protected function incrementQueries()
84+
protected function incrementQueries()
8585
{
8686
$this->queries++;
8787
}
@@ -94,22 +94,22 @@ protected function canQuery()
9494
return $this->maxQueries > $this->queries;
9595
}
9696

97-
protected function incrementTotalDownloads($i=1)
97+
protected function incrementTotalDownloads($i=1)
9898
{
9999
$this->downloaded += $i;
100100
}
101101

102-
public function resetQueryCount()
102+
public function resetQueryCount()
103103
{
104104
$this->queries = 0;
105105
}
106106

107-
protected function addResponse($response)
107+
protected function addResponse($response)
108108
{
109109
$this->responses[] = $response;
110110
}
111111

112-
protected function populate($array)
112+
protected function populate($array)
113113
{
114114
if($this->raw){
115115
$this->items = array_merge($this->items, $array[$this->resource->getApiMultipleDataField()]);
@@ -123,7 +123,7 @@ protected function populate($array)
123123
}
124124
}
125125

126-
protected function processMeta($array)
126+
protected function processMeta($array)
127127
{
128128
if(Arr::has($array, $this->resource->client->getResultsPageField())){
129129
$this->apiCurrentPage = (int) Arr::get($array, $this->resource->client->getResultsPageField());
@@ -176,50 +176,50 @@ protected function apiPageHasBeenDownloaded($page)
176176
return in_array($page, $this->apiPagesDownloaded);
177177
}
178178

179-
protected function apiHasMorePages()
179+
protected function apiHasMorePages()
180180
{
181181
if($this->apiLastPage() == null){
182182
return true;
183183
}
184184
return $this->apiLastPage() > $this->apiCurrentPage();
185185
}
186186

187-
protected function apiHasPerPage()
187+
protected function apiHasPerPage()
188188
{
189189
return $this->apiPerPage != '';
190190
}
191191

192-
protected function apiIsFirstPage()
192+
protected function apiIsFirstPage()
193193
{
194194
return $this->apiCurrentPage() <= 1;
195195
}
196196

197-
protected function apiLastPage()
197+
protected function apiLastPage()
198198
{
199199
return $this->apiLastPage;
200200
}
201201

202-
protected function apiFirstPage()
202+
protected function apiFirstPage()
203203
{
204204
return 1;
205205
}
206206

207-
protected function apiPerPage()
207+
protected function apiPerPage()
208208
{
209209
return $this->apiPerPage;
210210
}
211211

212-
protected function apiCurrentPage()
212+
protected function apiCurrentPage()
213213
{
214214
return $this->apiCurrentPage;
215215
}
216216

217-
protected function apiNextPageNumber()
217+
protected function apiNextPageNumber()
218218
{
219219
return $this->apiCurrentPage + 1;
220220
}
221221

222-
protected function apiPreviousPageNumber()
222+
protected function apiPreviousPageNumber()
223223
{
224224
return $this->apiCurrentPage - 1;
225225
}
@@ -229,7 +229,7 @@ protected function apiTotalRecords()
229229
return $this->apiTotalRecords;
230230
}
231231

232-
public function hasMorePages()
232+
public function hasMorePages()
233233
{
234234
if($this->hasOwnMeta){
235235
return $this->lastPage() > $this->currentPage();
@@ -238,7 +238,7 @@ public function hasMorePages()
238238
}
239239
}
240240

241-
public function hasPerPage()
241+
public function hasPerPage()
242242
{
243243
if($this->hasOwnMeta){
244244
return true;
@@ -247,7 +247,7 @@ public function hasPerPage()
247247
}
248248
}
249249

250-
public function isFirstPage()
250+
public function isFirstPage()
251251
{
252252
if($this->hasOwnMeta){
253253
return $this->currentPage() <= 1;
@@ -256,7 +256,7 @@ public function isFirstPage()
256256
}
257257
}
258258

259-
public function totalRecords()
259+
public function totalRecords()
260260
{
261261
if($this->totalRecords){
262262
return $this->totalRecords;
@@ -265,7 +265,7 @@ public function totalRecords()
265265
}
266266
}
267267

268-
public function lastPage()
268+
public function lastPage()
269269
{
270270
if($this->hasOwnMeta){
271271
return $this->lastPage;
@@ -274,7 +274,7 @@ public function lastPage()
274274
}
275275
}
276276

277-
public function firstPage()
277+
public function firstPage()
278278
{
279279
if($this->hasOwnMeta){
280280
return 1;
@@ -283,7 +283,7 @@ public function firstPage()
283283
}
284284
}
285285

286-
public function perPage()
286+
public function perPage()
287287
{
288288
if($this->hasOwnMeta){
289289
return $this->perPage;
@@ -292,7 +292,7 @@ public function perPage()
292292
}
293293
}
294294

295-
public function currentPage()
295+
public function currentPage()
296296
{
297297
if($this->hasOwnMeta){
298298
return $this->currentPage;
@@ -301,7 +301,7 @@ public function currentPage()
301301
}
302302
}
303303

304-
public function nextPageNumber()
304+
public function nextPageNumber()
305305
{
306306
if($this->hasOwnMeta){
307307
return $this->currentPage + 1;
@@ -310,7 +310,7 @@ public function nextPageNumber()
310310
}
311311
}
312312

313-
public function previousPageNumber()
313+
public function previousPageNumber()
314314
{
315315
if($this->hasOwnMeta){
316316
return $this->currentPage -1;
@@ -319,7 +319,7 @@ public function previousPageNumber()
319319
}
320320
}
321321

322-
public function processRecordSweep()
322+
public function processRecordSweep()
323323
{
324324
if($this->all || !$this->builder->shouldPaginate()){
325325
$this->recursiveRecordCollection();
@@ -342,7 +342,7 @@ public function recursiveRecordCollection()
342342
}
343343
}
344344

345-
public function updateMeta()
345+
public function updateMeta()
346346
{
347347
$this->hasOwnMeta = true;
348348
$this->perPage = $this->maxQueries * $this->apiPerPage;
@@ -363,7 +363,7 @@ public function getNextRecords()
363363
return $this;
364364
}
365365

366-
public function resetPage()
366+
public function resetPage()
367367
{
368368
$this->items = new Collection;
369369
}
@@ -395,7 +395,7 @@ public function previousPage()
395395
} else {
396396
throw new OutOfResultSetException();
397397
}
398-
398+
399399
return $this;
400400
}
401401

@@ -419,7 +419,7 @@ public function getCollection()
419419
return $this->items;
420420
}
421421

422-
public function getResults()
422+
public function getResults()
423423
{
424424
return $this->getCollection();
425425
}
@@ -461,7 +461,7 @@ public function toArray()
461461
];
462462
}
463463

464-
public function itemsToArray()
464+
public function itemsToArray()
465465
{
466466
$data = [];
467467
foreach($this->items as $item){
@@ -485,4 +485,4 @@ public function __call($method, $parameters)
485485
return $this->forwardCallTo($this->getCollection(), $method, $parameters);
486486
}
487487

488-
}
488+
}

src/Traits/HasAttributes.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function setAttribute($key, $value)
6666
if($this->relationLoaded($key)){
6767
$this->updateRelationAttribute($key, $value);
6868
return $this;
69-
}
69+
}
7070
}
7171
}
7272

@@ -110,7 +110,7 @@ public function setAttribute($key, $value)
110110
return $this;
111111
}
112112

113-
protected function setRelationAttribute($key, $value)
113+
protected function setRelationAttribute($key, $value)
114114
{
115115
if($this->loadRaw || in_array($key, $this->dontAutoloadRelation)){
116116
return;
@@ -130,12 +130,12 @@ protected function setRelationAttribute($key, $value)
130130
}
131131
}
132132

133-
protected function updateRelationAttribute($key, $value)
133+
protected function updateRelationAttribute($key, $value)
134134
{
135135
if($this->loadRaw || in_array($key, $this->dontAutoloadRelation)){
136136
return;
137137
}
138-
138+
139139
if ($this->isRelationship($key)){
140140
$this->getRelation($key)->update($value);
141141
} elseif ($this->isRelationship(Str::plural($key))) {
@@ -152,8 +152,8 @@ public function getAttribute($key)
152152
if (! $key) {
153153
return;
154154
}
155-
156-
// need to check for a relationship first - otherwise we will be passing back the
155+
156+
// need to check for a relationship first - otherwise we will be passing back the
157157
// array if relationship is passed as attributes in the API call
158158
if(method_exists($this, 'setRelation') && $this->isRelationship($key)){
159159
return $this->getRelationValue($key);
@@ -191,7 +191,7 @@ public function getRelationValue($key)
191191
// it is a relationship and will load and return results from the query
192192
// and hydrate the relationship's value on the "relationships" array.
193193
if (method_exists($this, $key)) {
194-
return $this->getRelationshipFromMethod($key);
194+
return $this->getRelationshipFromMethod($key)->getResults();
195195
}
196196
}
197197

@@ -218,10 +218,12 @@ protected function getRelationshipFromMethod($method)
218218
'%s::%s must return a relationship instance.', static::class, $method
219219
));
220220
}
221+
$this->setRelation($method, $relation);
221222

222-
return tap($relation->getResults(), function ($results) use ($method) {
223-
$this->setRelation($method, $results);
224-
});
223+
return $this->getRelation($method);
224+
// return tap($relation->getResults(), function ($results) use ($method) {
225+
//
226+
// });
225227
}
226228

227229
/**
@@ -233,5 +235,5 @@ protected function getDateFormat()
233235
{
234236
return $this->dateFormat ?: 'Y-m-d H:i:s';
235237
}
236-
237-
}
238+
239+
}

0 commit comments

Comments
 (0)