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
2 changes: 0 additions & 2 deletions src/Contracts/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ protected function getSortValue(): array
return is_array($sort) ? $sort : explode(',', $sort);
}



/**
* parses our filter parameters.
*/
Expand Down
23 changes: 11 additions & 12 deletions src/Contracts/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Phpsa\LaravelApiController\Contracts;

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

trait Relationships
{

/**
* Gets whitelisted methods
* Gets whitelisted methods.
*
* @return array
*/
Expand All @@ -19,7 +18,7 @@ protected function getIncludesWhitelist(): array
}

/**
* Gets blacklisted methods
* Gets blacklisted methods.
*
* @return array
*/
Expand All @@ -29,7 +28,7 @@ protected function getIncludesBlacklist(): array
}

/**
* is method blacklisted
* is method blacklisted.
*
* @param string $item
*
Expand All @@ -49,7 +48,7 @@ public function isBlacklisted($item)
*/
protected function filterAllowedIncludes(array $includes): array
{
return array_filter($includes, function ($item) {
return array_filter($includes, function ($item) {
$callable = method_exists(self::$model, $item);

if (! $callable) {
Expand All @@ -65,7 +64,7 @@ protected function filterAllowedIncludes(array $includes): array
return false;
}

return empty($this->getIncludesWhitelist()) && ! Str::startsWith($item, '_');;
return empty($this->getIncludesWhitelist()) && ! Str::startsWith($item, '_');
});
}

Expand All @@ -81,19 +80,19 @@ protected function storeRelated($item, $relateds, $data): void
$relation = $item->$with();
$type = class_basename(get_class($relation));

if(!in_array($type, ['HasOne','HasMany'])){
if (! in_array($type, ['HasOne', 'HasMany'])) {
throw new ApiException("$type mapping not implemented yet");
}

$collection = $type === 'HasOne' ? [$data [$with]]: $data[$with];
$collection = $type === 'HasOne' ? [$data [$with]] : $data[$with];
$this->repository->with($with);
$localKey = $relation->getLocalKeyName();

foreach($collection as $relatedRecord){
if(isset($relatedRecord[$localKey])){
foreach ($collection as $relatedRecord) {
if (isset($relatedRecord[$localKey])) {
$existanceCheck = [$localKey => $relatedRecord[$localKey]];
$item->$with()->updateOrCreate($existanceCheck, $relatedRecord);
}else{
} else {
$item->$with()->create($relatedRecord);
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Contracts/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

trait Validation
{

/**
* \Illuminate\Http\Request instance.
*
Expand Down
5 changes: 2 additions & 3 deletions src/Generator/ApiMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,10 @@ protected function createOptionals()
{
//dd($this->stubVariables);
if ($this->option('model') || $this->option('all')) {

$params = ['name' => $this->stubVariables['model']['fullName']];
if ($this->confirm('Would you like to create a Migration for this resource?') ){
if ($this->confirm('Would you like to create a Migration for this resource?')) {
$params['--migration'] = true;
if($this->confirm('Would you like to create a Seeder for this resource?')){
if ($this->confirm('Would you like to create a Seeder for this resource?')) {
$seederName = $this->stubVariables['model']['fullNameWithoutRoot'].'Seeder';
$this->call('make:seeder', ['name' => $seederName]);
$this->line('Please add the following to your DatabaseSeeder.php file', 'important');
Expand Down
13 changes: 6 additions & 7 deletions src/Helpers.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
<?php

namespace Phpsa\LaravelApiController;

use Illuminate\Support\Str;

class Helpers
{

public static function camelCaseArrayKeys(array $array): array
{
$keys = array_keys($array);
foreach($keys as $key){
foreach ($keys as $key) {
$value = &$array[$key]; //reference and not copy so that keeps any modifiers
unset($array[$key]);

if (is_array($value) ) {
if (is_array($value)) {
$value = self::camelCaseArrayKeys($value);
}

$newKey = self::camel($key);
$array[$newKey] = $value;
unset($value); //cleanup

}

return $array;
Expand Down Expand Up @@ -60,7 +59,7 @@ public static function snake(string $value): string

/**
* Str::camel wrapper - for specific extra functionality
* Note this is generally only applicable when dealing with API input/output key case
* Note this is generally only applicable when dealing with API input/output key case.
*
* @param string $value
* @return string
Expand All @@ -71,7 +70,7 @@ public static function camel($value)
if (strtoupper($value) === $value) {
return $value;
}

return Str::camel($value);
}

}
}
2 changes: 0 additions & 2 deletions src/Http/Controllers/Api/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ public function handleUpdateAction($id, $request)

$data = $this->qualifyUpdateQuery($data);


$columns = $this->getTableColumns();

$updates = array_intersect_key($data, array_flip($columns));
Expand Down Expand Up @@ -268,7 +267,6 @@ public function handleUpdateAction($id, $request)

return $this->errorWrongArgs($exception->getMessage());
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Middleware/SnakeCaseInputs.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function handle($request, Closure $next)
$this->processParamBag($request->request);

if ($request->isJson()) {
$this->processParamBag(/** @scrutinizer ignore-type */$request->json());
$this->processParamBag(/* @scrutinizer ignore-type */$request->json());
}
}

Expand Down
18 changes: 7 additions & 11 deletions src/Http/Resources/Contracts/CaseFormat.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?php

namespace Phpsa\LaravelApiController\Http\Resources\Contracts;

use Phpsa\LaravelApiController\Helpers;

trait CaseFormat
{

public function toArray($request)
{
$data = parent::toArray($request);

return $this->caseFormat($request, $data);
}

protected function caseFormat($request, $data)
{
switch(strtolower($request->header('X-Accept-Case-Type')))
{
case "camel":
case "camel-case":
switch (strtolower($request->header('X-Accept-Case-Type'))) {
case 'camel':
case 'camel-case':
return Helpers::camelCaseArrayKeys($data);
break;

case "snake":
case "snake-case":
case 'snake':
case 'snake-case':

return Helpers::snakeCaseArrayKeys($data);
break;
Expand All @@ -31,8 +31,4 @@ protected function caseFormat($request, $data)

return $data;
}


}

?>