Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Metrix Information Solutions committed Oct 14, 2018
0 parents commit e974445
Show file tree
Hide file tree
Showing 14 changed files with 1,364 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
vendor
composer.lock
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright for portions of project metrix/eloquent-sortable are held by Spatie bvba as part of spatie/eloquent-sortable.
All other copyright for project metrix/eloquent-sortable are held by (c) Michael J. Pawlowsky 2018.

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.
176 changes: 176 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Sortable behaviour for Eloquent models

This package provides a trait that adds sortable behaviour to an Eloquent model.

The value of the order column of a new record of a model is determined by the maximum value of the order column of all or a subset group of records of that model + 1.

The package also provides a query scope to fetch all the records in the right order.

This package is a fork of the popular [spatie/eloquent-sortable](https://github.com/spatie/eloquent-sortable) with added functionality
to allow sorting on subsets of models as well as moving a model to a specific position.

Thank you to [Freek Van der Herten](https://murze.be) for sharing the original package.

## Installation

This package can be installed through Composer.

```
composer require metrix/eloquent-sortable
```

## Usage

To add sortable behaviour to your model you must:
1. Use the trait `Metrix\EloquentSortable\Sortable`.
2. Optionally specify which column will be used as the order column. The default is `display_order`.

### Examples

*Simple ordered model*

```php
use Metrix\EloquentSortable\Sortable;

class MyModel extends Eloquent
{

use SortableTrait;

public $sortable = [
'sort_on_creating' => true,
'order_column' => 'display_order',
];

...
}
```

*Ordered model with a grouping column*

```php
use Metrix\EloquentSortable\Sortable;

class MyModel extends Eloquent
{

use SortableTrait;

public $sortable = [
'sort_on_creating' => true,
'order_column' => 'display_order',
'group_column' => 'group_id',
];

...
}
```

*Ordered model grouped on multiple columns*

```php
use Metrix\EloquentSortable\Sortable;

class MyModel extends Eloquent
{

use SortableTrait;

public $sortable = [
'sort_on_creating' => true,
'order_column' => 'display_order',
'group_column' => ['group_id','user_id'],
];

...
}
```

If you don't set a value for `$sortable['order_column']` the package will assume an order column name of `display_order`.

If you don't set a value `$sortable['sort_on_creating']` the package will automatically assign the next highest order value to the new model;

Assuming that the db table for `MyModel` is empty:

```php
$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 1

$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 2

$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 3
```

The trait also provides an ordered query scope.
All models will be returned ordered by 'group' and then 'display_order'
if you have not applied a where() method for your group column on your query,

```php
$orderedRecords = MyModel::ordered()->get();

$groupedOrderedRecords = MyModel::where('group_id', 2)->ordered()->get();

$allRecords = MyModel::ordered()->get();
```

You can set a new order for all the records using the `setNewOrder`-method

```php
/**
* the record for model id 3 will have record_column value 1
* the record for model id 1 will have record_column value 2
* the record for model id 2 will have record_column value 3
*/
MyModel::setNewOrder([3,1,2]);
```

Optionally you can pass the starting order number as the second argument.

```php
/**
* the record for model id 3 will have record_column value 11
* the record for model id 1 will have record_column value 12
* the record for model id 2 will have record_column value 13
*/
MyModel::setNewOrder([3,1,2], 10);
```

You can also move a model up or down with these methods:

```php
$myModel->moveOrderDown();
$myModel->moveOrderUp();
```

You can also move a model to the first or last position:

```php
$myModel->moveToStart();
$myModel->moveToEnd();
```

You can swap the order of two models:

```php
MyModel::swapOrder($myModel, $anotherModel);
```

You can move a model to a specific position:

```php
$myModel->moveToPosition(4);
```

## Tests

The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via the ./bin/test.sh script from the root directory.

```
$ ./bin/test.sh
```

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
16 changes: 16 additions & 0 deletions bin/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

echo "PHP Lint"
vendor/bin/parallel-lint --blame src

echo "Fixing code sniffs"
vendor/bin/phpcbf -p -s --standard=build/phpcs.xml

echo "Running code sniffer"
vendor/bin/phpcs -p -s --standard=build/phpcs.xml

echo "Running Mess Detector"
vendor/bin/phpmd app text build/phpmd.xml

echo "Running Tests"
vendor/bin/phpunit -d memory_limit=512M
30 changes: 30 additions & 0 deletions build/phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">

<description>The coding standard for PHP_CodeSniffer itself.</description>

<file>.././src</file>

<exclude-pattern>*/Standards/*/Tests/*.(inc|css|js)</exclude-pattern>

<arg name="basepath" value=""/>
<arg name="colors" />
<arg name="parallel" value="75" />
<arg value="np"/>

<!-- Don't hide tokenizer exceptions -->
<rule ref="Internal.Tokenizer.Exception">
<type>error</type>
</rule>

<!-- All PHP files MUST use the Unix LF (linefeed) line ending. -->
<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n"/>
</properties>
</rule>

<rule ref="PSR1" />
<rule ref="PSR2" />

</ruleset>
29 changes: 29 additions & 0 deletions build/phpmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="My first PHPMD rule set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0
http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="
http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>
Honeyfund 2 Rule Set
</description>

<rule ref="rulesets/unusedcode.xml">
<exclude name="UnusedFormalParameter"/>
</rule>

<rule ref="rulesets/codesize.xml">
<exclude name="CyclomaticComplexity"/>
<exclude name="ExcessiveMethodLength"/>
<exclude name="NPathComplexity"/>
<exclude name="TooManyMethods"/>
<exclude name="TooManyPublicMethods"/>
<exclude name="ExcessiveClassComplexity"/>
<exclude name="ExcessivePublicCount"/>
<exclude name="TooManyFields"/>
</rule>


</ruleset>
48 changes: 48 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "metrix/laravel-sortable",
"description": "Sortable trait for Laravel Eloquent models",
"homepage": "https://github.com/metrix/laravel-sortable",
"authors": [
{
"name": "Michael J. Pawlowsky",
"email": "[email protected]"
}
],
"keywords":
[
"sort",
"sortable",
"eloquent",
"model",
"laravel",
"trait",
"display order"
],
"license": "MIT",
"require": {
"php": ">=7.1",
"laravel/framework": "~5.5.0|~5.6.0|~5.7.0"
},
"require-dev": {
"phpunit/phpunit" : "^6.2|^7.0",
"orchestra/testbench": "~3.5.0|~3.6.0|~3.7.0",
"consistence/coding-standard": "^2.0",
"jakub-onderka/php-parallel-lint": "^0.9.2",
"pdepend/pdepend": "^2.5",
"phploc/phploc": "^4.0",
"phpmd/phpmd": "^2.6",
"phpunit/php-code-coverage": "^5.2",
"sebastian/phpcpd": "^3.0",
"squizlabs/php_codesniffer": "^3.2"
},
"autoload": {
"psr-4": {
"Metrix\\EloquentSortable\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Metrix\\EloquentSortable\\Test\\": "tests"
}
}
}
29 changes: 29 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
</php>
</phpunit>
Loading

0 comments on commit e974445

Please sign in to comment.