Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
ananevam committed Oct 15, 2015
0 parents commit 602e169
Show file tree
Hide file tree
Showing 14 changed files with 1,676 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor
/node_modules
Homestead.yaml
Homestead.json
.env
.DS_Store
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Changelog

All Notable changes to `:package_name` will be documented in this file

## NEXT - YYYY-MM-DD

### Added
- Nothing

### Deprecated
- Nothing

### Fixed
- Nothing

### Removed
- Nothing

### Security
- Nothing
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing

Contributions are **welcome** and will be fully **credited**.

We accept contributions via Pull Requests on [Github](https://github.com/thephpleague/:package_name).


## Pull Requests

- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).

- **Add tests!** - Your patch won't be accepted if it doesn't have tests.

- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.

- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.

- **Create feature branches** - Don't ask us to pull from your master branch.

- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.

- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.


## Running Tests

``` bash
$ composer test
```


**Happy coding**!
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2015 :author_name <:author_email>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
238 changes: 238 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
# wavelaravel/wavelaravel

Features: self model validation, form model binding(including relations), STI pattern.

## Installation

`composer.json`

``` json
"repositories": [
{
"type": "vcs",
"url": "https://bitbucket.org/Minoru/wavelaravel"
}
]
```
``` json
"require": {
"wavelaravel/wavelaravel": "dev-master"
}
```

And run `composer update`

## Usage
You must extend your classes from Wavelaravel\Wavelaravel\WaveModel
``` php
use Wavelaravel\Wavelaravel\WaveModel;

class Post extends WaveModel {
}
```
## Validation
Declare validation rules in array `$validation_rules`.

You can create custom validation rules the same way you would for the Laravel Validator.
``` php
class Post extends WaveModel {
protected $fillable = ['name', 'preview', 'body'];
public $validation_rules = [
'name'=>'required',
];
}
```

## Model Hooks
Here's the complete list of available hooks:

- `before`/`afterCreate()`

- `before`/`afterSave()`

- `before`/`afterUpdate()`

- `before`/`afterDelete()`


## Relations
Wavelaravel don't support many to many relation, but you can use default laravel method.

Relation rules to declare in array `relations_rules`
If you add flag `'validate'=>true` in relation rule, validator will check relation models validation.


Sometimes we don't need to save relation model if all its fields are empty. In such case you can add parameter reject_if and declare static check function.

Example:

``` php
class Post extends WaveModel {
protected $fillable = ['name', 'preview', 'body','comments', 'images'];

public $validation_rules = [
'name'=>'required',
];
public $relations_rules = array(
'comments' => [
self::HAS_MANY, 'App\Models\Comment', 'validate'=>true
],
'images' => [
self::MORPH_MANY,
'App\Models\Image',
'owner',
'validate' => true,
'reject_if' => 'rejectIf',
],
);
public static function rejectIf($model){
if ($model->file_file_name == '' and $model->name == '' and !$model->id){
return true;
}else{
return false;
}
}
}
```

## Form Model Binding
Example views form
posts/create.blade.php
``` php
@extends('layouts.master')
@section('content')
<h1>Create New Post</h1>
<hr/>
@include('posts/_form')
@endsection
```
posts/edit.blade.php
``` php
@extends('layouts.master')
@section('content')
<h1>Edit Post</h1>
<hr/>
@include('posts/_form')
@endsection
```
posts/_form.blade.php
``` php
{!! $f = form_for($post, ['class'=>'form-horizontal']) !!}
@if ($post->hasErrors())
<p class="bg-danger">
{!! implode('<br>', array_dot($post->errors)) !!}
</p>
@endif
<div class="form-group">
{!! $f->label('name', null, ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! $f->text('name', ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
{!! $f->submit('Save',null, ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! $f->close() !!}
```
You can use `fields_for` for relation models

Example:
```php
@foreach ($f->fields_for('comments') as $cf)
{!! $cf() !!}
<div class="form-group">
{!! $cf->label('name', null, ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! $cf->text('name', ['class' => 'form-control']) !!}
</div>
</div>
@endforeach
```
Method `{!! $cf() !!}` out model id attribute if it exists. Controller changes for it? Nothing. Only add `comments` in fillable fields
`protected $fillable = ['name', 'preview', 'body','comments'];`

Since wavelaravel uses self model validation we dont't need to use redirect withErrors, use like ruby on rails method, model field `errors` required errors messages

Example controller
``` php
class PostController extends Controller {
public function index() {
$posts = Post::all();
return view('posts.index', compact('posts'));
}
public function create() {
$post = new Post();
return view('posts.create', compact('post'));
}
public function store(Request $request) {
$post = new Post($request->input('post'));
if ($post->save()){
return redirect()->route('posts.show',$post);
}else {
return view('posts.create', compact('post'));
}
}
public function show($id) {
$post = Post::findOrFail($id);
return view('posts.show', compact('post'));
}
public function edit($id) {
$post = Post::findOrFail($id);
return view('posts.edit', compact('post'));
}
public function update($id, Request $request) {
$post = Post::findOrFail($id);
if ($post->update($request->input('post'))){
return redirect()->route('posts.show', $post);
}else{
return view('posts.edit', compact('post'));
}

}
public function destroy($id) {
Post::destroy($id);
return redirect()->route('posts.index');
}
}
```

If you use resource for declare routing for model, wavelaravel will guess routing.
``` php
Route::resource('posts', 'PostController');
```

Else you can substitute its route

Example create:
```php
@section('content')
{!! $f = form_for($model, ['action' => route("admin.news.store"), 'files'=>true, 'class'=>'form-horizontal']) !!}
@include("admin/news/_form")
{!! $f->close() !!}
@endsection
```

Example edit:
```php
@section('content')
{!! $f = form_for($model, ['action' => route("admin.news.update", [$model->id]), 'files'=>true, 'class'=>'form-horizontal']) !!}
@include("admin/news/_form")
{!! $f->close() !!}
@endsection
```

## STI pattern
Just add field `sti_field` and create migration with string field.

Example:
```php
class Post extends WaveModel {
public $sti_field = 'class_name';
}
```
```php
class SecondPost extends Post {
}
```
50 changes: 50 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"name": "wavelaravel/wavelaravel",
"description": "Self-Model Validation, Form model binding",
"keywords": [
"league",
"WaveLaravel"
],
"homepage": "https://github.com/thephpwavelaravel/wavelaravel",
"license": "MIT",
"authors": [
{
"name": "Alexander Ananev",
"email": "minoru.null@gmail.com",
"homepage": "https://vk.com/minoru",
"role": "Developer"
}
],
"require": {
"illuminate/support": "~5.1",
"php" : ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit" : "4.*",
"scrutinizer/ocular": "~1.1"
},
"autoload": {
"psr-4": {
"Wavelaravel\\Wavelaravel\\": "src"
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
"Wavelaravel\\Wavelaravel\\Test\\": "tests"
},
"files": [
"src/helpers.php"
]
},
"scripts": {
"test": "phpunit"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
Loading

0 comments on commit 602e169

Please sign in to comment.