Skip to content

Commit fd53bdf

Browse files
Merge pull request #38 from TheDragonCode/2.x
The code has been rewritten from scratch
2 parents 01fcdc4 + 83cef82 commit fd53bdf

62 files changed

Lines changed: 1928 additions & 1254 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 99 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ return new SizeSorter([
5555
/*
5656
* Returns:
5757
*
58-
* array: [
58+
* Illuminate\Support\Collection([
5959
* 'XXS',
6060
* 'XL',
6161
* 'XXL',
@@ -64,37 +64,29 @@ return new SizeSorter([
6464
* '28',
6565
* '54',
6666
* 'ONE SIZE',
67-
* ]
67+
* ])
6868
*/
6969
```
7070

7171
```php
7272
use DragonCode\SizeSorter\SizeSorter;
7373

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

7777
return new SizeSorter($items)
7878
->column('title')
7979
->sort();
8080
```
8181

82+
The static `items` method is also available:
83+
8284
```php
8385
use DragonCode\SizeSorter\SizeSorter;
8486

85-
// Laravel collection
86-
$items = collect([...]);
87-
88-
return new SizeSorter($items)
89-
->sort();
90-
91-
/*
92-
* Returns:
93-
*
94-
* Collection: [
95-
* // ...
96-
* ]
97-
*/
87+
return SizeSorter::items([
88+
// ...
89+
])->sort();
9890
```
9991

10092
### Groups Order
@@ -137,7 +129,10 @@ use DragonCode\SizeSorter\Enum\Group;
137129
use DragonCode\SizeSorter\SizeSorter;
138130

139131
return new SizeSorter($items)
140-
->orderBy([Group::BraSize, Group::OtherSizes])
132+
->orderBy([
133+
Group::BraSize,
134+
Group::OtherSizes,
135+
])
141136
->sort();
142137
```
143138

@@ -148,12 +143,98 @@ ascending order:
148143
3 - 5 - 1 - 2 - 4
149144
```
150145

146+
### Custom Column
147+
148+
When working with an array of objects, you can specify which value can be used for sorting.
149+
150+
```php
151+
use DragonCode\SizeSorter\SizeSorter;
152+
153+
return new SizeSorter($items)
154+
->column('foo')
155+
->sort();
156+
```
157+
158+
You can also use "dotted" notation:
159+
160+
```php
161+
use DragonCode\SizeSorter\SizeSorter;
162+
163+
$items = [
164+
[
165+
'foo' => [
166+
'bar' => [
167+
'baz' => 'Some value',
168+
]
169+
]
170+
]
171+
];
172+
173+
return new SizeSorter($items)
174+
->column('foo.bar.baz')
175+
->sort();
176+
```
177+
178+
And you can use the callback function in the same way:
179+
180+
```php
181+
use DragonCode\SizeSorter\SizeSorter;
182+
183+
class Foo
184+
{
185+
public function __construct(
186+
public int $number,
187+
public string $value1,
188+
public string $value2,
189+
) {}
190+
}
191+
192+
$items = [
193+
new Foo(1, 'first 1', 'first 2'),
194+
new Foo(2, 'second 1', 'second 2'),
195+
];
196+
197+
return new SizeSorter($items)
198+
->column(function (Foo $item) {
199+
return $item->number % 2 === 0
200+
? $item->value1
201+
: $item->value2;
202+
})
203+
->sort();
204+
```
205+
206+
And this is also possible:
207+
208+
```php
209+
use DragonCode\SizeSorter\SizeSorter;
210+
211+
$items = [
212+
['foo' => 'XS'],
213+
['foo' => '2XS'],
214+
['foo' => '3XL'],
215+
];
216+
217+
return new SizeSorter($items)
218+
->column(function (array $item) {
219+
return match ($item['foo']) {
220+
'2XS' => 'XXS',
221+
'3XL' => 'XXXL',
222+
default => $item['foo']
223+
};
224+
})
225+
->sort();
226+
```
227+
228+
## Upgrade Guide
229+
230+
You can find the upgrade documentation [here](UPGRADE.md).
231+
151232
## License
152233

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

155236

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

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

UPGRADE.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Upgrade Guide
2+
3+
## To 2.x from 1.0
4+
5+
### Updating Dependencies
6+
7+
You should update the following dependencies in your application's `composer.json` file:
8+
9+
- `dragon-code/size-sorter` to `^2.0`
10+
11+
### Changing Namespace
12+
13+
Namespace should be changed:
14+
15+
You should update the namespace and method name to call:
16+
17+
```diff
18+
-DragonCode\SizeSorter\Sorter::sort()
19+
+DragonCode\SizeSorter\SizeSorter::items()
20+
21+
-use DragonCode\SizeSorter\Enum\Group;
22+
+use DragonCode\SizeSorter\Enums\GroupEnum;
23+
```
24+
25+
### Changing parameters and call methods
26+
27+
```diff
28+
-use DragonCode\SizeSorter\Sorter;
29+
-use DragonCode\SizeSorter\Enum\Group;
30+
+use DragonCode\SizeSorter\SizeSorter;
31+
+use DragonCode\SizeSorter\Enums\GroupEnum;
32+
33+
-$sort1 = Sorter::sort($items, 'column.name', [1, 2]);
34+
-$sort2 = Sorter::sort($items, 'column.name', [Group::GROUP_1, Group::GROUP_2]);
35+
+
36+
+$sort = SizeSorter::items($items)
37+
+ ->column('column.name')
38+
+ ->orderBy([
39+
+ GroupEnum::LetterClothingSize,
40+
+ GroupEnum::ClothesAndShoes,
41+
+ ])
42+
+ ->sort();
43+
```
44+
45+
### Groups
46+
47+
> [!WARNING]
48+
>
49+
> The array to sort must now consist of GroupEnum objects.
50+
> Integers are no longer accepted.
51+
52+
'Group::GROUP_1' replaced by `GroupEnum::LetterClothingSize`
53+
'Group::GROUP_2' replaced by `GroupEnum::ClothesAndShoes`
54+
'Group::GROUP_3' replaced by `GroupEnum::BraSize`
55+
'Group::GROUP_4' replaced by `GroupEnum::OverallDimensions`
56+
'Group::GROUP_5' replaced by `GroupEnum::OtherSizes`
57+
58+
```php
59+
enum GroupEnum: int
60+
{
61+
case LetterClothingSize = 1;
62+
case ClothesAndShoes = 2;
63+
case BraSize = 3;
64+
case OverallDimensions = 4;
65+
case OtherSizes = 5;
66+
}
67+
```

src/Contracts/GroupMatcher.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Contracts/Sorter.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Detectors/Base.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Detectors/Group1.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Detectors/Group2.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Detectors/Group3.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Detectors/Group4.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/Enum/Group.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)