Skip to content

Commit e96e516

Browse files
committed
Update for 1.0.12
1 parent 4b1ba86 commit e96e516

11 files changed

+268
-102
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `laravel-api-client` will be documented in this file
44

5+
## 1.0.12 - 2020-05-27
6+
7+
- Updates to many things.
8+
59
## 1.0.11 - 2020-05-18
610

711
- Initial Public Release

README.md

+30-19
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ composer require macsidigital/laravel-api-client
1414

1515
1.0 - Laravel 5.5 - 5.8 - Bug fixes only, mainly there for backward compatibility, if there are any issues then create a pull request.
1616

17-
2.0 - Laravel 6.0 - Maintanined, again feel free to create pull requests. This is open source which is a 2 way street.
17+
2.0 - Laravel 6.0 - Maintained, again feel free to create pull requests. This is open source which is a 2 way street.
1818

19-
3.0 - Laravel 7.0 - Maintanined, again feel free to create pull requests. This is open source which is a 2 way street. Reason why it only uses Laravel 7 is because we hook into the new HTTP/Client and some other models in Laravel, thankfully created by Taylor Otwell.
19+
3.0 - Laravel 7.0 - Maintained, again feel free to create pull requests. This is open source which is a 2 way street.
2020

2121
## Usage
2222

@@ -189,12 +189,14 @@ Xero uses a patch request for creating models and post request for updating mode
189189

190190
## Retreiving models
191191

192-
You can retrieve a single model by either using the find method and passing an ID, or an array of ID's, or by running a query and returning first();
192+
You can retrieve a single model by either using the find method and passing an ID, or an array of ID's, or by running a query and returning first() or last();
193193

194194
```php
195195
$user = API::user()->find('ID');
196196

197-
$user = API::user()->where('Name', 'Bob')->first();
197+
$user = API::user()->where('Name', 'Bob')->first(); // First occurence
198+
199+
$user = API::user()->where('Name', 'Bob')->last(); // Last occurence
198200
```
199201

200202
You can also use get and all to retreive many models, this will return a Result Set, which is an enhanced Laravel Collection.
@@ -317,17 +319,17 @@ We utilise Laravel's hasAttributes trait in the models so you should be able to
317319

318320
### Resources
319321

320-
These are generally gesources that are returned as relationships of other models but do not interact with the API directly. To create one of these extend the MacsiDigital/API/Support/Resource.
322+
These are generally gesources that are returned as relationships of other models but do not interact with the API directly. To create one extend the MacsiDigital/API/Support/Resource.
321323

322-
THis should only be used on models that are returned as a sub array of a called model.
324+
This should only be used on models that are returned as a sub array of a called model.
323325

324-
### ApiResources
326+
### API Resources
325327

326-
These are models that will interact directly with an API, or indirectly through a parent model. To create one of these extend the MacsiDigital/API/Support/APIResource.
328+
These are models that will interact directly with an API, or indirectly through a parent model. To create one extend the MacsiDigital/API/Support/APIResource.
327329

328330
If you want to roll your own then you need to ensure you add the MacsiDigital\API\Traits\InteractsWithAPI trait. Also follow how our Support API resource works.
329331

330-
In these models we also variosu info we store, like primary key and api endpoints, these are the available attributes
332+
In these models we also house many variables, like primary key and api endpoints, these are the available attributes
331333

332334
```php
333335
// These will map to RESTful requests
@@ -353,7 +355,7 @@ In these models we also variosu info we store, like primary key and api endpoint
353355
// Also, some API's return 'users' for multiple and user for single, set teh multiple field below to wheat is required if different
354356
protected $apiMultipleDataField = 'data';
355357
```
356-
The apiData and apiMultiple fields dictate what fields that are being returned contains the resources, this should be 'data' in a good api but it really does vary. Zoom uses 'users' for multiple records and '' for single.
358+
The apiData and apiMultiple fields dictate what field in the response will house the main body data, this should be 'data' in a good api but it really does vary. Zoom uses 'users' for multiple records and '' for single.
357359

358360
If apiDataField is set to '' it will return the body direct.
359361

@@ -363,7 +365,7 @@ If apiDataField is set to '' it will return the body direct.
363365

364366
Xero also uses a different method.
365367

366-
In the case of Xero it can be overwritten in the getApiDatField function so that we dont have to set on all models.
368+
In the case of Xero it can be overwritten in the getApiMultipleDataField function so that we dont have to set on all models.
367369

368370
```php
369371
public function getApiMultipleDataField()
@@ -390,9 +392,18 @@ A quick note on endpoints, we can set an endpoint as 'users' but we can also inc
390392
protected $endPoint = 'users/{user:id}/settings';
391393
```
392394

395+
We can also set customEndpoints on a model for specific endPoints, if the endPoints dont follow convention.
396+
397+
```php
398+
protected $customEndPoints = [
399+
'get' => 'users/{user:id}/meetings',
400+
'post' => 'users/{user:id}/meetings'
401+
];
402+
```
403+
393404
### Relationships
394405

395-
We have tried to get the models as close to a Laravel model as we can, even down to how relationships work.
406+
We have tried to get the models as close to a Laravel model as we can, even down to how relationships work.
396407

397408
By default we will try to create relationship objects for any returned input if they are setup. However you can override this behaviour by setting LoadRaw to true in the model
398409

@@ -424,7 +435,7 @@ This could also be a HasMany relationship and the reverse on the address would b
424435
}
425436
```
426437

427-
A name and a field can be passed as a 2nd and 3rd argument.
438+
A name and a field can be passed as a 2nd and 3rd argument. A 4th argument can also passed which will be any fields and values in an array that should be passed onto all relationship models. This is handy when you need to track a parent's id on teh child model.
428439

429440
We try to automatically work out the name and field attributes if not passed for you based on the function name, so we will look for a field 'user_id' in the array 'users' in the above method. However not all API's use the same id naming so you can set the IDSuffix by adding this to your model.
430441

@@ -470,7 +481,7 @@ We utilise save, saveMany (has many only), create and createMany (has many only)
470481
$user = API::user()->find('id');
471482

472483
$user->address()->create([
473-
'address1' => '21 Test Street',
484+
'address1' => '17 Test Street',
474485
...
475486
]);
476487
```
@@ -494,7 +505,7 @@ When the relation models do not interact with the api then we expose new make()
494505
$user->address()->attach($address);
495506
```
496507

497-
This works well when relationships are returned direct as part of an API call, which is common in API's. However sometimes API's dont send these items direct adn therefore need different end point calls.
508+
This works well when relationships are returned direct as part of an API call, which is common in API's. However sometimes API's dont send these items direct and therefore need different end point calls.
498509

499510
In these cases we use custom relationship models
500511

@@ -504,7 +515,7 @@ First you need to extend either the HasOne or HasMany relationship models.
504515

505516
Within the extended model you need to create the logic for retreiving, creating, updating and deleting as is required by the API. Unfortunetly as these are all case specific you will need to create CustomRelations models for any implementation required.
506517

507-
To call it we call the hasCustom method and pass the model class as the 2nd parameter. Parameters 3 and 4 can still be the name and field.
518+
To call it we call the hasCustom method and pass the model class as the 2nd parameter. Parameters 3 and 4 can still be the name and field and 5 any fields and values to add to any new models.
508519

509520
```php
510521
public function addresses()
@@ -605,7 +616,7 @@ As arrays cant use symbols as keys we do some translating, so if '=' is called w
605616

606617
The default is called for any custom filters, so lets say you call where('name', 'StartsWith', 'Bob') this will call addWhereStartsWith and processWhereStartsWith methods
607618

608-
Now each API will handle filtering differently so the logic in these methods are to suit the API, here is an example for xero, who add all filters to a where query string. They allow the main ones and have 3 custom types, 'Contains' which is the same as a 'like' call, 'StartWith' and 'EndsWith'.
619+
Now each API will handle filtering differently so the logic in these methods are to suit the API, here is an example for Xero, who add all filters to a where query string. They allow the main ones and have 3 custom types, 'Contains' which is the same as a 'like' call, 'StartWith' and 'EndsWith'.
609620

610621
```php
611622
public function processEquals($detail)
@@ -720,7 +731,7 @@ Of course for these sorts of custom actions we have to update the allowed operan
720731

721732
## Creating and Updating
722733

723-
Generally in API's the attributes required for saving and updating are different, and they are in turn differnet to the attributes when a model is retreived. We therefore utilise 'Persistance' models which will house validation logic and the attributes required to make a successful save. So in our models we need to Extend the InteractsWithAPI trait and add the following 2 protected attributes.
734+
Generally in API's the attributes required for saving and updating are different, and they are in turn different to the attributes when a model is retrieved. We therefore utilise 'Persistance' models which will house validation logic and the attributes required to make a successful save. So in our models we need to Extend the InteractsWithAPI trait and add the following 2 protected attributes.
724735

725736
```php
726737
protected $insertResource = 'MacsiDigital\API\Dev\Resources\StoreAddress';
@@ -846,7 +857,7 @@ To save its as simple as calling the save() function.
846857
$user->save();
847858
```
848859

849-
THe model will see if it already exists and call the correct update method. If updating we will only pass dirty attributes to the persistance model.
860+
THe model will see if it already exists and call the correct insert or update method. If updating we will only pass dirty attributes to the persistance model.
850861

851862
You can also utilise other laravel methods like make, create and update directly in the model.
852863

src/Support/Builder.php

+53-18
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public function find($id, $column="")
175175
return $this->whereIn($id, $column)->get(null, $raw);
176176
}
177177
return $this->handleResponse($this->sendRequest('get', [
178-
$this->retreiveEndPoint('find').'/'.$id
178+
$this->retreiveEndPoint('find').'/'.$id,
179+
$this->combineQueries()
179180
]), "individual", "allow");
180181
}
181182

@@ -262,15 +263,21 @@ public function handle404($response, $ifEmpty)
262263
public function all()
263264
{
264265
$this->setPerPageToMax();
266+
if($this->resource->beforeQuery($this) === false){
267+
return;
268+
}
265269
return $this->handleResponse($this->sendRequest('get', [
266270
$this->retreiveEndPoint('get'),
267-
$this->addPagination([]),
271+
$this->addPagination($this->combineQueries()),
268272
]), 'all');
269273

270274
}
271275

272276
public function get()
273277
{
278+
if($this->resource->beforeQuery($this) === false){
279+
return;
280+
}
274281
return $this->handleResponse($this->sendRequest('get', [
275282
$this->retreiveEndPoint('get'),
276283
$this->addPagination($this->combineQueries())
@@ -279,61 +286,88 @@ public function get()
279286

280287
public function getOne()
281288
{
289+
if($this->resource->beforeQuery($this) === false){
290+
return;
291+
}
282292
return $this->handleResponse($this->sendRequest('get', [
283293
$this->retreiveEndPoint('get'),
284294
$this->addPagination($this->combineQueries())
285295
]), 'individual');
286296
}
287297

288-
public function post($attributes)
298+
public function post($attributes, $type="individual")
289299
{
300+
if($this->resource->beforePostQuery($this) === false){
301+
return;
302+
}
290303
return $this->handleResponse($this->sendRequest('post', [
291304
$this->retreiveEndPoint('post'),
292305
$attributes,
293306
$this->combineQueries()
294-
]));
307+
]), $type);
295308
}
296309

297-
public function patch($attributes)
310+
public function patch($attributes, $type="individual")
298311
{
312+
if($this->resource->beforePatchQuery($this) === false){
313+
return;
314+
}
299315
return $this->handleResponse($this->sendRequest('patch', [
300316
$this->retreiveEndPoint('patch'),
301317
$attributes,
302318
$this->combineQueries()
303-
]));
319+
]), $type);
304320
}
305321

306-
public function put($attributes)
322+
public function put($attributes, $type="individual")
307323
{
324+
if($this->resource->beforePutQuery($this) === false){
325+
return;
326+
}
308327
return $this->handleResponse($this->sendRequest('put', [
309328
$this->retreiveEndPoint('put'),
310329
$attributes,
311330
$this->combineQueries()
312-
]));
331+
]), $type);
313332
}
314333

315-
public function delete()
334+
public function delete($type="individual")
316335
{
317-
return $this->handleResponse($this->sendRequest('post', [
318-
$this->retreiveEndPoint('post'),
336+
if($this->resource->beforeDeleteQuery($this) === false){
337+
return;
338+
}
339+
return $this->handleResponse($this->sendRequest('delete', [
340+
$this->retreiveEndPoint('delete'),
319341
$this->combineQueries()
320-
]));
342+
]), $type);
321343
}
322344

323345
public function first()
324346
{
325347
return $this->get()->first();
326348
}
327349

328-
public function firstWhere($column, $value)
350+
public function last()
329351
{
330-
$this->where($column, $value);
352+
return $this->get()->last();
353+
}
354+
355+
public function firstWhere($column, $operand = null, $value = null)
356+
{
357+
$this->where($column, $operand, $value);
331358
return $this->first();
332359
}
333360

361+
public function addQuery($key, $value)
362+
{
363+
$this->queries[$key] = $value;
364+
return $this;
365+
}
366+
334367
public function whereRaw($column, $value)
335368
{
336-
$this->queries[$column] = $value;
369+
$this->addQuery($column, $value);
370+
return $this;
337371
}
338372

339373
public function where($column, $operand = null, $value = null)
@@ -405,7 +439,8 @@ public function whereIn(array $values, $column="")
405439
if($column == ''){
406440
$column = $this->resource->getKeyName().'s';
407441
}
408-
return $this->queries($column, $string);
442+
$this->addQuery($column, $string);
443+
return $this;
409444
}
410445

411446
public function orderBy($value, $column='order')
@@ -631,9 +666,9 @@ protected function processIndividualResponse($response)
631666
$data = $response->json();
632667
}
633668
if(isset($data[0])){
634-
return $this->resource->newFromBuilder($data[0]);
669+
return $this->resource->newFromBuilder($this->resource->passOnAttributes($data[0]));
635670
} else {
636-
return $this->resource->newFromBuilder($data);
671+
return $this->resource->newFromBuilder($this->resource->passOnAttributes($data));
637672
}
638673
}
639674

src/Support/Relations/BelongsTo.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class BelongsTo extends Relation
1212

1313
public $type = 'BelongsTo';
1414

15-
public function __construct($related, $owner, $name, $field)
15+
public function __construct($related, $owner, $name, $field, $updateFields = [])
1616
{
1717
$this->relatedClass = $related;
1818
$this->related = new $related($owner->client);

0 commit comments

Comments
 (0)