Skip to content

Commit

Permalink
Merge pull request #17 from Napp/develop
Browse files Browse the repository at this point in the history
update for support L 5.8
  • Loading branch information
Mads Møller authored Jul 2, 2019
2 parents a970d6a + fbd5437 commit 7d5009f
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: php

php:
- 7.1
- 7.2
- 7.3

before_script:
- travis_retry composer self-update
Expand Down
180 changes: 179 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,183 @@
[![Code Coverage](https://scrutinizer-ci.com/g/Napp/apicore/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Napp/apicore/?branch=master)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

*Still in development*
Use as a foundation for APIs.

## Features

* Full request cycle
* APU auth guard
* Transform request input based on model api mapping
* Validate transformed data
* Transform response output (with support for nested relationships using `TransformAware`)
* Correct HTTP responses backed into ApiController
* Exception handling with two renderers (dev and prod)
* Standard Exceptions
* ETag middleware for cache responses (Not Modified 304)
* Internal Router for internal api requests
* API Proxy to use easy request handling


## Usage

### Transform Mapping

Being able to hide database fields from the outside exposed API. With auto type casting.

```php
<?php

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
/**
* @var array
*/
public $apiMapping = [
'id' => ['newName' => 'id', 'dataType' => 'int'],
'image' => ['newName' => 'image', 'dataType' => 'string'],
'title' => ['newName' => 'name', 'dataType' => 'string'],
'description' => ['newName' => 'desc', 'dataType' => 'string'],
'created_at' => ['newName' => 'createdAt', 'dataType' => 'datetime'],
'published' => ['newName' => 'published', 'dataType' => 'boolean'],
];
}

```


### Factory

Using a factory pattern.

```php
<?php

namespace App\Posts\Factory;

use App\Posts\Models\Post;
use Napp\Core\Api\Validation\ValidateTrait;

class PostFactory
{
use ValidateTrait;

/**
* @param array $attributes
* @param bool $validate
* @return Post
* @throws \Napp\Core\Api\Exceptions\Exceptions\ValidationException
*/
public static function create(array $attributes, $validate = true): Post
{
if (true === $validate) {
static::validate($attributes, PostValidationRules::$createRules);
}

return new Post($attributes);
}
}


```


### Requests

Extending the `ApiRequest` will automatically transform the input and validate it if Laravel rules are defined.

```php
<?php

namespace App\Posts\Request;

use App\Posts\Factory\PostValidationRules;
use App\Posts\Transformer\PostTransformer;
use Napp\Core\Api\Requests\ApiRequest;
use Napp\Core\Api\Transformers\TransformerInterface;

class StorePostRequest extends ApiRequest
{
/**
* @return array
*/
public function rules(): array
{
return PostTransformer::$createRules;
}

/**
* @return TransformerInterface
*/
protected function getTransformer(): TransformerInterface
{
return app(PostTransformer::class);
}
}
```


### API Controllers

API Controllers can use the requests, the factory for creating a model, transforming the output and finally deliver the correct reponse.

```php
<?php

namespace App\Posts\Controllers\Api;

use App\Posts\Factory\PostFactory;
use App\Posts\Transformer\PostTransformer;
use Napp\Core\Api\Controllers\ApiController;

class PostController extends ApiController
{
public function show(int $id, Request $request, PostTransformer $transformer): JsonResponse
{
$post = $this->postRepository->find($id);
if (null === $post) {
return $this->responseNotFound();
}

return $this->respond($transformer->transformOutput($post));
}

public function store(StorePostRequest $request, PostTransformer $transformer): JsonResponse
{
if (/* some logic */) {
return $this->responseUnauthorized();
}

$post = PostFactory::create($request->validated(), false);

return $this->responseCreated($transformer->transformOutput($post));
}
}

```


### Internal router

Using the Internal router to request APIs.

```php
<?php

use Napp\Core\Api\Controllers\ApiInternalController;

class MyController extends ApiInternalController
{
public function someImportantAction()
{
// using API get/post/put/delete
$data = $this->get('/api/v1/some/route');
$stuff = $this->post('/api/v1/new/stuff', $data);

return view('my.view', compact('stuff'));
}
}

```

30 changes: 15 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@
"require": {
"php" : ">=7.1.3",
"ext-json": "*",
"illuminate/container": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/contracts": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/database": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/http": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/routing": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/support": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/validation": "~5.4.0|~5.5.0|~5.6.0",
"illuminate/pagination": "~5.4.0|~5.5.0|~5.6.0"
"illuminate/container": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/contracts": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/database": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/http": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/routing": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/support": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/validation": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0",
"illuminate/pagination": "~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.7",
"friendsofphp/php-cs-fixer": "^2.15",
"mockery/mockery": "^1.0",
"phpunit/phpunit" : "^7.0",
"phpunit/phpcov": "^5.0",
"squizlabs/php_codesniffer": "^3.1",
"orchestra/testbench": "^3.6",
"orchestra/database": "^3.6",
"fzaninotto/faker": "^1.7"
"phpunit/phpunit" : "^8.0",
"phpunit/phpcov": "^6.0",
"squizlabs/php_codesniffer": "^3.4",
"orchestra/testbench": "^3.8",
"orchestra/database": "^3.8",
"fzaninotto/faker": "^1.8"
},
"extra": {
"laravel": {
Expand Down
2 changes: 1 addition & 1 deletion tests/ApiPaginatedTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ApiPaginatedTransformerTest extends TestCase
*/
protected $faker;

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down
2 changes: 1 addition & 1 deletion tests/ApiRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ApiRequestTest extends TestCase
*/
protected $request;

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down
2 changes: 1 addition & 1 deletion tests/ApiTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ApiTransformerTest extends TestCase
*/
protected $transformer;

public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TestCase extends \Orchestra\Testbench\TestCase
*
* @return void
*/
public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down

0 comments on commit 7d5009f

Please sign in to comment.