Skip to content

Commit

Permalink
Release 4.0 (#14)
Browse files Browse the repository at this point in the history
* Add isOwnedByDefaultOwner method to Ownable models

* Upgrade package architecture
  • Loading branch information
Anton Komarev authored Sep 9, 2017
1 parent f4c2487 commit 01bf9d0
Show file tree
Hide file tree
Showing 38 changed files with 463 additions and 227 deletions.
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ disabled:
- phpdoc_no_package
- logical_not_operators_with_successor_space
- length_ordered_imports
- simplified_null_return
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ language: php
sudo: false

php:
- 5.6
- 7.0
- 7.1
- 7.2

env:
global:
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to `laravel-ownership` will be documented in this file.

## [4.0.0] - 2017-09-09

### Added

- Ownable models got new `isOwnedByDefaultOwner` method which automatically try to resolve current user.

### Changed

- Contracts namespace changed from `Cog\Ownership\Contracts` to `Cog\Contracts\Laravel\Ownership`
- Classes namespace changed from `Cog\Ownership` to `Cog\Laravel\Ownership`
- `ModelObserver` renamed to `OwnableObserver`
- `HasOwner` contract renamed to `Ownable`

## [3.1.0] - 2017-08-30

### Added
Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ And then include the service provider within `app/config/app.php`.

```php
'providers' => [
Cog\Ownership\Providers\OwnershipServiceProvider::class,
Cog\Laravel\Ownership\Providers\OwnershipServiceProvider::class,
];
```

Expand All @@ -76,14 +76,14 @@ Polymorphic ownership is useful when model can belongs to owners of different ty

### Prepare ownable model with strict ownership

Use `HasOwner` contract in model which will get ownership behavior and implement it or just use `HasOwner` trait.
Use `Ownable` contract in model which will get ownership behavior and implement it or just use `HasOwner` trait.

```php
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
use Cog\Ownership\Traits\HasOwner;
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements HasOwnerContract
class Article extends Model implements OwnableContract
{
use HasOwner;
}
Expand All @@ -106,11 +106,11 @@ By default owner model will be the same as `config('auth.providers.users.model')
To override default owner model in strict ownership, it's primary key or foreign key extend your ownable model with additional attributes:

```php
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
use Cog\Ownership\Traits\HasOwner;
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements HasOwnerContract
class Article extends Model implements OwnableContract
{
use HasOwner;

Expand All @@ -122,14 +122,14 @@ class Article extends Model implements HasOwnerContract

### Prepare ownable model with polymorphic ownership

Use `HasOwner` contract in model which will get polymorphic ownership behavior and implement it or just use `HasMorphOwner` trait.
Use `Ownable` contract in model which will get polymorphic ownership behavior and implement it or just use `HasMorphOwner` trait.

```php
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
use Cog\Ownership\Traits\HasMorphOwner;
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
use Cog\Laravel\Ownership\Traits\HasMorphOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements HasOwnerContract
class Article extends Model implements OwnableContract
{
use HasMorphOwner;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ $article->isNotOwnedBy($owner);
#### Manually define default owner on model creation

```php
$article = new Article();
$article = new Article;
$article->withDefaultOwner()->save();
```

Expand All @@ -219,14 +219,14 @@ Or provide concrete owner:

```php
$user = User::where('name', 'admin')->first();
$article = new Article();
$article = new Article;
$article->withDefaultOwner($user)->save();
```

#### Skip defining default owner on model creation

```php
$article = new Article();
$article = new Article;
$article->withoutDefaultOwner()->save();
```

Expand All @@ -249,11 +249,11 @@ Article::whereNotOwnedBy($owner)->get();
To set currently authenticated user as owner for ownable model create - extend it with attribute `withDefaultOwnerOnCreate`. It works for both strict and polymorphic ownership behavior.

```php
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
use Cog\Ownership\Traits\HasOwner;
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements HasOwnerContract
class Article extends Model implements OwnableContract
{
use HasOwner;

Expand All @@ -264,11 +264,11 @@ class Article extends Model implements HasOwnerContract
To override strategy of getting default owner extend ownable model with `resolveDefaultOwner` method:

```php
use Cog\Ownership\Contracts\HasOwner as HasOwnerContract;
use Cog\Ownership\Traits\HasOwner;
use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
use Cog\Laravel\Ownership\Traits\HasOwner;
use Illuminate\Database\Eloquent\Model;

class Article extends Model implements HasOwnerContract
class Article extends Model implements OwnableContract
{
use HasOwner;

Expand All @@ -277,7 +277,7 @@ class Article extends Model implements HasOwnerContract
/**
* Resolve entity default owner.
*
* @return \Cog\Ownership\Contracts\CanBeOwner|null
* @return \Cog\Contracts\Laravel\Ownership\CanBeOwner|null
*/
public function resolveDefaultOwner()
{
Expand Down
13 changes: 11 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# Upgrade Guide

- [Upgrading To 3.0 From 2.0](#upgrade-3.0)
- [Upgrading From 3.0 To 4.0](#upgrade-4.0)
- [Upgrading From 2.0 To 3.0](#upgrade-3.0)

<a name="upgrade-3.0"></a>
## Upgrading To 3.0 From 2.0
## Upgrading From 3.0 To 4.0

- Find all `Cog\Ownership\Contracts\HasOwner` and replace with `Cog\Contracts\Laravel\Ownership\Ownable`
- Find all `Cog\Ownership\Observers\ModelObserver` and replace with `Cog\Laravel\Ownership\Observers\OwnableObserver`
- Find all `Cog\Ownership\Contracts` and replace with `Cog\Contracts\Laravel\Ownership`
- Find all `Cog\Ownership` and replace with `Cog\Laravel\Ownership`

<a name="upgrade-3.0"></a>
## Upgrading From 2.0 To 3.0

You need to upgrade only if you have models with Strict Ownership and you are using default `owned_by` column names.

Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,19 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^1.11",
"mockery/mockery": "^0.9.8",
"orchestra/database": "~3.4.0",
"orchestra/testbench": "~3.4.0",
"phpunit/phpunit": "^5.7"
"orchestra/database": "~3.5.0",
"orchestra/testbench": "~3.5.0",
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {
"Cog\\Ownership\\": "src/"
"Cog\\Contracts\\Laravel\\Ownership\\": "contracts/",
"Cog\\Laravel\\Ownership\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Cog\\Ownership\\Tests\\": "tests/"
"Cog\\Tests\\Laravel\\Ownership\\": "tests/"
}
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions src/Contracts/CanBeOwner.php → contracts/CanBeOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
* file that was distributed with this source code.
*/

namespace Cog\Ownership\Contracts;
namespace Cog\Contracts\Laravel\Ownership;

/**
* Interface CanBeOwner.
*
* @package Cog\Ownership\Contracts
* @package Cog\Contracts\Laravel\Ownership
*/
interface CanBeOwner
{
Expand Down
34 changes: 34 additions & 0 deletions contracts/Exceptions/InvalidDefaultOwner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of Laravel Ownership.
*
* (c) Anton Komarev <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cog\Contracts\Laravel\Ownership\Exceptions;

use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
use Exception;

/**
* Class InvalidDefaultOwner.
*
* @package Cog\Contracts\Laravel\Ownership\Exceptions
*/
class InvalidDefaultOwner extends Exception
{
/**
* Default owner for ownable model is null.
*
* @param \Cog\Contracts\Laravel\Ownership\Ownable $ownable
* @return static
*/
public static function isNull(OwnableContract $ownable)
{
return new static(sprintf('Model `%s` default owner is null.', get_class($ownable)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
* file that was distributed with this source code.
*/

namespace Cog\Ownership\Exceptions;
namespace Cog\Contracts\Laravel\Ownership\Exceptions;

use Cog\Contracts\Laravel\Ownership\Ownable as OwnableContract;
use Exception;
use Cog\Ownership\Contracts\HasOwner;
use Cog\Ownership\Contracts\CanBeOwner as CanBeOwnerContract;
use Cog\Contracts\Laravel\Ownership\CanBeOwner as CanBeOwnerContract;

/**
* Class InvalidOwnerType.
*
* @package Cog\Ownership\Exceptions
* @package Cog\Contracts\Laravel\Ownership\Exceptions
*/
class InvalidOwnerType extends Exception
{
/**
* Owner of the provided type is not allowed to own this model.
*
* @param \Cog\Ownership\Contracts\HasOwner $model
* @param \Cog\Ownership\Contracts\CanBeOwner $owner
* @param \Cog\Contracts\Laravel\Ownership\Ownable $ownable
* @param \Cog\Contracts\Laravel\Ownership\CanBeOwner $owner
* @return static
*/
public static function notAllowed(HasOwner $model, CanBeOwnerContract $owner)
public static function notAllowed(OwnableContract $ownable, CanBeOwnerContract $owner)
{
return new static(sprintf('Model `%s` not allows owner of type `%s`.', get_class($model), get_class($owner)));
return new static(sprintf('Model `%s` not allows owner of type `%s`.', get_class($ownable), get_class($owner)));
}
}
Loading

0 comments on commit 01bf9d0

Please sign in to comment.