Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 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
785d5b1
Merge branch 'master' into develop
craigAtCD Dec 17, 2019
372f1ea
Fixed issue with parser uing query instead of input
craigAtCD Dec 18, 2019
a82d90d
work on response mapping / default fields
craigAtCD Dec 20, 2019
6df3092
Merge remote-tracking branch 'origin/master' into develop
craigAtCD Jan 19, 2020
72cd9e5
Fix issue where array_merge_request does not overwite and we need it …
craigAtCD Jan 19, 2020
1ef16b8
Apply fixes from StyleCI (#40)
phpsa Jan 20, 2020
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
6 changes: 3 additions & 3 deletions src/Contracts/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function getUriParser($request)
protected function addCustomParams($request, array $extraParams = []): void
{
$all = $request->all();
$new = array_merge_recursive($all, $extraParams);
$new = Helpers::array_merge_request($all, $extraParams);
$request->replace($new);
}

Expand Down Expand Up @@ -162,13 +162,13 @@ protected function setWhereClause($where): void
}

/**
* Gets our default fields for our query
* Gets our default fields for our query.
*
* @return array
*/
protected function getDefaultFields(): array
{
return (method_exists($this->resourceSingle, 'getDefaultFields')) ? ($this->resourceSingle)::getDefaultFields() : ['*'];
return (method_exists($this->resourceSingle, 'getDefaultFields')) ? ($this->resourceSingle)::getDefaultFields() : ['*'];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Phpsa\LaravelApiController\Contracts;

use Illuminate\Support\Str;
use Phpsa\LaravelApiController\Helpers;
use Phpsa\LaravelApiController\Exceptions\ApiException;
use Phpsa\LaravelApiController\Helpers;

trait Relationships
{
Expand Down
35 changes: 30 additions & 5 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Helpers
{
/**
* CamelCases an array
* CamelCases an array.
*
* @param array $array
*
Expand All @@ -22,7 +22,7 @@ public static function camelCaseArray(array $array): array
}

/**
* Snake cases an array
* Snake cases an array.
*
* @param array $array
*
Expand All @@ -36,7 +36,7 @@ public static function snakeCaseArray(array $array): array
}

/**
* camel cases array keys
* camel cases array keys.
*
* @param array $array
*
Expand All @@ -62,7 +62,7 @@ public static function camelCaseArrayKeys(array $array): array
}

/**
* Snake cases array keys
* Snake cases array keys.
*
* @param array $array
*
Expand All @@ -88,7 +88,7 @@ public static function snakeCaseArrayKeys(array $array): array
}

/**
* Convert to snake
* Convert to snake.
*
* @param string $value
*
Expand Down Expand Up @@ -177,4 +177,29 @@ public static function excludeArrayValues(array $array, array $excludes, ?array
return true;
});
}

public static function array_merge_request($main, ...$arrays): array
{
foreach ($arrays as $array) {
$main = self::array_merge_replace($main, $array);
}

return $main;
}

public static function array_merge_replace(array $array, array $newValues): array
{
foreach ($newValues as $key => $value) {
if (is_array($value)) {
if (! isset($array[$key])) {
$array[$key] = [];
}
$array[$key] = self::array_merge_replace($array[$key], $value);
} else {
$array[$key] = $value;
}
}

return $array;
}
}
6 changes: 2 additions & 4 deletions src/Http/Controllers/Api/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,9 @@ public function handleUpdateAction($id, $request, array $extraParams = [])
public function handleDestroyAction($id, $request)
{
$this->validateRequestType($request);

$this->authoriseUserAction('delete', self::$model::find($id));

try {
$item = $this->repository->getById($id);
$item = self::$model::findOrFail($id);
$this->authoriseUserAction('delete', self::$model::find($id));
$this->repository->deleteById($id);
event(new Deleted($item, $request));
} catch (ModelNotFoundException $exeption) {
Expand Down
2 changes: 0 additions & 2 deletions src/Http/Resources/ApiCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Phpsa\LaravelApiController\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;
use Phpsa\LaravelApiController\Http\Resources\ApiResource;

class ApiCollection extends ResourceCollection
{

protected function collects()
{
return parent::collects() ?? ApiResource::class;
Expand Down
18 changes: 9 additions & 9 deletions src/Http/Resources/Contracts/AllowableFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@
trait AllowableFields
{
/**
* Resources to be mapped (ie children)
* Resources to be mapped (ie children).
*
* @var array|null
*/
protected static $mapResources = null;

/**
* Default fields to return on request
* Default fields to return on request.
*
* @var array|null
*/
protected static $defaultFields = null;

/**
* Allowable fields to be used
* Allowable fields to be used.
*
* @var array|null
*/
Expand Down Expand Up @@ -49,13 +49,16 @@ protected function onlyAllowed($request): array

protected function mapRelatedResources($resources)
{
if (empty(static::$mapResources)) return $resources;
if (empty(static::$mapResources)) {
return $resources;
}

foreach ($resources as $key => $value) {
if (array_key_exists($key, static::$mapResources)) {
$resources[$key] = static::$mapResources[$key]::make($value);
}
}

return $resources;
}

Expand All @@ -82,13 +85,11 @@ protected function mapFields($request): array
$fields = Helpers::filterFieldsFromRequest($request, $defaultFields, $allowedFields);

return $this->filterAllowedFields($fields);


}

public function filterAllowedFields($fields)
{
if(empty(static::$allowedFields) || static::$allowedFields === ['*'] ){
if (empty(static::$allowedFields) || static::$allowedFields === ['*']) {
return $fields;
}

Expand All @@ -107,9 +108,8 @@ protected function getResourceFields(): array
return is_array($this->resource) ? $this->resource : $this->resource->getAttributes();
}


/**
* Return default fields for this collection
* Return default fields for this collection.
*
* @return array
*/
Expand Down