Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a45a5a8
Added Created, Updated and Deleted events, Moved request to each meth…
craigAtCD Oct 8, 2019
3616e39
Apply fixes from StyleCI
phpsa Oct 8, 2019
61fea0d
Merge pull request #10 from phpsa/analysis-XWEwaP
phpsa Oct 8, 2019
3c5e170
Scrutinizer updates
craigAtCD Oct 8, 2019
c8bb9db
Merge pull request #9 from phpsa/feature/events
phpsa Oct 10, 2019
ae915b1
Update controller.stub
phpsa Oct 10, 2019
d5c5ba3
Feature/requests mapping (#13)
phpsa Oct 23, 2019
c91e2f4
Merge branch 'master' into develop
phpsa Oct 23, 2019
e5e00b9
Apply fixes from StyleCI (#15)
phpsa Oct 23, 2019
bf98e94
Fixed namespace type declaration
phpsa Oct 23, 2019
3d553af
Fixed namespace type declaration
phpsa Oct 23, 2019
c2a09cf
Fixed namespace type declaration
phpsa Oct 23, 2019
e77a6e9
Feature/requests mapping (#17)
phpsa Nov 12, 2019
e91159e
Feature/policy collections update (#19)
phpsa Nov 12, 2019
d939faf
Feature/policy collections update (#22)
phpsa Nov 19, 2019
6bf75c5
Merge branch 'master' into develop
phpsa Nov 19, 2019
12f70d0
Apply fixes from StyleCI (#27)
phpsa Nov 19, 2019
d24fca6
Policiies auth mehtod without users
craigAtCD Nov 20, 2019
bd3f789
user not needed
craigAtCD Nov 20, 2019
c1ef7df
Tidy up uri parser class
craigAtCD Nov 26, 2019
bf62767
Fixes
craigAtCD Nov 26, 2019
7c134b6
fix more
craigAtCD Nov 26, 2019
be69b35
Feature/policy collections update (#29)
phpsa Nov 28, 2019
72e8f5d
Feature/policy collections update (#32)
phpsa Nov 28, 2019
14fa0dd
Merge branch 'refs/heads/master' into develop
craigAtCD Nov 28, 2019
887ee52
csfixer run on code
craigAtCD Nov 28, 2019
6fc9f22
Missing file updated
craigAtCD Nov 28, 2019
c079e26
Includes should be returnable
craigAtCD Nov 28, 2019
30709f1
static not defined
craigAtCD Nov 28, 2019
7de3a3f
Allowed fields updated
craigAtCD Nov 28, 2019
32272b0
minor updates
craigAtCD Dec 6, 2019
26eb667
Merge branch 'master' into develop
craigAtCD Dec 16, 2019
f15462a
Logic inversion (#35)
samatcd Dec 16, 2019
6a1e78d
Api Exception update
craigAtCD Dec 16, 2019
1e56a37
Fixed query param optionals
craigAtCD Dec 17, 2019
e80a9cc
updated unit tests
craigAtCD Dec 17, 2019
029c827
Updated travis ci config / requirements to laravel / php versions
craigAtCD Dec 17, 2019
d382e5a
Put mismatched method back in
craigAtCD Dec 17, 2019
edb9806
Apply fixes from StyleCI (#36)
phpsa Dec 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ notifications:
on_failure: always

php:
- 7.1
- 7.2
- 7.3
- 7.4

env:
- LARAVEL_VERSION=5.8.*
- LARAVEL_VERSION=6.*

before_install:
- travis_retry composer self-update --stable -n
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ By default all fields are returned, you can limit that to specific fields in the
* Api Controller parameter `$defaultFields` default as `protected $defaultFields = ['*'];` - switch to include an array of fields
* fields param in url querystring: ie `fields=id,name,age` = will only return those, this will also override the above.
* in your response resource you can set the static::allowedFields to lock down which fields are returnable
* addfields and removefields params in url querystring will work with these.
* `addfields` and `removefields` params in url querystring will work with these.
* Use laravel [eloquent model `$appends`](https://laravel.com/docs/6.x/eloquent-serialization#appending-values-to-json) property to automatically include custom attribute accessors.

## Relationships

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
}
],
"require": {
"php": ">=7.1.3",
"illuminate/support": "~5.8.0|^6.0"
"php": ">=7.2",
"illuminate/support": "^6.0"
},
"require-dev": {
"orchestra/testbench": "~3.8.0",
"phpunit/phpunit": "^7.0"
"orchestra/testbench": "^4.0",
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 13 additions & 0 deletions src/Contracts/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ protected function getUriParser($request)
return self::$uriParser;
}

/**
* Method to add extra request parameters to the request instance.
*
* @param mixed $request
* @param array $extraParams
*/
protected function addCustomParams($request, array $extraParams = []): void
{
$all = $request->all();
$new = array_merge_recursive($all, $extraParams);
$request->replace($new);
}

/**
* Parses our include joins.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Contracts/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ protected function rulesForUpdate(/* @scrutinizer ignore-unused */$id): array
*
* @param mixed $role role name or array of role names
*
* @deprecated -- should be rather used in policies / gates
*
* @return bool
*/
protected function hasRole($role): bool
Expand All @@ -73,6 +75,8 @@ protected function hasRole($role): bool
*
* @param array $roles
*
* @deprecated -- should be rather used in policies / gates
*
* @return bool
*/
protected function hasAllRoles(array $roles): bool
Expand Down
5 changes: 3 additions & 2 deletions src/Generator/stubs/controller.stub
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ class {{controller.name}} extends Controller
/**
* Store a newly created resource in storage.
* POST /api/{resource}.
*
* @param Request $request
*
* @param Request $request
*
* @see self::handleStoreOrUpdateAction to do magic insert / update
* @return Response
*/
public function store(Request $request)
Expand Down
14 changes: 11 additions & 3 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,18 @@ public static function filterFieldsFromRequest($request, ?array $defaultFields,
*
* @return array
*/
public static function excludeArrayValues(array $array, array $excludes, ?array $optionals = []): array
public static function excludeArrayValues(array $array, array $excludes, ?array $acceptable = []): array
{
return Arr::where($array, function ($value) use ($excludes, $optionals) {
return ! in_array($value, $excludes) || in_array($value, $optionals);
return Arr::where($array, function ($value) use ($excludes, $acceptable) {
if (in_array($value, $excludes)) {
return false;
}

if (! empty($acceptable)) {
return in_array($value, $acceptable);
}

return true;
});
}
}
24 changes: 18 additions & 6 deletions src/Http/Controllers/Api/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ public function __construct()
*
* @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request
*/
public function handleIndexAction($request)
public function handleIndexAction($request, array $extraParams = [])
{
$this->addCustomParams($request, $extraParams);
$this->validateRequestType($request);
$this->authoriseUserAction('viewAny');
$this->getUriParser($request);
Expand All @@ -124,14 +125,23 @@ public function handleIndexAction($request)
return $this->respondWithMany($items);
}

public function handleStoreOrUpdateAction($request, array $extraParams = [])
{
$key = self::$model->getKeyName();
$id = $request->input($key, null) ?? data_get($extraParams, $key, null);

return $id ? $this->handleUpdateAction($id, $request, $extraParams) : $this->handleStoreAction($request, $extraParams);
}

/**
* Store a newly created resource in storage.
* POST /api/{resource}.
*
* @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request
*/
public function handleStoreAction($request)
public function handleStoreAction($request, array $extraParams = [])
{
$this->addCustomParams($request, $extraParams);
$this->validateRequestType($request);
$this->authoriseUserAction('create');

Expand Down Expand Up @@ -168,7 +178,7 @@ public function handleStoreAction($request)
} catch (\Illuminate\Database\QueryException $exception) {
$message = config('app.debug') ? $exception->getMessage() : 'Failed to create Record';

throw new ApiException($message);
throw new ApiException($message, $exception->getCode(), $exception);
} catch (\Exception $exception) {
DB::rollback();

Expand All @@ -183,8 +193,9 @@ public function handleStoreAction($request)
* @param int $id
* @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request
*/
public function handleShowAction($id, $request)
public function handleShowAction($id, $request, array $extraParams = [])
{
$this->addCustomParams($request, $extraParams);
$this->validateRequestType($request);

$this->authoriseUserAction('view', self::$model::find($id));
Expand Down Expand Up @@ -212,8 +223,9 @@ public function handleShowAction($id, $request)
* @param int $id
* @param \Illuminate\Http\Request|\Illuminate\Foundation\Http\FormRequest $request
*/
public function handleUpdateAction($id, $request)
public function handleUpdateAction($id, $request, array $extraParams = [])
{
$this->addCustomParams($request, $extraParams);
$this->validateRequestType($request);

$this->authoriseUserAction('update', self::$model::find($id));
Expand Down Expand Up @@ -260,7 +272,7 @@ public function handleUpdateAction($id, $request)
} catch (\Illuminate\Database\QueryException $exception) {
$message = config('app.debug') ? $exception->getMessage() : 'Failed to update Record';

throw new ApiException($message);
throw new ApiException($message, $exception->getCode(), $exception);
} catch (\Exception $exception) {
DB::rollback();

Expand Down
90 changes: 90 additions & 0 deletions tests/LaravelApiHelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Phpsa\LaravelApiController\Tests;

use Orchestra\Testbench\TestCase;
use Phpsa\LaravelApiController\Facades\LaravelApiController;
use Phpsa\LaravelApiController\Helpers;
use Phpsa\LaravelApiController\ServiceProvider;

class LaravelApiHelpersTest extends TestCase
{
protected function getPackageProviders($app)
{
return [ServiceProvider::class];
}

protected function getPackageAliases($app)
{
return [
'laravel-api-controller' => LaravelApiController::class,
];
}

public function testSnakeCasing()
{
$array = [
'test_one' => 'test_one',
'testTwo' => 'test_two',
];

$transposed = Helpers::snakeCaseArrayKeys($array);

$this->assertSame(array_keys($transposed), array_values($array));
}

public function testCamelCasing()
{
$array = [
'test_one' => 'testOne',
'testTwo' => 'testTwo',
];

$transposed = Helpers::camelCaseArrayKeys($array);

$this->assertSame(array_keys($transposed), array_values($array));
}

public function testArrayExcludes()
{
$allowedFields = [
'field1',
'field2',
'field3',
'field4',
'field5',
];

$excludeFields = [
'field2',
];

$inputData = [
'field1',
'field2',
'field3',
'field4',
'field5',
'field6',
];

$remaining = Helpers::excludeArrayValues($inputData, $excludeFields, $allowedFields);

$this->assertSame([
'field1',
'field3',
'field4',
'field5',
], array_values($remaining));

$remaining = Helpers::excludeArrayValues($inputData, [], $allowedFields);

$this->assertSame([
'field1',
'field2',
'field3',
'field4',
'field5',
], array_values($remaining));
}
}