Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 99 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ return new SizeSorter([
/*
* Returns:
*
* array: [
* Illuminate\Support\Collection([
* 'XXS',
* 'XL',
* 'XXL',
Expand All @@ -64,37 +64,29 @@ return new SizeSorter([
* '28',
* '54',
* 'ONE SIZE',
* ]
* ])
*/
```

```php
use DragonCode\SizeSorter\SizeSorter;

// Laravel models collection
$items = Size::query()->get();
$items = Size::get();

return new SizeSorter($items)
->column('title')
->sort();
```

The static `items` method is also available:

```php
use DragonCode\SizeSorter\SizeSorter;

// Laravel collection
$items = collect([...]);

return new SizeSorter($items)
->sort();

/*
* Returns:
*
* Collection: [
* // ...
* ]
*/
return SizeSorter::items([
// ...
])->sort();
```

### Groups Order
Expand Down Expand Up @@ -137,7 +129,10 @@ use DragonCode\SizeSorter\Enum\Group;
use DragonCode\SizeSorter\SizeSorter;

return new SizeSorter($items)
->orderBy([Group::BraSize, Group::OtherSizes])
->orderBy([
Group::BraSize,
Group::OtherSizes,
])
->sort();
```

Expand All @@ -148,12 +143,98 @@ ascending order:
3 - 5 - 1 - 2 - 4
```

### Custom Column

When working with an array of objects, you can specify which value can be used for sorting.

```php
use DragonCode\SizeSorter\SizeSorter;

return new SizeSorter($items)
->column('foo')
->sort();
```

You can also use "dotted" notation:

```php
use DragonCode\SizeSorter\SizeSorter;

$items = [
[
'foo' => [
'bar' => [
'baz' => 'Some value',
]
]
]
];

return new SizeSorter($items)
->column('foo.bar.baz')
->sort();
```

And you can use the callback function in the same way:

```php
use DragonCode\SizeSorter\SizeSorter;

class Foo
{
public function __construct(
public int $number,
public string $value1,
public string $value2,
) {}
}

$items = [
new Foo(1, 'first 1', 'first 2'),
new Foo(2, 'second 1', 'second 2'),
];

return new SizeSorter($items)
->column(function (Foo $item) {
return $item->number % 2 === 0
? $item->value1
: $item->value2;
})
->sort();
```

And this is also possible:

```php
use DragonCode\SizeSorter\SizeSorter;

$items = [
['foo' => 'XS'],
['foo' => '2XS'],
['foo' => '3XL'],
];

return new SizeSorter($items)
->column(function (array $item) {
return match ($item['foo']) {
'2XS' => 'XXS',
'3XL' => 'XXXL',
default => $item['foo']
};
})
->sort();
```

## Upgrade Guide

You can find the upgrade documentation [here](UPGRADE.md).

## License

This package is licensed under the [MIT License](LICENSE).


[badge_build]: https://img.shields.io/github/actions/workflow/status/TheDragonCode/size-sorter/phpunit.yml?style=flat-square
[badge_build]: https://img.shields.io/github/actions/workflow/status/TheDragonCode/size-sorter/tests.yml?style=flat-square

[badge_downloads]: https://img.shields.io/packagist/dt/dragon-code/size-sorter.svg?style=flat-square

Expand Down
67 changes: 67 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Upgrade Guide

## To 2.x from 1.0

### Updating Dependencies

You should update the following dependencies in your application's `composer.json` file:

- `dragon-code/size-sorter` to `^2.0`

### Changing Namespace

Namespace should be changed:

You should update the namespace and method name to call:

```diff
-DragonCode\SizeSorter\Sorter::sort()
+DragonCode\SizeSorter\SizeSorter::items()

-use DragonCode\SizeSorter\Enum\Group;
+use DragonCode\SizeSorter\Enums\GroupEnum;
```

### Changing parameters and call methods

```diff
-use DragonCode\SizeSorter\Sorter;
-use DragonCode\SizeSorter\Enum\Group;
+use DragonCode\SizeSorter\SizeSorter;
+use DragonCode\SizeSorter\Enums\GroupEnum;

-$sort1 = Sorter::sort($items, 'column.name', [1, 2]);
-$sort2 = Sorter::sort($items, 'column.name', [Group::GROUP_1, Group::GROUP_2]);
+
+$sort = SizeSorter::items($items)
+ ->column('column.name')
+ ->orderBy([
+ GroupEnum::LetterClothingSize,
+ GroupEnum::ClothesAndShoes,
+ ])
+ ->sort();
```

### Groups

> [!WARNING]
>
> The array to sort must now consist of GroupEnum objects.
> Integers are no longer accepted.

'Group::GROUP_1' replaced by `GroupEnum::LetterClothingSize`
'Group::GROUP_2' replaced by `GroupEnum::ClothesAndShoes`
'Group::GROUP_3' replaced by `GroupEnum::BraSize`
'Group::GROUP_4' replaced by `GroupEnum::OverallDimensions`
'Group::GROUP_5' replaced by `GroupEnum::OtherSizes`

```php
enum GroupEnum: int
{
case LetterClothingSize = 1;
case ClothesAndShoes = 2;
case BraSize = 3;
case OverallDimensions = 4;
case OtherSizes = 5;
}
```
10 changes: 0 additions & 10 deletions src/Contracts/GroupMatcher.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Contracts/Sorter.php

This file was deleted.

23 changes: 0 additions & 23 deletions src/Detectors/Base.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Detectors/Group1.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Detectors/Group2.php

This file was deleted.

17 changes: 0 additions & 17 deletions src/Detectors/Group3.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/Detectors/Group4.php

This file was deleted.

30 changes: 0 additions & 30 deletions src/Enum/Group.php

This file was deleted.

Loading
Loading