Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:Napp/apicore
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Requests/ApiRequest.php
#	src/Router/Provider/RouterServiceProvider.php
#	src/Transformers/ApiTransformer.php
  • Loading branch information
Mads Møller committed Jul 2, 2019
2 parents efd685a + e8bd1b9 commit 083a970
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ _ide_helper.php
.phpstorm.meta.php
coverage
coverage.xml
composer.lock
composer.lock
\.phpunit\.result\.cache
12 changes: 12 additions & 0 deletions config/api-core.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

/*
|--------------------------------------------------------------------------
| API core key
|--------------------------------------------------------------------------
|
*/

return [
'api-key' => env('API_CORE_KEY', 'api_key'),
];
7 changes: 4 additions & 3 deletions src/Auth/ApiGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ class ApiGuard extends TokenGuard
{
/**
* @param UserProvider $provider
* @param Request $request
* @param Request $request
* @param $storageKey
*/
public function __construct(UserProvider $provider, Request $request)
public function __construct(UserProvider $provider, Request $request, $storageKey)
{
parent::__construct($provider, $request);

$this->storageKey = 'api_key';
$this->storageKey = $storageKey;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/Auth/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ class AuthServiceProvider extends ServiceProvider
public function register()
{
Auth::extend('api', function ($app, $name, array $config) {
return new ApiGuard(Auth::createUserProvider($config['provider']), $app['request']);
return new ApiGuard(Auth::createUserProvider($config['provider']), $app['request'], $app['config']->get('api-core.api-key'));
});
}

public function boot()
{
$this->publishes([
__DIR__ . '/../config/api-core.php' => config_path('api-core.php'),
]);
}
}
15 changes: 8 additions & 7 deletions src/Requests/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Napp\Core\Api\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Str;
use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException;
use Napp\Core\Api\Exceptions\Exceptions\InvalidFieldException;
use Napp\Core\Api\Exceptions\Exceptions\ValidationException;
use Napp\Core\Api\Transformers\ApiTransformer;
use Napp\Core\Api\Transformers\TransformerInterface;
use Napp\Core\Api\Exceptions\Exceptions\ValidationException;
use Napp\Core\Api\Exceptions\Exceptions\InvalidFieldException;
use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException;

/**
* Class ApiRequest.
Expand Down Expand Up @@ -57,7 +58,7 @@ protected function validateInputFields(): void
$rules = $this->rules();
if (false === empty(array_diff_key($input, $rules))) {
$exception = new InvalidFieldException();
$exception->statusMessage = $exception->statusMessage . ': ' . implode(',', array_keys(array_diff_key($input, $rules)));
$exception->statusMessage = $exception->statusMessage.': '.implode(',', array_keys(array_diff_key($input, $rules)));

throw $exception;
}
Expand All @@ -72,7 +73,7 @@ protected function transformInput(): array
* Remove input fields like _method, _token, etc.
*/
$input = array_filter($this->input(), function ($key) {
return ! starts_with($key, '_');
return ! Str::startsWith($key, '_');
}, ARRAY_FILTER_USE_KEY);

return $this->getTransformer()->transformInput($input);
Expand Down Expand Up @@ -113,7 +114,7 @@ protected function handleApiCallFailedValidation(Validator $validator)
{
$message = $validator->messages()->first();
$exception = new ValidationException();
$exception->statusMessage = $exception->statusMessage . ': ' . $message;
$exception->statusMessage = $exception->statusMessage.': '.$message;
$exception->validation = $this->transformValidationOutput($validator);

throw $exception;
Expand Down
4 changes: 0 additions & 4 deletions src/Requests/Provider/RequestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,4 @@ public function boot()
});
}
}

public function register()
{
}
}
11 changes: 1 addition & 10 deletions src/Router/Provider/RouterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Napp\Core\Api\Router\Provider;

use Napp\Core\Api\Router\Router;
use Illuminate\Support\ServiceProvider as BaseProvider;

/**
Expand All @@ -12,14 +11,6 @@ class RouterServiceProvider extends BaseProvider
{
public function register()
{
}

public function boot()
{
$this->app->singleton('internalrouter', function () {
$app = app();

return new Router($app, $app['request'], $app['router']);
});
$this->app->singleton('internalrouter');
}
}
19 changes: 10 additions & 9 deletions src/Transformers/ApiTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Napp\Core\Api\Transformers;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Pagination\Paginator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
* Class ApiTransformer.
Expand Down Expand Up @@ -69,7 +70,7 @@ public function transformOutput($data): array
{
$output = [];

if (true === $data instanceof LengthAwarePaginator || true === $data instanceof Paginator) {
if (true === ($data instanceof AbstractPaginator)) {
return $this->transformPaginatedOutput($data);
}

Expand Down Expand Up @@ -110,7 +111,7 @@ public function transformOutputKeys(array $data): array
protected function transformAttributes(array $output, array $data): array
{
foreach ($data as $key => $value) {
if (true === $this->strict && ! $this->isMapped($key)) {
if (true === $this->strict && !$this->isMapped($key)) {
continue;
}

Expand All @@ -131,7 +132,7 @@ protected function transformRelationships(array $output, Model $data): array
/** @var Model $data */
$relationships = $data->getRelations();
foreach ($relationships as $relationshipName => $relationship) {
if (true === $this->strict && ! $this->isMapped($relationshipName)) {
if (true === $this->strict && !$this->isMapped($relationshipName)) {
continue;
}

Expand Down Expand Up @@ -249,7 +250,7 @@ protected function convertValueType(string $key, $value)
? $this->apiMapping[$key]['dataType']
: 'unknown';

foreach (static::normalizeType($type) as [$method, $parameters]) {
foreach (static::normalizeType($type) as list($method, $parameters)) {
if (true === empty($method)) {
return $value;
}
Expand Down Expand Up @@ -285,7 +286,7 @@ protected static function parseStringDataType($type): array
// easy {data-type}:{parameters} formatting convention. For instance the
// data-type "float:3" states that the value will be converted to a float with 3 decimals.
if (mb_strpos($type, ':') !== false) {
[$dataType, $parameter] = explode(':', $type, 2);
list($dataType, $parameter) = explode(':', $type, 2);

$parameters = static::parseParameters($parameter);
}
Expand Down

0 comments on commit 083a970

Please sign in to comment.