-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added strict validation to API controller trait Added support for fractal in models
- Loading branch information
1 parent
d0eaaba
commit 7fb5800
Showing
5 changed files
with
171 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,55 @@ | ||
# larapi | ||
A collection of classes to be extended/used in laravel api applications for quick development | ||
# laraquick | ||
|
||
A collection of classes to be extended/used in laravel api applications for quick | ||
development. | ||
|
||
## Introduction | ||
|
||
The library contains traits with well documented methods that should be used by | ||
controllers and models to enhance coding speed. | ||
|
||
## Installation | ||
|
||
``` | ||
composer require d-scribe/laraquick | ||
``` | ||
|
||
## Dependencies | ||
|
||
- PHP >= 5.6.0 | ||
- Laravel - 5.4.* | ||
- Laravel Fractal - ^4.0 | ||
|
||
## Example | ||
|
||
An example controller for a `Book` model is: | ||
|
||
```php | ||
use App\Book; | ||
use Laraquick\Controllers\Traits\Api; | ||
|
||
class BookController extends Controller { | ||
|
||
use Api; | ||
|
||
protected function model() { | ||
return Book::class; | ||
} | ||
|
||
protected function validationRules($forUpdate = false) { | ||
return [ | ||
'title' => 'required|max:200', | ||
'author' => 'required|max:50', | ||
'genre' => 'required' | ||
]; | ||
} | ||
} | ||
|
||
``` | ||
|
||
And with just the above, the controller would take care of listing (w/ pagination), | ||
and all `CRUD` operations and give the right JSON responses. | ||
|
||
## API Documentation | ||
|
||
Coming soon |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
namespace Laraquick\Models\Traits; | ||
|
||
use Iterator; | ||
|
||
trait Fractal | ||
{ | ||
/** | ||
* Transform the model with fractal | ||
* | ||
* @return void | ||
*/ | ||
final public function fractalize() | ||
{ | ||
return fractal($this, function () { | ||
return $this->transform(); | ||
}); | ||
} | ||
|
||
/** | ||
* Fractalize many models of this class | ||
* | ||
* @param Iterator $items | ||
* @return void | ||
*/ | ||
public static function fractality(Iterator $items) | ||
{ | ||
return collect($items)->map([self, 'fractalize']); | ||
} | ||
|
||
abstract public function transform(); | ||
} |