Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/Contracts/Parser.php
Original file line number Diff line number Diff line change
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
20 changes: 11 additions & 9 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 @@ -180,24 +180,26 @@ public static function excludeArrayValues(array $array, array $excludes, ?array

public static function array_merge_request($main, ...$arrays): array
{
foreach($arrays as $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 ) {
foreach ($newValues as $key => $value) {
if (is_array($value)) {
if (!isset($array[$key])) {
$array[$key] = array();
if (! isset($array[$key])) {
$array[$key] = [];
}
$array[$key] = self::array_merge_replace($array[$key], $value);
} else {
$array[$key] = $value;
}
}

return $array;
}
}
1 change: 1 addition & 0 deletions src/Http/Controllers/Api/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public function handleDestroyAction($id, $request)
} catch (ModelNotFoundException $exeption) {
return $this->errorNotFound('Record does not exist');
}

return $this->respondNoContent();
}
}
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