Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from StyleCI #69

Merged
merged 1 commit into from
Jul 15, 2024
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
29 changes: 16 additions & 13 deletions helpers/helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

if (! function_exists('APIUser')) {
if (!function_exists('APIUser')) {
function APIUser()
{
$user = app('Dingo\Api\Auth\Auth')->user();
Expand All @@ -9,14 +9,15 @@ function APIUser()
}
}

if (! function_exists('camel_case_array_keys')) {
if (!function_exists('camel_case_array_keys')) {
/**
* Recursively camel-case an array's keys
* Recursively camel-case an array's keys.
*
* @deprecated Use Helpers.php
*
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
*
* @return array $array
*/
function camel_case_array_keys($array, $levels = null)
Expand All @@ -25,14 +26,15 @@ function camel_case_array_keys($array, $levels = null)
}
}

if (! function_exists('snake_case_array_keys')) {
if (!function_exists('snake_case_array_keys')) {
/**
* Recursively snake-case an array's keys
* Recursively snake-case an array's keys.
*
* @deprecated Use Helpers.php
*
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
*
* @return array $array
*/
function snake_case_array_keys(array $array, $levels = null)
Expand All @@ -41,14 +43,15 @@ function snake_case_array_keys(array $array, $levels = null)
}
}

if (! function_exists('model_relation_name')) {
if (!function_exists('model_relation_name')) {
/**
* Converts the name of a model class to the name of the relation of this resource on another model
* Converts the name of a model class to the name of the relation of this resource on another model.
*
* @deprecated Use Helpers.php
*
* @param string $resourceName The name of the resource we are dealing with
* @param string $relationType The type of relation - ie.. one to.. X ('one', 'many')
* @param string $resourceName The name of the resource we are dealing with
* @param string $relationType The type of relation - ie.. one to.. X ('one', 'many')
*
* @return string The name of the relation, as it would appear inside an eloquent model
*/
function model_relation_name($resourceName, $relationType = 'many')
Expand Down
31 changes: 17 additions & 14 deletions src/APIBoilerplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
class APIBoilerplate
{
/**
* Case type constants for configuring responses
* Case type constants for configuring responses.
*/
const CAMEL_CASE = 'camel-case';
const SNAKE_CASE = 'snake-case';
const DEFAULT_CASE = self::CAMEL_CASE;

/**
* Case type config path
* Case type config path.
*/
const CASE_TYPE_CONFIG_PATH = 'api.formatsOptions.caseType';

/**
* The header which can be used to override config provided case type
* The header which can be used to override config provided case type.
*/
const CASE_TYPE_HEADER = 'X-Accept-Case-Type';

Expand All @@ -30,21 +30,21 @@ class APIBoilerplate
protected static $requestedKeyCaseFormat = null;

/**
* Get the required 'case type' for transforming response data
* Get the required 'case type' for transforming response data.
*
* @return string
*/
public static function getResponseCaseType()
{
$format = static::$requestedKeyCaseFormat;

if (! is_null($format)) {
if (!is_null($format)) {
return $format;
}

// See if the client is requesting a specific case type
$caseFormat = request()->header(static::CASE_TYPE_HEADER, null);
if (! is_null($caseFormat)) {
if (!is_null($caseFormat)) {
if ($caseFormat == static::CAMEL_CASE) {
$format = static::CAMEL_CASE;
} elseif ($caseFormat == static::SNAKE_CASE) {
Expand Down Expand Up @@ -73,10 +73,11 @@ public static function getResponseCaseType()
}

/**
* Formats case of the input array or scalar to desired case
* Formats case of the input array or scalar to desired case.
*
* @param array|string $input
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
*
* @param array|string $input
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array|string $transformed
*/
public static function formatKeyCaseAccordingToResponseFormat($input, $levels = null)
Expand All @@ -87,7 +88,7 @@ public static function formatKeyCaseAccordingToResponseFormat($input, $levels =
}

// Use the other function for strings
if (! is_array($input)) {
if (!is_array($input)) {
return static::formatCaseAccordingToResponseFormat($input);
}

Expand All @@ -106,9 +107,10 @@ public static function formatKeyCaseAccordingToResponseFormat($input, $levels =
}

/**
* Format the provided string into the required case response format, for attributes (ie. keys)
* Format the provided string into the required case response format, for attributes (ie. keys).
*
* @param string $attributeString
*
* @param string $attributeString
* @return string
*/
public static function formatCaseAccordingToResponseFormat($attributeString)
Expand All @@ -125,11 +127,12 @@ public static function formatCaseAccordingToResponseFormat($attributeString)
}

/**
* Format the provided key string into the required case response format
* Format the provided key string into the required case response format.
*
* @deprecated Use the updated function name
*
* @param string $key
* @param string $key
*
* @return string
*/
public static function formatKeyCaseAccordingToReponseFormat($value)
Expand Down
33 changes: 19 additions & 14 deletions src/APIHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
class APIHelpers
{
/**
* Recursively camel-case an array's keys
* Recursively camel-case an array's keys.
*
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
*
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
public static function camelCaseArrayKeys($array, $levels = null)
Expand Down Expand Up @@ -40,10 +41,11 @@ public static function camelCaseArrayKeys($array, $levels = null)
}

/**
* Recursively snake-case an array's keys
* Recursively snake-case an array's keys.
*
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
*
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infinitely (null)
* @return array $array
*/
public static function snakeCaseArrayKeys(array $array, $levels = null)
Expand Down Expand Up @@ -74,9 +76,10 @@ public static function snakeCaseArrayKeys(array $array, $levels = null)

/**
* 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
*
* @param string $value
* @return string
*/
public static function camel($value)
Expand All @@ -91,9 +94,10 @@ public static function camel($value)

/**
* Str::snake 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
*
* @param string $value
* @return mixed|string|string[]|null
*/
public static function snake($value)
Expand All @@ -113,7 +117,7 @@ public static function snake($value)
}

/**
* Get the calling method name
* Get the calling method name.
*
* @return string
*/
Expand All @@ -123,10 +127,11 @@ public static function getCallingMethod()
}

/**
* Converts the name of a model class to the name of the relation of this resource on another model
* Converts the name of a model class to the name of the relation of this resource on another model.
*
* @param string $resourceName The name of the resource we are dealing with
* @param string $relationType The type of relation - ie.. one to.. X ('one', 'many')
*
* @param string $resourceName The name of the resource we are dealing with
* @param string $relationType The type of relation - ie.. one to.. X ('one', 'many')
* @return string The name of the relation, as it would appear inside an eloquent model
*/
public static function modelRelationName($resourceName, $relationType = 'many')
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Commands/Laravel/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class ControllerMakeCommand extends \Illuminate\Routing\Console\ControllerMakeCo
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @param string $stub
*
* @return string
*/
protected function resolveStubPath($stub)
{
$boilerplateStub = __DIR__. '/../../../../resources' .$stub;
$boilerplateStub = __DIR__.'/../../../../resources'.$stub;

return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Commands/Laravel/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class ModelMakeCommand extends \Illuminate\Foundation\Console\ModelMakeCommand
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @param string $stub
*
* @return string
*/
protected function resolveStubPath($stub)
{
$boilerplateStub = __DIR__. '/../../../../resources' .$stub;
$boilerplateStub = __DIR__.'/../../../../resources'.$stub;

return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Commands/Laravel/PolicyMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class PolicyMakeCommand extends \Illuminate\Foundation\Console\PolicyMakeCommand
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @param string $stub
*
* @return string
*/
protected function resolveStubPath($stub)
{
$boilerplateStub = __DIR__. '/../../../../resources' .$stub;
$boilerplateStub = __DIR__.'/../../../../resources'.$stub;

return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Commands/Laravel/SeederMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ class SeederMakeCommand extends \Illuminate\Database\Console\Seeds\SeederMakeCom
/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @param string $stub
*
* @return string
*/
protected function resolveStubPath($stub)
{
$boilerplateStub = __DIR__. '/../../../../resources' .$stub;
$boilerplateStub = __DIR__.'/../../../../resources'.$stub;

return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
Expand Down
28 changes: 14 additions & 14 deletions src/Console/Commands/MakeApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle()

// Conditionally create policy
if ($this->anticipate('Would you like to create a policy for this resource?', ['yes', 'no']) == 'yes') {
$policyName = '../Models/Policies/' . $name . 'Policy';
$policyName = '../Models/Policies/'.$name.'Policy';
$this->call('make:policy', ['name' => $policyName, '-m' => $name]);
}

Expand All @@ -67,12 +67,12 @@ public function handle()

// Conditionally create seeder
if ($this->anticipate('Would you like to create a Seeder for this resource?', ['yes', 'no']) == 'yes') {
$seederName = Str::plural($name) . 'Seeder';
$seederName = Str::plural($name).'Seeder';

$this->call('make:seeder', ['name' => $seederName]);

$this->line('Please add the following to your DatabaseSeeder.php file', 'important');
$this->line('$this->call('. $seederName .'::class);', 'code');
$this->line('$this->call('.$seederName.'::class);', 'code');
$this->line(PHP_EOL);
}

Expand All @@ -84,25 +84,25 @@ public function handle()

$sectionName = Str::pluralStudly($name);
$routePrefix = Str::plural(Str::kebab($name));
$controllerName = $name . 'Controller';
$controllerName = $name.'Controller';

$exampleRoutes =
'/*' . PHP_EOL .
' * ' . $sectionName . PHP_EOL .
' */' . PHP_EOL .
'$api->group([\'prefix\' => \''. $routePrefix .'\'], function (Router $api) {' . PHP_EOL .
' $api->get(\'/\', \'App\Http\Controllers\\'. $controllerName .'@getAll\');' . PHP_EOL .
' $api->get(\'/{uuid}\', \'App\Http\Controllers\\'. $controllerName .'@get\');' . PHP_EOL .
' $api->post(\'/\', \'App\Http\Controllers\\'. $controllerName .'@post\');' . PHP_EOL .
' $api->patch(\'/{uuid}\', \'App\Http\Controllers\\'. $controllerName .'@patch\');' . PHP_EOL .
' $api->delete(\'/{uuid}\', \'App\Http\Controllers\\'. $controllerName .'@delete\');' . PHP_EOL .
'/*'.PHP_EOL.
' * '.$sectionName.PHP_EOL.
' */'.PHP_EOL.
'$api->group([\'prefix\' => \''.$routePrefix.'\'], function (Router $api) {'.PHP_EOL.
' $api->get(\'/\', \'App\Http\Controllers\\'.$controllerName.'@getAll\');'.PHP_EOL.
' $api->get(\'/{uuid}\', \'App\Http\Controllers\\'.$controllerName.'@get\');'.PHP_EOL.
' $api->post(\'/\', \'App\Http\Controllers\\'.$controllerName.'@post\');'.PHP_EOL.
' $api->patch(\'/{uuid}\', \'App\Http\Controllers\\'.$controllerName.'@patch\');'.PHP_EOL.
' $api->delete(\'/{uuid}\', \'App\Http\Controllers\\'.$controllerName.'@delete\');'.PHP_EOL.
'});';

$this->line($exampleRoutes, 'code');
}

/**
* Setup styles for command
* Setup styles for command.
*/
protected function setupStyles()
{
Expand Down
Loading
Loading