Skip to content

Commit 24313a7

Browse files
phpsasamatcd
andauthored
Request merging should not change type (#41)
* Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Feature/requests mapping (#17) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#18) * Feature/policy collections update (#19) * Develop (#14) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Develop To Master (#16) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Feature/policy collections update (#22) * Develop (#14) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Develop To Master (#16) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Added middleware and query qualifiers * Policy Update * Post adding new and artisan updated, code cleanup * Apply fixes from StyleCI (#21) * Scrutiniser fixes * Put now handles relations * Fix return types * Minor updates to fix relationships and make command * Sanke case vs camel case * Apply fixes from StyleCI (#23) * Minor bugfix for case sensitifity * Fixed make command * model is static * Apply fixes from StyleCI (#24) * Scrutiniser fixes * Apply fixes from StyleCI (#25) * Apply fixes from StyleCI (#27) * Policiies auth mehtod without users * user not needed * Tidy up uri parser class * Fixes * fix more * Feature/policy collections update (#29) * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Added middleware and query qualifiers * Policy Update * Post adding new and artisan updated, code cleanup * Apply fixes from StyleCI (#21) * Scrutiniser fixes * Put now handles relations * Fix return types * Minor updates to fix relationships and make command * Sanke case vs camel case * Apply fixes from StyleCI (#23) * Minor bugfix for case sensitifity * Fixed make command * model is static * Apply fixes from StyleCI (#24) * Scrutiniser fixes * policies - for unauthed users * Minor updates * STYLEFIXES * fixed phpcs rule * Apply fixes from StyleCI (#31) * Feature/policy collections update (#32) * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Added middleware and query qualifiers * Policy Update * Post adding new and artisan updated, code cleanup * Apply fixes from StyleCI (#21) * Scrutiniser fixes * Put now handles relations * Fix return types * Minor updates to fix relationships and make command * Sanke case vs camel case * Apply fixes from StyleCI (#23) * Minor bugfix for case sensitifity * Fixed make command * model is static * Apply fixes from StyleCI (#24) * Scrutiniser fixes * policies - for unauthed users * Minor updates * STYLEFIXES * fixed phpcs rule * Apply fixes from StyleCI (#31) * minor updates * csfixer run on code * Missing file updated * Includes should be returnable * static not defined * Allowed fields updated * minor updates * Logic inversion (#35) * Develop (#33) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Feature/requests mapping (#17) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#18) * Feature/policy collections update (#19) * Develop (#14) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Develop To Master (#16) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Feature/policy collections update (#22) * Develop (#14) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Develop To Master (#16) * Added Created, Updated and Deleted events, Moved request to each method to allow overloading: * Apply fixes from StyleCI * Scrutinizer updates * Update controller.stub * Feature/requests mapping (#13) * Event accepts request object and any type of request can now be pushed through * Apply fixes from StyleCI (#12) * Request should have a constructor for instances with models * Update BaseRepository.php * Apply fixes from StyleCI (#15) * Fixed namespace type declaration * Fixed namespace type declaration * Fixed namespace type declaration * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Added middleware and query qualifiers * Policy Update * Post adding new and artisan updated, code cleanup * Apply fixes from StyleCI (#21) * Scrutiniser fixes * Put now handles relations * Fix return types * Minor updates to fix relationships and make command * Sanke case vs camel case * Apply fixes from StyleCI (#23) * Minor bugfix for case sensitifity * Fixed make command * model is static * Apply fixes from StyleCI (#24) * Scrutiniser fixes * Apply fixes from StyleCI (#25) * Apply fixes from StyleCI (#27) * Policiies auth mehtod without users * user not needed * Tidy up uri parser class * Fixes * fix more * Feature/policy collections update (#29) * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Added middleware and query qualifiers * Policy Update * Post adding new and artisan updated, code cleanup * Apply fixes from StyleCI (#21) * Scrutiniser fixes * Put now handles relations * Fix return types * Minor updates to fix relationships and make command * Sanke case vs camel case * Apply fixes from StyleCI (#23) * Minor bugfix for case sensitifity * Fixed make command * model is static * Apply fixes from StyleCI (#24) * Scrutiniser fixes * policies - for unauthed users * Minor updates * STYLEFIXES * fixed phpcs rule * Apply fixes from StyleCI (#31) * Feature/policy collections update (#32) * Added resource collections and policy check. * Updated Readme * Formatting * Apply fixes from StyleCI (#20) * Scrutinizer fixes - deprecation / namesapce * Added middleware and query qualifiers * Policy Update * Post adding new and artisan updated, code cleanup * Apply fixes from StyleCI (#21) * Scrutiniser fixes * Put now handles relations * Fix return types * Minor updates to fix relationships and make command * Sanke case vs camel case * Apply fixes from StyleCI (#23) * Minor bugfix for case sensitifity * Fixed make command * model is static * Apply fixes from StyleCI (#24) * Scrutiniser fixes * policies - for unauthed users * Minor updates * STYLEFIXES * fixed phpcs rule * Apply fixes from StyleCI (#31) * minor updates * csfixer run on code * Missing file updated * Includes should be returnable * static not defined * Allowed fields updated * minor updates * Logic inversion + Updated excludeArrayValues to exclude if not in optionals instead of always including. * Added accessor notes about $appends usage * Update controller with query modifier * Add extra query parser method * Include previous exception with ApiException * Api Exception update * Fixed query param optionals * updated unit tests * Updated travis ci config / requirements to laravel / php versions * Put mismatched method back in * Apply fixes from StyleCI (#36) * Fixed issue with parser uing query instead of input * work on response mapping / default fields * Fix issue where array_merge_request does not overwite and we need it to for our requests. * Apply fixes from StyleCI (#40) Co-authored-by: Sam Sehnert <[email protected]>
1 parent b7c91a1 commit 24313a7

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

src/Contracts/Parser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function getUriParser($request)
3232
protected function addCustomParams($request, array $extraParams = []): void
3333
{
3434
$all = $request->all();
35-
$new = array_merge_recursive($all, $extraParams);
35+
$new = Helpers::array_merge_request($all, $extraParams);
3636
$request->replace($new);
3737
}
3838

@@ -162,13 +162,13 @@ protected function setWhereClause($where): void
162162
}
163163

164164
/**
165-
* Gets our default fields for our query
165+
* Gets our default fields for our query.
166166
*
167167
* @return array
168168
*/
169169
protected function getDefaultFields(): array
170170
{
171-
return (method_exists($this->resourceSingle, 'getDefaultFields')) ? ($this->resourceSingle)::getDefaultFields() : ['*'];
171+
return (method_exists($this->resourceSingle, 'getDefaultFields')) ? ($this->resourceSingle)::getDefaultFields() : ['*'];
172172
}
173173

174174
/**

src/Contracts/Relationships.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Phpsa\LaravelApiController\Contracts;
44

55
use Illuminate\Support\Str;
6-
use Phpsa\LaravelApiController\Helpers;
76
use Phpsa\LaravelApiController\Exceptions\ApiException;
7+
use Phpsa\LaravelApiController\Helpers;
88

99
trait Relationships
1010
{

src/Helpers.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Helpers
99
{
1010
/**
11-
* CamelCases an array
11+
* CamelCases an array.
1212
*
1313
* @param array $array
1414
*
@@ -22,7 +22,7 @@ public static function camelCaseArray(array $array): array
2222
}
2323

2424
/**
25-
* Snake cases an array
25+
* Snake cases an array.
2626
*
2727
* @param array $array
2828
*
@@ -36,7 +36,7 @@ public static function snakeCaseArray(array $array): array
3636
}
3737

3838
/**
39-
* camel cases array keys
39+
* camel cases array keys.
4040
*
4141
* @param array $array
4242
*
@@ -62,7 +62,7 @@ public static function camelCaseArrayKeys(array $array): array
6262
}
6363

6464
/**
65-
* Snake cases array keys
65+
* Snake cases array keys.
6666
*
6767
* @param array $array
6868
*
@@ -88,7 +88,7 @@ public static function snakeCaseArrayKeys(array $array): array
8888
}
8989

9090
/**
91-
* Convert to snake
91+
* Convert to snake.
9292
*
9393
* @param string $value
9494
*
@@ -177,4 +177,29 @@ public static function excludeArrayValues(array $array, array $excludes, ?array
177177
return true;
178178
});
179179
}
180+
181+
public static function array_merge_request($main, ...$arrays): array
182+
{
183+
foreach ($arrays as $array) {
184+
$main = self::array_merge_replace($main, $array);
185+
}
186+
187+
return $main;
188+
}
189+
190+
public static function array_merge_replace(array $array, array $newValues): array
191+
{
192+
foreach ($newValues as $key => $value) {
193+
if (is_array($value)) {
194+
if (! isset($array[$key])) {
195+
$array[$key] = [];
196+
}
197+
$array[$key] = self::array_merge_replace($array[$key], $value);
198+
} else {
199+
$array[$key] = $value;
200+
}
201+
}
202+
203+
return $array;
204+
}
180205
}

src/Http/Controllers/Api/Controller.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,9 @@ public function handleUpdateAction($id, $request, array $extraParams = [])
283283
public function handleDestroyAction($id, $request)
284284
{
285285
$this->validateRequestType($request);
286-
287-
$this->authoriseUserAction('delete', self::$model::find($id));
288-
289286
try {
290-
$item = $this->repository->getById($id);
287+
$item = self::$model::findOrFail($id);
288+
$this->authoriseUserAction('delete', self::$model::find($id));
291289
$this->repository->deleteById($id);
292290
event(new Deleted($item, $request));
293291
} catch (ModelNotFoundException $exeption) {

src/Http/Resources/ApiCollection.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
namespace Phpsa\LaravelApiController\Http\Resources;
44

55
use Illuminate\Http\Resources\Json\ResourceCollection;
6-
use Phpsa\LaravelApiController\Http\Resources\ApiResource;
76

87
class ApiCollection extends ResourceCollection
98
{
10-
119
protected function collects()
1210
{
1311
return parent::collects() ?? ApiResource::class;

src/Http/Resources/Contracts/AllowableFields.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
trait AllowableFields
88
{
99
/**
10-
* Resources to be mapped (ie children)
10+
* Resources to be mapped (ie children).
1111
*
1212
* @var array|null
1313
*/
1414
protected static $mapResources = null;
1515

1616
/**
17-
* Default fields to return on request
17+
* Default fields to return on request.
1818
*
1919
* @var array|null
2020
*/
2121
protected static $defaultFields = null;
2222

2323
/**
24-
* Allowable fields to be used
24+
* Allowable fields to be used.
2525
*
2626
* @var array|null
2727
*/
@@ -49,13 +49,16 @@ protected function onlyAllowed($request): array
4949

5050
protected function mapRelatedResources($resources)
5151
{
52-
if (empty(static::$mapResources)) return $resources;
52+
if (empty(static::$mapResources)) {
53+
return $resources;
54+
}
5355

5456
foreach ($resources as $key => $value) {
5557
if (array_key_exists($key, static::$mapResources)) {
5658
$resources[$key] = static::$mapResources[$key]::make($value);
5759
}
5860
}
61+
5962
return $resources;
6063
}
6164

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

8487
return $this->filterAllowedFields($fields);
85-
86-
8788
}
8889

8990
public function filterAllowedFields($fields)
9091
{
91-
if(empty(static::$allowedFields) || static::$allowedFields === ['*'] ){
92+
if (empty(static::$allowedFields) || static::$allowedFields === ['*']) {
9293
return $fields;
9394
}
9495

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

110-
111111
/**
112-
* Return default fields for this collection
112+
* Return default fields for this collection.
113113
*
114114
* @return array
115115
*/

0 commit comments

Comments
 (0)