Skip to content

Commit 61ee8bc

Browse files
committed
Added initial version
0 parents  commit 61ee8bc

19 files changed

+775
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
composer.lock
2+
vendor
3+
build
4+
5+
.idea/
6+
.idea_modules/

.scrutinizer.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
filter:
2+
excluded_paths: [tests/*]
3+
4+
checks:
5+
php:
6+
remove_extra_empty_lines: true
7+
remove_php_closing_tag: true
8+
remove_trailing_whitespace: true
9+
fix_use_statements:
10+
remove_unused: true
11+
preserve_multiple: false
12+
preserve_blanklines: true
13+
order_alphabetically: true
14+
fix_php_opening_tag: true
15+
fix_linefeed: true
16+
fix_line_ending: true
17+
fix_identation_4spaces: true
18+
fix_doc_comments: true

.styleci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
preset: laravel
2+
3+
linting: true

.travis.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
language: php
2+
3+
php:
4+
- 7.0
5+
- 7.1
6+
7+
# This triggers builds to run on the new TravisCI infrastructure.
8+
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
9+
sudo: false
10+
11+
## Cache composer
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
env:
17+
matrix:
18+
- COMPOSER_FLAGS="--prefer-lowest"
19+
- COMPOSER_FLAGS=""
20+
21+
before_script:
22+
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-dist
23+
24+
script:
25+
- vendor/bin/phpcs --standard=psr2 src/
26+
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
27+
28+
after_script:
29+
- |
30+
if [[ "$TRAVIS_PHP_VERSION" != 'hhvm' && "$TRAVIS_PHP_VERSION" != '7.0' ]]; then
31+
wget https://scrutinizer-ci.com/ocular.phar
32+
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
33+
fi

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/).
6+
7+
## [1.0.0] - 2017-05-04
8+
### Added
9+
- Initial version.
10+

CONTRIBUTING.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/byTestGear/eloquent-incrementable).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - Check the code style with ``$ composer check-style`` and fix it with ``$ composer fix-style``.
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **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.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ vendor/bin/phpunit
29+
```
30+
31+
32+
**Happy coding**!

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2017 Testgear BV <info@testgear.com>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Incrementable Eloquent models
2+
3+
[![Latest Stable Version](https://poser.pugx.org/bytestgear/eloquent-incrementable/v/stable)](https://packagist.org/packages/bytestgear/eloquent-incrementable)
4+
[![Travis Build](https://travis-ci.org/byTestGear/eloquent-incrementable.svg?branch=master)](https://travis-ci.org/byTestGear/eloquent-incrementable)
5+
[![Code Quality](https://scrutinizer-ci.com/g/byTestGear/eloquent-incrementable/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/byTestGear/eloquent-incrementable/?branch=master)
6+
[![StyleCI](https://styleci.io/repos/89586066/shield)](https://styleci.io/repos/89586066)
7+
[![License](https://poser.pugx.org/bytestgear/eloquent-incrementable/license)](https://packagist.org/packages/eloquent-incrementable)
8+
9+
Define a custom auto-increment field in your Eloquent model, that is determined through PHP
10+
rather than your database engine.
11+
12+
Furthermore, by making use of increment groups, you can restart counting in-table based on
13+
other fields. Consider this example:
14+
15+
| id | **code** | project_id |
16+
|----|:--------:|:----------:|
17+
| 1 | **1** | 1 |
18+
| 2 | **2** | 1 |
19+
| 3 | **3** | 1 |
20+
| 4 | **1** | 2 |
21+
| 5 | **2** | 2 |
22+
23+
Imagine a bug tracking application that stores each bug in a single table, but is represented
24+
on a per-project basis. You'll want start each project with a fresh bug count, while maintaining
25+
a unique database id. Incrementable will enable you to automatically reset the `code` counter
26+
once a new `project_id` is defined.
27+
28+
## Table of Contents
29+
30+
- [Installation](#installation)
31+
- [Usage](#usage)
32+
- [Examples](#examples)
33+
- [Tests](#tests)
34+
- [Changelog](#changelog)
35+
- [Contributing](#contributing)
36+
- [Credits](#credits)
37+
- [License](#license)
38+
39+
## Installation
40+
41+
This package can be installed through Composer:
42+
43+
```sh
44+
$ composer require bytestgear/eloquent-incrementable
45+
```
46+
47+
## Usage
48+
49+
In order to add Incrementable to your Eloquent model, you'll need to:<br />
50+
51+
1. Use the trait ```ByTestGear\Incrementable\Traits\Incrementable``` on your model(s).
52+
2. Configure the incrementable field *(note: make sure its an integer column)*.
53+
3. Optionally, add one or more increment groups.
54+
55+
Add the Incrementable trait on the models you want to track:
56+
57+
```php
58+
use Illuminate\Database\Eloquent\Model;
59+
use Illuminate\Database\Eloquent\SoftDeletes;
60+
use ByTestGear\Incrementable\Traits\Incrementable;
61+
62+
class Bug extends Model
63+
{
64+
use Incrementable, SoftDeletes;
65+
66+
protected $table = 'bugs';
67+
68+
protected $incrementable = 'code';
69+
70+
// This will cause the code to reset once
71+
// a new project_id is found.
72+
protected $incrementableGroups = ['project_id'];
73+
}
74+
```
75+
76+
In order to avoid collisions, Incrementable will preserve the count for a
77+
soft-deleted model. Although this will cause a gap between this and the
78+
next model, it will ensure uniqueness when the model is restored.
79+
80+
## Examples
81+
82+
In this example, we have set up the following:
83+
84+
- A table containing a `name` and `code` field.
85+
- An Eloquent model called `App\Bug`, which uses the Incrementable trait
86+
- A property on the Bug model: `$incrementable = 'code'`
87+
88+
We can now run this example:
89+
90+
```php
91+
$bug = new App\Bug(['name' => 'It doesn\'t work.']);
92+
$bug->save();
93+
94+
// Will show '1'
95+
echo $bug->code;
96+
97+
$bug = new App\Bug(['name' => 'It really doesn\'t work.']);
98+
$bug->save();
99+
100+
// Will show '2'
101+
echo $bug->code;
102+
```
103+
104+
## Tests
105+
106+
The package contains integration tests. You can run them using PHPUnit.
107+
108+
```
109+
$ vendor/bin/phpunit
110+
```
111+
112+
## Changelog
113+
114+
Refer to [CHANGELOG](CHANGELOG.md) for more information.
115+
116+
## Contributing
117+
118+
Refer to [CONTRIBUTING](CONTRIBUTING.md) for contributing details.
119+
120+
## Credits
121+
122+
- [Thijs Kok](https://www.testmonitor.com/)
123+
- [Stephan Grootveld](https://www.testmonitor.com/)
124+
- [Frank Keulen](https://www.testmonitor.com/)
125+
- [All Contributors](../../contributors)
126+
127+
## License
128+
129+
The MIT License (MIT). Refer to the [License](LICENSE.md) for more information.

composer.json

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"name": "bytestgear/eloquent-incrementable",
3+
"description": "Define a custom auto-increment field in your Eloquent model, that is determined through PHP rather than your database engine.",
4+
"type": "library",
5+
"keywords":
6+
[
7+
"testgear",
8+
"count",
9+
"increment",
10+
"eloquent",
11+
"model",
12+
"laravel"
13+
],
14+
"authors": [
15+
{
16+
"name": "Thijs Kok",
17+
"email": "tkok@testmonitor.com",
18+
"homepage": "https://www.testmonitor.com/",
19+
"role": "Lead Developer"
20+
},
21+
{
22+
"name": "Stephan Grootveld",
23+
"email": "sgrootveld@testmonitor.com",
24+
"homepage": "https://www.testmonitor.com/",
25+
"role": "Developer"
26+
},
27+
{
28+
"name": "Frank Keulen",
29+
"email": "fkeulen@testmonitor.com",
30+
"homepage": "https://www.testmonitor.com/",
31+
"role": "Developer"
32+
}
33+
],
34+
"minimum-stability": "stable",
35+
"require": {
36+
"php" : "^7.0",
37+
"illuminate/support": "~5.2",
38+
"illuminate/database": "~5.2",
39+
"illuminate/config": "~5.2"
40+
},
41+
"require-dev": {
42+
"phpunit/phpunit": "~5.7",
43+
"squizlabs/php_codesniffer": "2.*",
44+
"orchestra/testbench": "~3.3.0|~3.4.0",
45+
"orchestra/database": "~3.3.0|~3.4.0"
46+
},
47+
"autoload": {
48+
"psr-4": {
49+
"ByTestGear\\Incrementable\\": "src"
50+
}
51+
},
52+
"autoload-dev": {
53+
"psr-4": {
54+
"ByTestGear\\Incrementable\\Test\\": "tests"
55+
}
56+
},
57+
58+
"scripts": {
59+
"test": "vendor/bin/phpunit"
60+
},
61+
"license": "MIT"
62+
}

0 commit comments

Comments
 (0)