Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mayoz committed Aug 1, 2017
0 parents commit b1af8a4
Show file tree
Hide file tree
Showing 24 changed files with 927 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

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

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_style = space
indent_size = 2
12 changes: 12 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
* text=auto

/.github export-ignore
/build export-ignore
/tests export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
CHANGELOG.md export-ignore
CONTRIBUTING.md export-ignore
README.md export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
/build
/vendor
3 changes: 3 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
preset: laravel

linting: true
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: php

php:
- 7.0
- 7.1

matrix:
fast_finish: true

sudo: false

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script:
- vendor/bin/phpunit
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Release Notes

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

## 1.0.0 (2017-08-01)

Initial release.
35 changes: 35 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
CONTRIBUTING
============

Contributions are welcome, and are accepted via pull requests. Please review these guidelines before submitting any pull requests.

## Guidelines

* Please follow the [PSR-2 Coding Style Guide](http://www.php-fig.org/psr/psr-2/), enforced by [StyleCI](https://styleci.io/).
* Ensure that the current tests pass, and if you've added something new, add the tests where relevant.
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful.
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
* If you are changing the behavior, or the public api, you may need to update the docs.
* Please remember that we follow [SemVer](http://semver.org/).

## Running Tests

You will need an install of [Composer](https://getcomposer.org/) before continuing.

First, install the dependencies:

```bash
$ composer install
```

Then run PHPUnit:

```bash
$ vendor/bin/phpunit
```

If the test suite passes on your local machine you should be good to go.

When you make a pull request, the tests will automatically be run again by [Travis CI](https://travis-ci.org/).

We also have [StyleCI](https://styleci.io/) setup to automatically fix any code style issues.
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) Sercan Çakır

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.
185 changes: 185 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# Laravel Categorizable

[![Latest Version on Packagist](https://img.shields.io/packagist/v/mayoz/laravel-categorizable.svg?style=flat-square)](https://packagist.org/packages/mayoz/laravel-categorizable)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/mayoz/laravel-categorizable/master.svg?style=flat-square)](https://travis-ci.org/mayoz/laravel-categorizable)
[![StyleCI](https://styleci.io/repos/71335427/shield?branch=master)](https://styleci.io/repos/71335427)

Easily add the ability to category your Eloquent models in Laravel 5.

* [Installation](#installation)
* [Configuration](#configuration)
* [Usage](#usage)
* [Extending](#extending)
* [License](#license)

## Installation

You can install the package via composer:

``` bash
composer require mayoz/categorizable
```

Register the service provider in your `config/app.php` configuration file:

```php
'providers' => [
...
Mayoz\Categorizable\CategorizableServiceProvider::class,
...
];
```

You can publish the migration with:

```bash
php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="migrations"
```

The migration has been published you can create the `categories` and `categorizable` tables. You are feel free for added new fields that you need. After, run the migrations:

```bash
php artisan migrate
```

## Usage

Suppose, you have the `Post` model as follows:

```php
<?php

namespace App;

use Mayoz\Categorizable\Categorizable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use Categorizable;
}
```

Associate new categories for the `Post` model:

```php
$post = Post::find(1);

$post->categorize([1, 2, 3, 4, 5]);

return $post;
```

Now, the `post` model is associated with categories ids of `1`, `2`, `3`, `4` and `5`.

Remove the existing category association for the `Post` model:

```php
$post = Post::find(1);

$post->uncategorize([3, 5]);

return $post;
```

The `post` model is associated with categories ids of `1`, `2` and `4`.

Rearrange the category relationships for the `Post` model:

```php
$post = Post::find(1);

$post->recategorize([1, 5]);

return $post;
```

The `post` model is associated with categories ids of `1` and `5`.

## Extending

I suggest, you always extend the `Category` model to define your relationships directly. Create you own `Category` model:

```php
<?php

namespace App;

use Mayoz\Categorizable\Category as BaseCategory;

class Category extends BaseCategory
{
/**
* Get all posts for the relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphedByMany
*/
public function posts()
{
return $this->categorized(Post::class);
}
}
```

You publish the package config:

```bash
php artisan vendor:publish --provider="Mayoz\Categorizable\CategorizableServiceProvider" --tag="config"
```

This is the contents of the published config file:

```php
<?php

return [

/*
|--------------------------------------------------------------------------
| Model Namespace
|--------------------------------------------------------------------------
|
| Change these values when you need to extend the default category model
| or if the user model needs to be served in a different namespace.
|
*/

'category' => App\Category::class,

];
```

That is all. Now let's play for relationship query with the category.

```php
/**
* Respond the post
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function index(Category $category)
{
return $category->posts()->paginate(10);
}
```

If we did not extend the `Category` model, as had to use;

```php
/**
* Respond the post
*
* @param \App\Category $category
* @return \Illuminate\Http\Response
*/
public function index(Category $category)
{
return $category->categorize(Post::class)->paginate(10);
}
```

## License

This package is licensed under [The MIT License (MIT)](LICENSE.md).
49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "mayoz/laravel-categorizable",
"description": "Polymorphic categorizable for Laravel",
"keywords": ["laravel", "polymorphic", "categorizable"],
"license": "MIT",
"support": {
"issues": "https://github.com/mayoz/laravel-categorizable/issues",
"source": "https://github.com/mayoz/laravel-categorizable"
},
"authors": [
{
"name": "Sercan Çakır",
"email": "[email protected]"
}
],
"require": {
"php": "^7.0",
"illuminate/database": "~5.4|~5.5",
"illuminate/support": "~5.4|~5.5"
},
"require-dev": {
"orchestra/testbench": "^3.4",
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {
"Mayoz\\Categorizable\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Mayoz\\Tests\\Categorizable\\": "tests"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
},
"extra": {
"laravel": {
"providers": [
"Mayoz\\Categorizable\\CategorizableServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
17 changes: 17 additions & 0 deletions config/categorizable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Model Namespace
|--------------------------------------------------------------------------
|
| Change these values when you need to extend the default category model
| or if the user model needs to be served in a different namespace.
|
*/

'category' => Mayoz\Categorizable\Category::class,

];
Loading

0 comments on commit b1af8a4

Please sign in to comment.