From 927ee3a8426e45befdaa22861a9073648c4ab090 Mon Sep 17 00:00:00 2001 From: Sheikh Heera Date: Mon, 26 Jun 2017 16:07:34 +0600 Subject: [PATCH] Fixed psr2 formatting. --- src/Requent/Config/requent.php | 2 +- src/Requent/Facade/Requent.php | 10 +- src/Requent/Requent.php | 463 +++++++++--------- src/Requent/RequentServiceProvider.php | 50 +- src/Requent/Traits/QueryBuilderMethods.php | 16 +- src/Requent/Traits/QueryModifierMethods.php | 112 ++--- src/Requent/Traits/TransformerMethods.php | 22 +- src/Requent/Transformer/BaseTransformer.php | 14 +- .../Transformer/DefaultTransformer.php | 56 ++- src/Requent/Transformer/Transformer.php | 12 +- .../Transformer/TransformerException.php | 2 +- src/Requent/Transformer/TransformerHelper.php | 32 +- src/Requent/UrlParser/QueryStringParser.php | 14 +- tests/Requent/Models/Post.php | 20 +- tests/Requent/RequentTest.php | 1 - tests/Requent/Transformer/PostTransformer.php | 16 +- tests/Requent/Transformer/UserTransformer.php | 24 +- tests/bootstrap.php | 2 +- 18 files changed, 440 insertions(+), 428 deletions(-) diff --git a/src/Requent/Config/requent.php b/src/Requent/Config/requent.php index 42d5d45..8632b26 100644 --- a/src/Requent/Config/requent.php +++ b/src/Requent/Config/requent.php @@ -62,4 +62,4 @@ | by default unless you override it here by setting the value to false. */ 'select_default_attributes' => true, -]; \ No newline at end of file +]; diff --git a/src/Requent/Facade/Requent.php b/src/Requent/Facade/Requent.php index 4c8c0aa..f1ecb49 100644 --- a/src/Requent/Facade/Requent.php +++ b/src/Requent/Facade/Requent.php @@ -6,8 +6,8 @@ class Requent extends Facade { - public static function getFacadeAccessor() - { - return 'requent'; - } -} \ No newline at end of file + public static function getFacadeAccessor() + { + return 'requent'; + } +} diff --git a/src/Requent/Requent.php b/src/Requent/Requent.php index b33c00d..de338fd 100644 --- a/src/Requent/Requent.php +++ b/src/Requent/Requent.php @@ -12,83 +12,84 @@ use Requent\Traits\QueryBuilderMethods; use Illuminate\Database\Eloquent\Builder; use Requent\Transformer\DefaultTransformer; + class Requent { - use QueryBuilderMethods; - - /** - * The resource Model/QueryBuilder instance - * @var Mixed - */ - protected $model = null; - - /** - * The final builder instance after building the query - * @var Illuminate\Database\Eloquent\Builder - */ - protected $builder = null; - - /** - * The request query string parameters - * @var Array - */ - protected $queryString = null; - - /** - * The transformer to transform the result - * @var Mixed - */ - protected $transformer = null; - - /** - * The key to wrap the collection - * @var String - */ - protected $resourceKey = null; - - /** - * The selected columns - * @var array - */ - protected $selectedColumns = []; - - /** - * Don't transform the query result - * @var boolean - */ - protected $original = false; - - /** - * Construct the Requent Object - * @param Array|null $queryString - * @return void - */ - public function __construct(Array $config = [], Array $queryString = [], Array $selectedColumns) - { - $this->config = $config; - $this->queryString = $queryString; - $this->selectedColumns = $selectedColumns; - } - - /** - * Set the resource and transformer for the query - * @param Mixed $model Model Name|Object - * @param Mixed $transformer Transform Name|Object - * @return $this - */ - public function resource($model, $transformer = DefaultTransformer::class) + use QueryBuilderMethods; + + /** + * The resource Model/QueryBuilder instance + * @var Mixed + */ + protected $model = null; + + /** + * The final builder instance after building the query + * @var Illuminate\Database\Eloquent\Builder + */ + protected $builder = null; + + /** + * The request query string parameters + * @var Array + */ + protected $queryString = null; + + /** + * The transformer to transform the result + * @var Mixed + */ + protected $transformer = null; + + /** + * The key to wrap the collection + * @var String + */ + protected $resourceKey = null; + + /** + * The selected columns + * @var array + */ + protected $selectedColumns = []; + + /** + * Don't transform the query result + * @var boolean + */ + protected $original = false; + + /** + * Construct the Requent Object + * @param Array|null $queryString + * @return void + */ + public function __construct(array $config = [], array $queryString = [], array $selectedColumns) + { + $this->config = $config; + $this->queryString = $queryString; + $this->selectedColumns = $selectedColumns; + } + + /** + * Set the resource and transformer for the query + * @param Mixed $model Model Name|Object + * @param Mixed $transformer Transform Name|Object + * @return $this + */ + public function resource($model, $transformer = DefaultTransformer::class) { - return $this->init($model, $transformer)->buildQuery(); + return $this->init($model, $transformer)->buildQuery(); } /** - * Return the original data without transforming - * @return $this - */ + * Return the original data without transforming + * @return $this + */ public function raw() { - $this->original = true; - return $this; + $this->original = true; + return $this; } /** @@ -96,11 +97,11 @@ public function raw() * @param Mixed $transformer * @return $this */ - public function transformBy($transformer) + public function transformBy($transformer) { - $this->original = false; - $this->transformer = $transformer; - return $this; + $this->original = false; + $this->transformer = $transformer; + return $this; } /** @@ -110,8 +111,8 @@ public function transformBy($transformer) */ public function keyBy($key) { - $this->resourceKey = $key; - return $this; + $this->resourceKey = $key; + return $this; } /** @@ -121,47 +122,47 @@ public function keyBy($key) * @return $this */ protected function init($model, $transformer) - { - $this->transformer = $transformer; - if(!is_null($model)) { - $this->model = is_string($model) ? new $model : $model; - } - return $this; - } - - /** - * Build the query from request - * @return $this - */ - protected function buildQuery() - { - $this->builder = $this->filter( - $this->getBuilder(), - $this->getQueryableData() - ); - return $this; - } - - /** - * Get the query parameter data (key/value pair) - * @return Array - */ - protected function getQueryableData() - { - $key = $this->getConfigValue('query_identifier'); - return [$key => $this->selectedColumns]; - } - - /** - * Get the resource for building the query - * @return Illuminate\Database\Eloquent\Builder - */ + { + $this->transformer = $transformer; + if (!is_null($model)) { + $this->model = is_string($model) ? new $model : $model; + } + return $this; + } + + /** + * Build the query from request + * @return $this + */ + protected function buildQuery() + { + $this->builder = $this->filter( + $this->getBuilder(), + $this->getQueryableData() + ); + return $this; + } + + /** + * Get the query parameter data (key/value pair) + * @return Array + */ + protected function getQueryableData() + { + $key = $this->getConfigValue('query_identifier'); + return [$key => $this->selectedColumns]; + } + + /** + * Get the resource for building the query + * @return Illuminate\Database\Eloquent\Builder + */ protected function getBuilder() { - if ($this->model instanceof Builder) { - return $this->model; - } - return $this->model->query(); + if ($this->model instanceof Builder) { + return $this->model; + } + return $this->model->query(); } /** @@ -170,108 +171,110 @@ protected function getBuilder() * @param Mixed $values * @return Illuminate\Database\Eloquent\Builder */ - protected function filter($query, $values) - { - foreach ($values as $method => $value) { - if($this->methodExists($method)) { - call_user_func_array([$this, $method], [$query, $value]); - } - } - return $query; - } - - /** - * Check available methods that are callable to build the query - * @param String $method - * @return Mixed - */ - protected function methodExists($method) - { - if(!$this->isQueryBuilderMethod($method)) { - return method_exists($this, $method) || $this->isFieldParser($method); - } - return false; - } - - /** - * Check if the method belongs to QueryBuilder - * @param String $method - * @return Boolean - */ - protected function isQueryBuilderMethod($method) - { - return in_array($method, get_class_methods(QueryBuilderMethods::class)); - } - - /** - * Check if the field parser method is being called - * @param String $method - * @return Boolean - */ - protected function isFieldParser($method) - { - return $method == $this->getConfigValue('query_identifier'); - } - - /** - * Add query constraints for relation - * @param Illuminate\Database\Eloquent\Builder - * @param Array - * @return Illuminate\Database\Eloquent\Builder - */ - protected function addFields($query, $fields) - { - foreach((Array) $fields as $field => $value) { - $this->addField($query, $field, $value); - } - return $query; - } - - /** - * Add query constraint for relation - * @param Illuminate\Database\Eloquent\Builder - * @param String Field Name - * @param Mixed - * @return Illuminate\Database\Eloquent\Builder - */ - protected function addField($query, $field, $value) - { - if($method = $this->getRelationShipName($query, $field)) { - if($value === true) { - $query->with($method); - } else { - $query->with([$method => function($query) use ($value) { - $this->filter($query, $value); - }]); - } - } - } - - /** - * Get the relationship method name - * @param Illuminate\Database\Eloquent\Builder - * @param String - * @return Mixed - */ - protected function getRelationShipName($query, $method) - { - if(method_exists($query->getModel(), $method)) { - return $method; - } - } - - /** + protected function filter($query, $values) + { + foreach ($values as $method => $value) { + if ($this->methodExists($method)) { + call_user_func_array([$this, $method], [$query, $value]); + } + } + return $query; + } + + /** + * Check available methods that are callable to build the query + * @param String $method + * @return Mixed + */ + protected function methodExists($method) + { + if (!$this->isQueryBuilderMethod($method)) { + return method_exists($this, $method) || $this->isFieldParser($method); + } + return false; + } + + /** + * Check if the method belongs to QueryBuilder + * @param String $method + * @return Boolean + */ + protected function isQueryBuilderMethod($method) + { + return in_array($method, get_class_methods(QueryBuilderMethods::class)); + } + + /** + * Check if the field parser method is being called + * @param String $method + * @return Boolean + */ + protected function isFieldParser($method) + { + return $method == $this->getConfigValue('query_identifier'); + } + + /** + * Add query constraints for relation + * @param Illuminate\Database\Eloquent\Builder + * @param Array + * @return Illuminate\Database\Eloquent\Builder + */ + protected function addFields($query, $fields) + { + foreach ((Array) $fields as $field => $value) { + $this->addField($query, $field, $value); + } + return $query; + } + + /** + * Add query constraint for relation + * @param Illuminate\Database\Eloquent\Builder + * @param String Field Name + * @param Mixed + * @return Illuminate\Database\Eloquent\Builder + */ + protected function addField($query, $field, $value) + { + if ($method = $this->getRelationShipName($query, $field)) { + if ($value === true) { + $query->with($method); + } else { + $query->with([$method => function ($query) use ($value) { + $this->filter($query, $value); + }]); + } + } + } + + /** + * Get the relationship method name + * @param Illuminate\Database\Eloquent\Builder + * @param String + * @return Mixed + */ + protected function getRelationShipName($query, $method) + { + if (method_exists($query->getModel(), $method)) { + return $method; + } + } + + /** * Get query string parameter * @param String $key * @return Mixed */ protected function getQueryStringValue($key = null) { - if(!$key) return $this->queryString; + if (!$key) { + return $this->queryString; + } - if(isset($this->queryString[$key])) { - return $this->queryString[$key]; - } + if (isset($this->queryString[$key])) { + return $this->queryString[$key]; + } } /** @@ -281,28 +284,30 @@ protected function getQueryStringValue($key = null) */ protected function getConfigValue($key = null) { - if(!$key) return $this->config; + if (!$key) { + return $this->config; + } - if(isset($this->config[$key])) { - return $this->config[$key]; - } + if (isset($this->config[$key])) { + return $this->config[$key]; + } } /** - * Catch missing methods - * @param String $method - * @param Array $params - * @return Mixed - * @throws BadMethodCallException - */ - public function __call($method, $params) - { - if($this->isFieldParser($method)) { - return $this->addFields(...$params); - } elseif ($method == 'model') { - return $this->resource(...$params); - } - - throw new BadMethodCallException("Call to undefined method {$method}"); - } + * Catch missing methods + * @param String $method + * @param Array $params + * @return Mixed + * @throws BadMethodCallException + */ + public function __call($method, $params) + { + if ($this->isFieldParser($method)) { + return $this->addFields(...$params); + } elseif ($method == 'model') { + return $this->resource(...$params); + } + + throw new BadMethodCallException("Call to undefined method {$method}"); + } } diff --git a/src/Requent/RequentServiceProvider.php b/src/Requent/RequentServiceProvider.php index 7262108..eff8afc 100644 --- a/src/Requent/RequentServiceProvider.php +++ b/src/Requent/RequentServiceProvider.php @@ -7,32 +7,32 @@ class RequentServiceProvider extends ServiceProvider { - public function register() - { - $this->mergeConfigFrom( __DIR__.'/Config/requent.php', 'requent'); - } + public function register() + { + $this->mergeConfigFrom(__DIR__.'/Config/requent.php', 'requent'); + } - public function boot() - { - $requent = $this->makeRequentInstance(); + public function boot() + { + $requent = $this->makeRequentInstance(); - $this->app->instance('requent', $requent); - - $this->app->instance(Requent::class, $requent); + $this->app->instance('requent', $requent); + + $this->app->instance(Requent::class, $requent); - $this->publishes([ - __DIR__.'/Config/requent.php' => config_path('requent.php'), - ], 'config'); - } + $this->publishes([ + __DIR__.'/Config/requent.php' => config_path('requent.php'), + ], 'config'); + } - protected function makeRequentInstance() - { - $config = $this->app->config->get('requent'); - $key = $config['query_identifier']; - $query = $this->app->request->query(); - $parsedArray = Parser::parse( - isset($query[$key]) ? $query[$key] : '', $key - ); - return new Requent($config, $query, $parsedArray); - } -} \ No newline at end of file + protected function makeRequentInstance() + { + $config = $this->app->config->get('requent'); + $key = $config['query_identifier']; + $query = $this->app->request->query(); + $parsedArray = Parser::parse( + isset($query[$key]) ? $query[$key] : '', $key + ); + return new Requent($config, $query, $parsedArray); + } +} diff --git a/src/Requent/Traits/QueryBuilderMethods.php b/src/Requent/Traits/QueryBuilderMethods.php index 4c6f61d..a85c78c 100644 --- a/src/Requent/Traits/QueryBuilderMethods.php +++ b/src/Requent/Traits/QueryBuilderMethods.php @@ -4,8 +4,8 @@ trait QueryBuilderMethods { - use QueryModifierMethods, TransformerMethods; - + use QueryModifierMethods, TransformerMethods; + /** * Proxy to query builder methods (get/find) * @param Mixed $id @@ -23,9 +23,9 @@ public function fetch($id = null, $columns = ['*']) * @param Array $columns * @return Array (Transformed Result) */ - public function get($columns = ['*']) + public function get($columns = ['*']) { - if(($perPage = $this->perPage()) || $this->isPaged()) { + if (($perPage = $this->perPage()) || $this->isPaged()) { return $this->callPaginate($perPage, $columns); } return $this->transform($this->builder->get($columns)); @@ -97,7 +97,7 @@ protected function isPaged() /** * Check if the paginated result is required - * and find out the amount for the per page + * and find out the amount for the per page * @return Mixed */ protected function perPage() @@ -119,7 +119,7 @@ protected function perPage() */ protected function callPaginate($perPage, $columns) { - if($this->getPaginator() != 'simple') { + if ($this->getPaginator() != 'simple') { return $this->paginate($perPage, $columns); } return $this->simplePaginate($perPage, $columns); @@ -138,7 +138,7 @@ protected function getPaginator() } /** - * Normalize the method parameters + * Normalize the method parameters * @param Int $perPage * @param Mixed $columns * @return Array @@ -149,4 +149,4 @@ protected function normalizeParameters($perPage, $columns = null) $perPage = is_array($perPage) ? null : $perPage; return [$perPage, $columns]; } -} \ No newline at end of file +} diff --git a/src/Requent/Traits/QueryModifierMethods.php b/src/Requent/Traits/QueryModifierMethods.php index 735425f..afeee8d 100644 --- a/src/Requent/Traits/QueryModifierMethods.php +++ b/src/Requent/Traits/QueryModifierMethods.php @@ -4,69 +4,69 @@ trait QueryModifierMethods { - /** - * Proxy the orderBy method in QueryBuilder - * @param Illuminate\Database\Eloquent\Builder $query - * @param String $value - * @return Illuminate\Database\Eloquent\Builder - */ - protected function orderBy($query, $value) - { - return $query->orderBy($value); - } + /** + * Proxy the orderBy method in QueryBuilder + * @param Illuminate\Database\Eloquent\Builder $query + * @param String $value + * @return Illuminate\Database\Eloquent\Builder + */ + protected function orderBy($query, $value) + { + return $query->orderBy($value); + } - /** - * Proxy the orderByDesc method in QueryBuilder - * @param Illuminate\Database\Eloquent\Builder $query - * @param String $value - * @return Illuminate\Database\Eloquent\Builder - */ - protected function orderByDesc($query, $value) - { - return $query->orderBy($value, 'desc'); - } + /** + * Proxy the orderByDesc method in QueryBuilder + * @param Illuminate\Database\Eloquent\Builder $query + * @param String $value + * @return Illuminate\Database\Eloquent\Builder + */ + protected function orderByDesc($query, $value) + { + return $query->orderBy($value, 'desc'); + } - /** - * Proxy the skip method in QueryBuilder - * @param Illuminate\Database\Eloquent\Builder $query - * @param Int $value - * @return Illuminate\Database\Eloquent\Builder - */ - protected function skip($query, $value) - { - return $query->skip($value); - } + /** + * Proxy the skip method in QueryBuilder + * @param Illuminate\Database\Eloquent\Builder $query + * @param Int $value + * @return Illuminate\Database\Eloquent\Builder + */ + protected function skip($query, $value) + { + return $query->skip($value); + } - /** - * Proxy the offset method in QueryBuilder - * @param Illuminate\Database\Eloquent\Builder $query - * @param Int $value - * @return Illuminate\Database\Eloquent\Builder - */ - protected function offset($query, $value) - { - return $query->offset($value); - } + /** + * Proxy the offset method in QueryBuilder + * @param Illuminate\Database\Eloquent\Builder $query + * @param Int $value + * @return Illuminate\Database\Eloquent\Builder + */ + protected function offset($query, $value) + { + return $query->offset($value); + } - /** - * Proxy the take method in QueryBuilder - * @param Illuminate\Database\Eloquent\Builder $query - * @param Int $value - * @return Illuminate\Database\Eloquent\Builder - */ + /** + * Proxy the take method in QueryBuilder + * @param Illuminate\Database\Eloquent\Builder $query + * @param Int $value + * @return Illuminate\Database\Eloquent\Builder + */ protected function take($query, $value) { return $query->take($value); } /** - * Proxy the limit method in QueryBuilder - * @param Illuminate\Database\Eloquent\Builder $query - * @param Int $value - * @return Illuminate\Database\Eloquent\Builder - */ - protected function limit($query, $value) - { - return $query->limit($value); - } -} \ No newline at end of file + * Proxy the limit method in QueryBuilder + * @param Illuminate\Database\Eloquent\Builder $query + * @param Int $value + * @return Illuminate\Database\Eloquent\Builder + */ + protected function limit($query, $value) + { + return $query->limit($value); + } +} diff --git a/src/Requent/Traits/TransformerMethods.php b/src/Requent/Traits/TransformerMethods.php index 5dd26e6..9c8c793 100644 --- a/src/Requent/Traits/TransformerMethods.php +++ b/src/Requent/Traits/TransformerMethods.php @@ -15,14 +15,16 @@ trait TransformerMethods */ protected function transform($result) { - if($this->original) return $result; + if ($this->original) { + return $result; + } $transformed = $this->resolveTransformer()->transformResult( $result, $this->getSelectables($this->selectedColumns) ); - if(!$this->isPaginated($result)) { - if($resourceKey = $this->getResourceKey()) { + if (!$this->isPaginated($result)) { + if ($resourceKey = $this->getResourceKey()) { return [$resourceKey => $transformed]; } } @@ -36,7 +38,7 @@ protected function transform($result) */ protected function resolveTransformer() { - if(is_string($this->transformer)) { + if (is_string($this->transformer)) { $this->transformer = new $this->transformer; } @@ -50,7 +52,7 @@ protected function resolveTransformer() */ protected function getSelectables($selectables) { - if($this->transformer instanceof Transformer) { + if ($this->transformer instanceof Transformer) { return $this->transformer; } @@ -65,10 +67,10 @@ protected function getSelectables($selectables) protected function getFieldsForDefaultTransformer($selectables) { $paramName = $this->getConfigValue('query_identifier'); - foreach($selectables as $field => $value) { - if($value !== true) { - if(isset($value[$paramName])) { - if(!empty($value[$paramName])) { + foreach ($selectables as $field => $value) { + if ($value !== true) { + if (isset($value[$paramName])) { + if (!empty($value[$paramName])) { $selectables[$field] = $this->{__FUNCTION__}($value[$paramName]); } else { unset($selectables[$field]); @@ -99,4 +101,4 @@ protected function getResourceKey() { return $this->resourceKey; } -} \ No newline at end of file +} diff --git a/src/Requent/Transformer/BaseTransformer.php b/src/Requent/Transformer/BaseTransformer.php index eb4f464..575ab89 100644 --- a/src/Requent/Transformer/BaseTransformer.php +++ b/src/Requent/Transformer/BaseTransformer.php @@ -9,7 +9,7 @@ class BaseTransformer { - /** + /** * Config * @var Array */ @@ -19,7 +19,7 @@ class BaseTransformer * Set config * @param Array $config */ - public function setConfig(Array $config) + public function setConfig(array $config) { $this->config = $config; return $this; @@ -32,9 +32,11 @@ public function setConfig(Array $config) */ protected function getConfigValue($key = null) { - if(!$key) return $this->config; + if (!$key) { + return $this->config; + } - if(isset($this->config[$key])) { + if (isset($this->config[$key])) { return $this->config[$key]; } } @@ -51,7 +53,7 @@ protected function isPaginated($data) /** * Transform the result given by QueryBuilder - * + * * @param Mixed $result * @param Array $transformer * @return Array @@ -67,4 +69,4 @@ public function transformResult($result, $transformer) } return $result; } -} \ No newline at end of file +} diff --git a/src/Requent/Transformer/DefaultTransformer.php b/src/Requent/Transformer/DefaultTransformer.php index e3b6d73..2929cac 100644 --- a/src/Requent/Transformer/DefaultTransformer.php +++ b/src/Requent/Transformer/DefaultTransformer.php @@ -39,19 +39,21 @@ protected function transformItems($result, $selectables) return $result->toArray(); } - /** - * Transform a single item/model - * @param Mixed $model - * @param Mixed $selectables - * @return Mixed - * @throws Requent\Transformer\TransformerException - */ - protected function transformItem($model, $selectables) - { - if(is_array($model)) return $model; + /** + * Transform a single item/model + * @param Mixed $model + * @param Mixed $selectables + * @return Mixed + * @throws Requent\Transformer\TransformerException + */ + protected function transformItem($model, $selectables) + { + if (is_array($model)) { + return $model; + } // If nothing was selected through query string - if(!$selectables || !is_array($selectables)) { + if (!$selectables || !is_array($selectables)) { return $model->toArray(); } @@ -69,15 +71,15 @@ protected function transformItem($model, $selectables) } return $result; - } + } - /** - * Check if a given attribute is available - * @param Illuminate\Database\Eloquent\Model $model - * @param String $key - * @return Boolean - */ - protected function hasAttribute($model, $key) + /** + * Check if a given attribute is available + * @param Illuminate\Database\Eloquent\Model $model + * @param String $key + * @return Boolean + */ + protected function hasAttribute($model, $key) { return array_key_exists( $key, $model->getAttributes() @@ -93,7 +95,7 @@ protected function hasAttribute($model, $key) protected function getAttribute($model, $key) { $attribute = $model->getAttributeValue($key); - if($attribute instanceof Carbon) { + if ($attribute instanceof Carbon) { return $attribute->toDateTimeString(); } return $attribute; @@ -107,11 +109,11 @@ protected function getAttribute($model, $key) */ protected function getSelectbles($selectables, $model) { - if($this->getConfigValue('select_default_attributes')) { - if(!array_diff_key($selectables, $relations = $model->getRelations())) { - return array_merge(array_diff_key($model->toArray(), $relations), $selectables); - } - } - return $selectables; + if ($this->getConfigValue('select_default_attributes')) { + if (!array_diff_key($selectables, $relations = $model->getRelations())) { + return array_merge(array_diff_key($model->toArray(), $relations), $selectables); + } + } + return $selectables; } -} \ No newline at end of file +} diff --git a/src/Requent/Transformer/Transformer.php b/src/Requent/Transformer/Transformer.php index 36d64ff..e197dd2 100644 --- a/src/Requent/Transformer/Transformer.php +++ b/src/Requent/Transformer/Transformer.php @@ -13,7 +13,7 @@ abstract class Transformer extends BaseTransformer * @param Illuminate\Database\Eloquent\Model $model * @return Array Transformed Result */ - public abstract function transform($model); + abstract public function transform($model); /** * Transforms paginated result. @@ -54,7 +54,9 @@ public function transformItems($result, $transformer) */ public function transformItem($model, $transformer) { - if(!$model || is_array($model)) return $model; + if (!$model || is_array($model)) { + return $model; + } $transformed = $this->make($transformer)->transform($model); return $this->transformRelations( $model, $this->make($transformer), $transformed @@ -71,7 +73,7 @@ public function transformItem($model, $transformer) protected function transformRelations($model, $transformer, $transformed) { foreach ($model->getRelations() as $key => $relation) { - if(method_exists($transformer, $key)) { + if (method_exists($transformer, $key)) { $transformed[snake_case($key)] = $this->transformRelation( $transformer, $key, $relation ); @@ -111,11 +113,11 @@ protected function make($transformer) */ public function __call($method, $params) { - if(in_array($method, ['item', 'items'])) { + if (in_array($method, ['item', 'items'])) { $method = 'transform'.studly_case($method); return $this->{strtolower($method)}(...$params); } throw new TransformerException('Undefined method '.get_called_class().':'.$method); } -} \ No newline at end of file +} diff --git a/src/Requent/Transformer/TransformerException.php b/src/Requent/Transformer/TransformerException.php index 6f5b14a..96320fd 100644 --- a/src/Requent/Transformer/TransformerException.php +++ b/src/Requent/Transformer/TransformerException.php @@ -16,4 +16,4 @@ public function __construct($message, $code = 0, Exception $previous = null) $this->previous = $previous; } } -} \ No newline at end of file +} diff --git a/src/Requent/Transformer/TransformerHelper.php b/src/Requent/Transformer/TransformerHelper.php index 8efe76a..e664c1c 100644 --- a/src/Requent/Transformer/TransformerHelper.php +++ b/src/Requent/Transformer/TransformerHelper.php @@ -8,24 +8,24 @@ trait TransformerHelper { - /** - * A helper method to transform plain result - * @param Mixed $result - * @param instanceof Requent\Transformer\Transformer $transformer - * @param string $resourceKey - * @return Array - */ - protected function transform($result, $transformer, $resourceKey = null) - { - $transformer = is_string($transformer) ? new $transformer : $transformer; - $transformed = $transformer->transformResult($result, $transformer); - if(!$this->isPaginated($result) && !is_null($resourceKey)) { - return [$resourceKey => $transformed]; + /** + * A helper method to transform plain result + * @param Mixed $result + * @param instanceof Requent\Transformer\Transformer $transformer + * @param string $resourceKey + * @return Array + */ + protected function transform($result, $transformer, $resourceKey = null) + { + $transformer = is_string($transformer) ? new $transformer : $transformer; + $transformed = $transformer->transformResult($result, $transformer); + if (!$this->isPaginated($result) && !is_null($resourceKey)) { + return [$resourceKey => $transformed]; } return $transformed; - } + } - /** + /** * Determine whether the data is paginated * @param Mixed $data * @return Boolean @@ -34,4 +34,4 @@ protected function isPaginated($data) { return ($data instanceof Paginator || $data instanceof LengthAwarePaginator); } -} \ No newline at end of file +} diff --git a/src/Requent/UrlParser/QueryStringParser.php b/src/Requent/UrlParser/QueryStringParser.php index 754d843..1514c98 100644 --- a/src/Requent/UrlParser/QueryStringParser.php +++ b/src/Requent/UrlParser/QueryStringParser.php @@ -9,7 +9,7 @@ public static function parse($input, $paramName = 'fields') return (new static)->parseValue(str_split($input), $paramName); } - protected function parseValue(Array $chars, $paramName = 'fields') + protected function parseValue(array $chars, $paramName = 'fields') { $end = count($chars); $current = ''; @@ -62,7 +62,7 @@ protected function parseValue(Array $chars, $paramName = 'fields') return $output; } - protected function getNestedLength(Array $chars, $offset) + protected function getNestedLength(array $chars, $offset) { $nested = 0; $end = count($chars); @@ -70,7 +70,7 @@ protected function getNestedLength(Array $chars, $offset) while ($i < $end) { $char = $chars[$i]; switch ($char) { - case '{'; + case '{': $nested++; break; case '}': @@ -85,7 +85,7 @@ protected function getNestedLength(Array $chars, $offset) throw new \Exception('Nested operator {} do not match'); } - protected function getMethodLength(Array $chars, $offset) + protected function getMethodLength(array $chars, $offset) { $pos = strpos(join($chars), ')', $offset); if ($pos > $offset) { @@ -95,7 +95,7 @@ protected function getMethodLength(Array $chars, $offset) } } - protected function parseMethod(Array $chars, $paramName) + protected function parseMethod(array $chars, $paramName) { try { $str = join($chars); @@ -109,8 +109,8 @@ protected function parseMethod(Array $chars, $paramName) default: return [$methodName, $param]; } - } catch(\Exception $e) { + } catch (\Exception $e) { throw new \InvalidArgumentException('Method operator () do not match'); } } -} \ No newline at end of file +} diff --git a/tests/Requent/Models/Post.php b/tests/Requent/Models/Post.php index 2945643..171941a 100755 --- a/tests/Requent/Models/Post.php +++ b/tests/Requent/Models/Post.php @@ -8,15 +8,15 @@ class Post extends Model { - protected $guarded = ['id']; - - public function user() - { - return $this->belongsTo(User::class); - } + protected $guarded = ['id']; + + public function user() + { + return $this->belongsTo(User::class); + } - public function comments() - { - return $this->hasMany(Comment::class); - } + public function comments() + { + return $this->hasMany(Comment::class); + } } diff --git a/tests/Requent/RequentTest.php b/tests/Requent/RequentTest.php index aa3bacf..071fd0f 100755 --- a/tests/Requent/RequentTest.php +++ b/tests/Requent/RequentTest.php @@ -9,7 +9,6 @@ use Requent\Transformer\UserTransformer; use Illuminate\Database\Eloquent\Collection; - class RequentTest extends TestCase { public function testBasic() diff --git a/tests/Requent/Transformer/PostTransformer.php b/tests/Requent/Transformer/PostTransformer.php index 6cc83ae..a7ced73 100644 --- a/tests/Requent/Transformer/PostTransformer.php +++ b/tests/Requent/Transformer/PostTransformer.php @@ -6,11 +6,11 @@ class PostTransformer extends AbstractTransformer { - public function transform($model) - { - return [ - 'title' => $model->title, - 'body' => $model->body - ]; - } -} \ No newline at end of file + public function transform($model) + { + return [ + 'title' => $model->title, + 'body' => $model->body + ]; + } +} diff --git a/tests/Requent/Transformer/UserTransformer.php b/tests/Requent/Transformer/UserTransformer.php index 8ae5957..3bb7b05 100644 --- a/tests/Requent/Transformer/UserTransformer.php +++ b/tests/Requent/Transformer/UserTransformer.php @@ -7,16 +7,16 @@ class UserTransformer extends AbstractTransformer { - public function transform($model) - { - return [ - 'name' => $model->name, - 'email' => $model->email - ]; - } + public function transform($model) + { + return [ + 'name' => $model->name, + 'email' => $model->email + ]; + } - public function posts($collection) - { - return $this->items($collection, PostTransformer::class); - } -} \ No newline at end of file + public function posts($collection) + { + return $this->items($collection, PostTransformer::class); + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8430b7f..22bdeff 100755 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,3 +1,3 @@