<commit_before># Assertions for Laravel 5.1

[![Latest Stable Version](https://poser.pugx.org/spotonlive/sl-assertions/v/stable)](https://packagist.org/packages/spotonlive/sl-assertions) [![Total Downloads](https://poser.pugx.org/spotonlive/sl-assertions/downloads)](https://packagist.org/packages/spotonlive/sl-assertions) [![Latest Unstable Version](https://poser.pugx.org/spotonlive/sl-assertions/v/unstable)](https://packagist.org/packages/spotonlive/sl-assertions) [![License](https://poser.pugx.org/spotonlive/sl-assertions/license)](https://packagist.org/packages/spotonlive/sl-assertions)

**THIS PACKAGE IS UNDER DEVELOPMENT**

## Configuration

### Installation
Run `$ composer require spotonlive/sl-assertions`

`config/app.php`
*Insert the provider and helper alias into your application configuration*
````php
    'providers' => [
        (..)
        'SpotOnLive\Assertions\Providers\Services\AssertionServiceProvider',
        'SpotOnLive\Assertions\Providers\Helpers\AssertionHelperProvider',
        (..)
    ]

    'aliases' => [
        (..)
        'AssertionHelper' => 'SpotOnLive\Assertions\Facades\Helpers\AssertionHelperFacade',
        (..)
    ]
```

### Configuration
run `$ php artisan vendor:publish` to create the configuration file.
A configuration file is now available in `config/assertions.php`.

## Assertions
To create new examples create a new assertion file implementing the assertion interface.
For example:

`EditAssertion.php`
```php
<?php

namespace App\Assertions\Users;

use SpotOnLive\Assertions\AssertionInterface;
use App\Entities\User;

class EditAssertion implements AssertionInterface
{
    /**
     * @param User $user
     * @param array $data
     * @return bool
     */
    public function assert($user, array $data = [])
    {
        /** @var User $userToEdit */
        $userToEdit = $data['user'];

        if ($user == $userToEdit || $user->hasRole(['superadmin', 'admin'])) {
            return true;
        }

        return false;
    }
}
```

And then register the assertion in your configuration file:

`config/assertions.php`
````php
<?php

return [
    'users.edit' => 'App\Assertions\Users\EditAssertion'
];
```

## Usage

### Service
Use the assertion service by injecting `AssertionService`.

`app::make('AssertionService')`

*Example:*
```php
<?php

namespace App\Controllers;

use \SpotOnLive\Assertions\Services\AssertionServiceInterface;

class Controller
{
    /** @var AssertionServiceInterface **/
    protected $assertionService;

    public function __construct(AssertionServiceInterface $assertionService)
    {
        $this->assertionService = $assertionService;
    }

    public function admin()
    {
        if (!$this->assertionService->isGranted('admin.page', Auth::user())) {
            return redirect()->route('not-granted');
        }

        return view('admin.page');
    }
}
```

### Helper

Use the `AssertionHelper` directly in your views.

*Example:*
**view.blade.php**
```php
@if(AssertionHelper::isGranted('user.edit', Auth::user(), ['user' => $user]))
    <a href="{{URL::route('user.edit')}}">{{_('Edit user')}}</a>
@endif
```

## Organization & authors
* [**spotonlive**](https://github.com/spotonlive)
* [**nikolajpetersen**](https://github.com/Nikolajpetersen)<commit_msg>Badges<commit_after># Assertions for Laravel 5.1

[![Latest Stable Version](https://poser.pugx.org/spotonlive/sl-assertions/v/stable)](https://packagist.org/packages/spotonlive/sl-assertions) [![Total Downloads](https://poser.pugx.org/spotonlive/sl-assertions/downloads)](https://packagist.org/packages/spotonlive/sl-assertions) [![Latest Unstable Version](https://poser.pugx.org/spotonlive/sl-assertions/v/unstable)](https://packagist.org/packages/spotonlive/sl-assertions) [![License](https://poser.pugx.org/spotonlive/sl-assertions/license)](https://packagist.org/packages/spotonlive/sl-assertions) [![Code Climate](https://codeclimate.com/github/spotonlive/sl-assertions/badges/gpa.svg)](https://codeclimate.com/github/spotonlive/sl-assertions) [![Test Coverage](https://codeclimate.com/github/spotonlive/sl-assertions/badges/coverage.svg)](https://codeclimate.com/github/spotonlive/sl-assertions/coverage)

**THIS PACKAGE IS UNDER DEVELOPMENT**

## Configuration

### Installation
Run `$ composer require spotonlive/sl-assertions`

`config/app.php`
*Insert the provider and helper alias into your application configuration*
````php
    'providers' => [
        (..)
        'SpotOnLive\Assertions\Providers\Services\AssertionServiceProvider',
        'SpotOnLive\Assertions\Providers\Helpers\AssertionHelperProvider',
        (..)
    ]

    'aliases' => [
        (..)
        'AssertionHelper' => 'SpotOnLive\Assertions\Facades\Helpers\AssertionHelperFacade',
        (..)
    ]
```

### Configuration
run `$ php artisan vendor:publish` to create the configuration file.
A configuration file is now available in `config/assertions.php`.

## Assertions
To create new examples create a new assertion file implementing the assertion interface.
For example:

`EditAssertion.php`
```php
<?php

namespace App\Assertions\Users;

use SpotOnLive\Assertions\AssertionInterface;
use App\Entities\User;

class EditAssertion implements AssertionInterface
{
    /**
     * @param User $user
     * @param array $data
     * @return bool
     */
    public function assert($user, array $data = [])
    {
        /** @var User $userToEdit */
        $userToEdit = $data['user'];

        if ($user == $userToEdit || $user->hasRole(['superadmin', 'admin'])) {
            return true;
        }

        return false;
    }
}
```

And then register the assertion in your configuration file:

`config/assertions.php`
````php
<?php

return [
    'users.edit' => 'App\Assertions\Users\EditAssertion'
];
```

## Usage

### Service
Use the assertion service by injecting `AssertionService`.

`app::make('AssertionService')`

*Example:*
```php
<?php

namespace App\Controllers;

use \SpotOnLive\Assertions\Services\AssertionServiceInterface;

class Controller
{
    /** @var AssertionServiceInterface **/
    protected $assertionService;

    public function __construct(AssertionServiceInterface $assertionService)
    {
        $this->assertionService = $assertionService;
    }

    public function admin()
    {
        if (!$this->assertionService->isGranted('admin.page', Auth::user())) {
            return redirect()->route('not-granted');
        }

        return view('admin.page');
    }
}
```

### Helper

Use the `AssertionHelper` directly in your views.

*Example:*
**view.blade.php**
```php
@if(AssertionHelper::isGranted('user.edit', Auth::user(), ['user' => $user]))
    <a href="{{URL::route('user.edit')}}">{{_('Edit user')}}</a>
@endif
```

## Organization & authors
* [**spotonlive**](https://github.com/spotonlive)
* [**nikolajpetersen**](https://github.com/Nikolajpetersen)
{'fraction_non_alphanumeric': 0.15932944606413993, 'fraction_numerical': 0.0005830903790087463, 'mean_word_length': 3.978955007256894, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, 'https://': 24, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2904117', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2130, 'n_words': 391}