diff --git a/src/Contracts/Parser.php b/src/Contracts/Parser.php index aab44ff..47a785c 100644 --- a/src/Contracts/Parser.php +++ b/src/Contracts/Parser.php @@ -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); } @@ -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() : ['*']; } /** diff --git a/src/Contracts/Relationships.php b/src/Contracts/Relationships.php index 3da2f38..7c152f9 100644 --- a/src/Contracts/Relationships.php +++ b/src/Contracts/Relationships.php @@ -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 { diff --git a/src/Helpers.php b/src/Helpers.php index 49df0a2..266c68b 100644 --- a/src/Helpers.php +++ b/src/Helpers.php @@ -8,7 +8,7 @@ class Helpers { /** - * CamelCases an array + * CamelCases an array. * * @param array $array * @@ -22,7 +22,7 @@ public static function camelCaseArray(array $array): array } /** - * Snake cases an array + * Snake cases an array. * * @param array $array * @@ -36,7 +36,7 @@ public static function snakeCaseArray(array $array): array } /** - * camel cases array keys + * camel cases array keys. * * @param array $array * @@ -62,7 +62,7 @@ public static function camelCaseArrayKeys(array $array): array } /** - * Snake cases array keys + * Snake cases array keys. * * @param array $array * @@ -88,7 +88,7 @@ public static function snakeCaseArrayKeys(array $array): array } /** - * Convert to snake + * Convert to snake. * * @param string $value * @@ -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; + } } diff --git a/src/Http/Controllers/Api/Controller.php b/src/Http/Controllers/Api/Controller.php index 679cb6b..bb07ae8 100644 --- a/src/Http/Controllers/Api/Controller.php +++ b/src/Http/Controllers/Api/Controller.php @@ -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) { diff --git a/src/Http/Resources/ApiCollection.php b/src/Http/Resources/ApiCollection.php index 4692905..29d069c 100644 --- a/src/Http/Resources/ApiCollection.php +++ b/src/Http/Resources/ApiCollection.php @@ -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; diff --git a/src/Http/Resources/Contracts/AllowableFields.php b/src/Http/Resources/Contracts/AllowableFields.php index ae5263d..d015bad 100644 --- a/src/Http/Resources/Contracts/AllowableFields.php +++ b/src/Http/Resources/Contracts/AllowableFields.php @@ -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 */ @@ -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; } @@ -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; } @@ -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 */