Skip to content

Commit

Permalink
Apply fixes from StyleCI (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
specialtactics authored Mar 26, 2019
1 parent 6843bbb commit 542fb06
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
35 changes: 20 additions & 15 deletions helpers/helpers.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<?php

if (!function_exists('APIUser')) {
function APIUser() {
if (! function_exists('APIUser')) {
function APIUser()
{
$user = app('Dingo\Api\Auth\Auth')->user();

return $user;
}
}

if (!function_exists('camel_case_array_keys')) {
if (! function_exists('camel_case_array_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 infiniately (null)
* @return array $array
*/
function camel_case_array_keys($array, $levels = null) {
function camel_case_array_keys($array, $levels = null)
{
foreach (array_keys($array) as $key) {
// Get a reference to the value of the key (avoid copy)
// Then remove that array element
Expand All @@ -42,15 +44,16 @@ 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
*
* @param $array
* @param int|null $levels How many levels of an array keys to transform - by default recurse infiniately (null)
* @return array $array
*/
function snake_case_array_keys(array $array, $levels = null) {
function snake_case_array_keys(array $array, $levels = null)
{
foreach (array_keys($array) as $key) {
// Get a reference to the value of the key (avoid copy)
// Then remove that array element
Expand All @@ -76,45 +79,47 @@ function snake_case_array_keys(array $array, $levels = null) {
}
}

if (!function_exists('class_basename')) {
if (! function_exists('class_basename')) {

/**
* Get the basename of a class's FQNS name. This is proven to be the fastest way to do this (for now).
*
* @param string $className
* @return string
*/
function class_basename(string $className) {
function class_basename(string $className)
{
$reflection = new ReflectionClass($className);

return $reflection->getShortName();
}
}

if (!function_exists('get_calling_method')) {
if (! function_exists('get_calling_method')) {
/**
* Get the calling method name
*
* @return string
*/
function get_calling_method() {
function get_calling_method()
{
return debug_backtrace()[1]['function'];
}
}


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
*
* @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
* @throws \Exception
*/
function model_relation_name($resourceName, $relationType = 'many') {
function model_relation_name($resourceName, $relationType = 'many')
{
if ($relationType == 'many') {
return lcfirst(str_plural(class_basename($resourceName)));
}
else if ($relationType == 'one') {
} elseif ($relationType == 'one') {
return lcfirst(class_basename($resourceName));
} else {
throw new \Exception('Undefined relation type');
Expand Down
30 changes: 16 additions & 14 deletions src/Transformers/RestfulTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use League\Fractal\TransformerAbstract;
use Specialtactics\L5Api\APIBoilerplate;
use Specialtactics\L5Api\Models\RestfulModel;
use Specialtactics\L5Api\Helpers\APIHelper;

class RestfulTransformer extends TransformerAbstract
{
Expand Down Expand Up @@ -40,9 +39,9 @@ public function transform($object)
* @param \stdClass $object
* @return array
*/
public function transformStdClass($object) {

$transformed = (array)$object;
public function transformStdClass($object)
{
$transformed = (array) $object;

/**
* Transform all keys to correct case, recursively
Expand All @@ -58,7 +57,8 @@ public function transformStdClass($object) {
* @param RestfulModel $model
* @return array
*/
public function transformRestfulModel(RestfulModel $model) {
public function transformRestfulModel(RestfulModel $model)
{
$this->model = $model;

// Begin the transformation!
Expand All @@ -73,11 +73,11 @@ public function transformRestfulModel(RestfulModel $model) {
return ! in_array($key, $filterOutAttributes);
}, ARRAY_FILTER_USE_KEY);

/**
/*
* Format all dates as Iso8601 strings, this includes the created_at and updated_at columns
*/
foreach ($model->getDates() as $dateColumn) {
if (!empty($model->$dateColumn) && !in_array($dateColumn, $filterOutAttributes)) {
if (! empty($model->$dateColumn) && ! in_array($dateColumn, $filterOutAttributes)) {
$transformed[$dateColumn] = $model->$dateColumn->toIso8601String();
}
}
Expand Down Expand Up @@ -144,7 +144,8 @@ protected function transformKeysCase(array $transformed)
* @param int|null $levels How many levels of an array keys to transform - by default recurse infiniately (null)
* @return array $transformed
*/
protected function formatKeyCase($input, $levels = null) {
protected function formatKeyCase($input, $levels = null)
{
$caseFormat = APIBoilerplate::getResponseCaseType();

if ($caseFormat == APIBoilerplate::CAMEL_CASE) {
Expand All @@ -153,7 +154,7 @@ protected function formatKeyCase($input, $levels = null) {
} else {
$transformed = camel_case($input);
}
} else if ($caseFormat == APIBoilerplate::SNAKE_CASE) {
} elseif ($caseFormat == APIBoilerplate::SNAKE_CASE) {
if (is_array($input)) {
$transformed = snake_case_array_keys($input, 1);
} else {
Expand All @@ -172,7 +173,8 @@ protected function formatKeyCase($input, $levels = null) {
*
* @return array Array of attributes to filter out
*/
protected function getFilteredOutAttributes() {
protected function getFilteredOutAttributes()
{
$filterOutAttributes = array_merge(
$this->model->getHidden(),
[
Expand All @@ -190,7 +192,8 @@ protected function getFilteredOutAttributes() {
* @param array $transformed
* @return array $transformed
*/
protected function transformRelations(array $transformed) {
protected function transformRelations(array $transformed)
{
// Iterate through all relations
foreach ($this->model->getRelations() as $relationKey => $relation) {
$transformedRelationKey = $this->formatKeyCase($relationKey);
Expand All @@ -201,9 +204,8 @@ protected function transformRelations(array $transformed) {
}

// Transform Collection
else if ($relation instanceof \Illuminate\Database\Eloquent\Collection) {
elseif ($relation instanceof \Illuminate\Database\Eloquent\Collection) {
if (count($relation->getIterator()) > 0) {

$relationModel = $relation->first();
$relationTransformer = $relationModel::getTransformer();

Expand All @@ -229,7 +231,7 @@ protected function transformRelations(array $transformed) {
}

// Transformed related model
else if ($relation instanceof RestfulModel) {
elseif ($relation instanceof RestfulModel) {
// Get transformer of relation model
$relationTransformer = $relation::getTransformer();

Expand Down

0 comments on commit 542fb06

Please sign in to comment.