Skip to content

Commit 2e41ca0

Browse files
Rename DragonCode\SizeSorter\Sorter with DragonCode\SizeSorter\SizeSorter
1 parent c20cdec commit 2e41ca0

6 files changed

Lines changed: 63 additions & 65 deletions

File tree

README.md

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ When calling a sort with common values, each element will be assigned to one of
3939
5. Other values
4040

4141
```php
42-
use DragonCode\SizeSorter\Sorter;
42+
use DragonCode\SizeSorter\SizeSorter;
4343

44-
return new Sorter([
44+
return new SizeSorter([
4545
'XXL',
4646
'26',
4747
'28',
@@ -69,23 +69,23 @@ return new Sorter([
6969
```
7070

7171
```php
72-
use DragonCode\SizeSorter\Sorter;
72+
use DragonCode\SizeSorter\SizeSorter;
7373

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

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

8282
```php
83-
use DragonCode\SizeSorter\Sorter;
83+
use DragonCode\SizeSorter\SizeSorter;
8484

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

88-
return new Sorter($items)
88+
return new SizeSorter($items)
8989
->sort();
9090

9191
/*
@@ -111,16 +111,10 @@ But you can change the order by specifying identifiers as the third parameter:
111111

112112
```php
113113
use DragonCode\SizeSorter\Enum\Group;
114-
use DragonCode\SizeSorter\Sorter;
114+
use DragonCode\SizeSorter\SizeSorter;
115115

116-
return new Sorter($items)
117-
->groupsOrder([3, 5, 4, 2, 1])
118-
->sort();
119-
120-
// or
121-
122-
return new Sorter($items)
123-
->groupsOrder([
116+
return new SizeSorter($items)
117+
->orderBy([
124118
Group::BraSize,
125119
Group::OtherSizes,
126120
Group::OverallDimensions,
@@ -140,16 +134,10 @@ You can also specify some groups. For example:
140134

141135
```php
142136
use DragonCode\SizeSorter\Enum\Group;
143-
use DragonCode\SizeSorter\Sorter;
144-
145-
return new Sorter($items)
146-
->groupsOrder([3, 5])
147-
->sort();
148-
149-
// or
137+
use DragonCode\SizeSorter\SizeSorter;
150138

151-
return new Sorter($items)
152-
->groupsOrder([Group::BraSize, Group::OtherSizes])
139+
return new SizeSorter($items)
140+
->orderBy([Group::BraSize, Group::OtherSizes])
153141
->sort();
154142
```
155143

src/Services/MainLogic.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MainLogic
1414
public static function sort(iterable $items, string $column = 'value', ?array $groupsOrder = null): IC
1515
{
1616
return static::flatten(
17-
static::handle(collect($items), $column, Order::resolve($groupsOrder))
17+
static::handle(collect($items), $column, $groupsOrder)
1818
);
1919
}
2020

src/Services/Order.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,38 @@
88

99
class Order
1010
{
11-
public static function resolve(?array $groups): array
11+
public function resolve(?array $groups = null): array
1212
{
1313
return ! empty($groups)
14-
? static::fill($groups)
15-
: static::groups();
14+
? $this->fill($groups)
15+
: $this->groups();
1616
}
1717

18-
protected static function fill(array $groups): array
18+
protected function fill(array $groups): array
1919
{
20-
return static::fillDefault(
21-
static::fillCustom($groups)
20+
return $this->fillDefault(
21+
$this->fillCustom($groups)
2222
);
2323
}
2424

25-
protected static function fillCustom(array $groups): array
25+
protected function fillCustom(array $groups): array
2626
{
2727
$result = [];
2828

2929
foreach ($groups as $group) {
30-
$value = static::resolveValue($group);
30+
$value = $this->resolveValue($group);
3131

32-
if (static::existGroup($value)) {
32+
if ($this->existGroup($value)) {
3333
$result[] = $value;
3434
}
3535
}
3636

3737
return $result;
3838
}
3939

40-
protected static function fillDefault(array $groups): array
40+
protected function fillDefault(array $groups): array
4141
{
42-
foreach (static::groups() as $group) {
42+
foreach ($this->groups() as $group) {
4343
if (! in_array($group, $groups)) {
4444
$groups[] = $group;
4545
}
@@ -48,20 +48,20 @@ protected static function fillDefault(array $groups): array
4848
return $groups;
4949
}
5050

51-
protected static function existGroup(int $value): bool
51+
protected function existGroup(int $value): bool
5252
{
5353
return Group::exists($value);
5454
}
5555

5656
/**
5757
* @return array<Group>
5858
*/
59-
protected static function groups(): array
59+
protected function groups(): array
6060
{
6161
return Group::values();
6262
}
6363

64-
protected static function resolveValue(Group|int $group): int
64+
protected function resolveValue(Group|int $group): int
6565
{
6666
return $group->value ?? $group;
6767
}

src/Sorter.php renamed to src/SizeSorter.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66

77
use DragonCode\SizeSorter\Enum\Group;
88
use DragonCode\SizeSorter\Services\MainLogic;
9+
use DragonCode\SizeSorter\Services\Order;
10+
use Illuminate\Support\Collection;
911

10-
class Sorter
12+
class SizeSorter
1113
{
1214
protected string $column = 'value';
1315

14-
protected ?array $groupsOrder = null;
16+
protected ?array $orderBy = null;
1517

16-
public static function same(iterable $items): static
18+
public static function items(iterable $items): static
1719
{
1820
return new static($items);
1921
}
2022

2123
public function __construct(
22-
protected readonly iterable $items
24+
protected readonly iterable $items,
25+
protected readonly Order $order = new Order
2326
) {}
2427

2528
public function column(string $name): static
@@ -33,15 +36,22 @@ public function column(string $name): static
3336
* @param Group[]|null $order
3437
* @return $this
3538
*/
36-
public function groupsOrder(?array $order): static
39+
public function orderBy(?array $order): static
3740
{
38-
$this->groupsOrder = $order;
41+
(new Collection($order))->ensure(Group::class);
42+
43+
$this->orderBy = $this->order->resolve($order);
3944

4045
return $this;
4146
}
4247

4348
public function sort(): iterable
4449
{
45-
return MainLogic::sort($this->items, $this->column, $this->groupsOrder);
50+
return MainLogic::sort($this->items, $this->column, $this->getOrderBy());
51+
}
52+
53+
protected function getOrderBy(): ?array
54+
{
55+
return $this->orderBy ?? $this->order->resolve();
4656
}
4757
}

tests/Sorters/SortTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Tests\Sorters;
66

77
use DragonCode\SizeSorter\Enum\Group;
8-
use DragonCode\SizeSorter\Sorter;
8+
use DragonCode\SizeSorter\SizeSorter;
99
use PHPUnit\Framework\Attributes\DataProvider;
1010
use Tests\TestCase;
1111

@@ -14,8 +14,8 @@ class SortTest extends TestCase
1414
#[DataProvider('sorterData')]
1515
public function testArray(?array $order, array $expected): void
1616
{
17-
$actual = Sorter::same($this->values)
18-
->groupsOrder($order)
17+
$actual = SizeSorter::items($this->values)
18+
->orderBy($order)
1919
->sort()
2020
->toArray();
2121

@@ -31,8 +31,8 @@ public function testObjects(?array $order, array $expected): void
3131
'active' => true,
3232
]);
3333

34-
$actual = Sorter::same($items)
35-
->groupsOrder($order)
34+
$actual = SizeSorter::items($items)
35+
->orderBy($order)
3636
->sort()
3737
->pluck('value', 'id')
3838
->toArray();
@@ -48,8 +48,8 @@ public function testCustomColumn(?array $order, array $expected): void
4848
'some' => $value,
4949
]);
5050

51-
$actual = Sorter::same($items)
52-
->groupsOrder($order)
51+
$actual = SizeSorter::items($items)
52+
->orderBy($order)
5353
->column('some')
5454
->sort()
5555
->pluck('some', 'id')
@@ -71,8 +71,8 @@ public function testWithSaveKeys(?array $order, array $expected): void
7171
840 => 'XL',
7272
];
7373

74-
$actual = Sorter::same($values)
75-
->groupsOrder($order)
74+
$actual = SizeSorter::items($values)
75+
->orderBy($order)
7676
->sort()
7777
->toArray();
7878

tests/Sorters/TypeTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Tests\Sorters;
66

7-
use DragonCode\SizeSorter\Sorter;
7+
use DragonCode\SizeSorter\SizeSorter;
88
use Illuminate\Database\Eloquent\Collection;
99
use Tests\Fixtures\Enums\IntegerValue;
1010
use Tests\Fixtures\Enums\StringValue;
@@ -63,7 +63,7 @@ public function testString(): void
6363
104 => 'ONE SIZE',
6464
131 => 'some',
6565
],
66-
Sorter::same($items)->sort()->toArray()
66+
SizeSorter::items($items)->sort()->toArray()
6767
);
6868
}
6969

@@ -116,7 +116,7 @@ public function testStringable(): void
116116
104 => $items[104],
117117
131 => $items[131],
118118
],
119-
Sorter::same($items)->sort()->toArray()
119+
SizeSorter::items($items)->sort()->toArray()
120120
);
121121
}
122122

@@ -139,7 +139,7 @@ public function testInteger(): void
139139
149 => 21,
140140
133 => 30,
141141
],
142-
Sorter::same($items)->sort()->toArray()
142+
SizeSorter::items($items)->sort()->toArray()
143143
);
144144
}
145145

@@ -162,7 +162,7 @@ public function testFloat(): void
162162
149 => 21.8,
163163
133 => 30.5,
164164
],
165-
Sorter::same($items)->sort()->toArray()
165+
SizeSorter::items($items)->sort()->toArray()
166166
);
167167
}
168168

@@ -213,7 +213,7 @@ public function testEnumString(): void
213213
104 => StringValue::VALUE_ONE_SIZE,
214214
131 => StringValue::VALUE_SOME,
215215
],
216-
Sorter::same($items)->sort()->toArray()
216+
SizeSorter::items($items)->sort()->toArray()
217217
);
218218
}
219219

@@ -236,7 +236,7 @@ public function testEnumInteger(): void
236236
149 => IntegerValue::VALUE_21,
237237
133 => IntegerValue::VALUE_30,
238238
],
239-
Sorter::same($items)->sort()->toArray()
239+
SizeSorter::items($items)->sort()->toArray()
240240
);
241241
}
242242

@@ -289,7 +289,7 @@ public function testNestedArray(): void
289289
104 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => 'ONE SIZE']]],
290290
131 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => 'some']]],
291291
],
292-
Sorter::same($items)->column('bar.some.nested')->sort()->toArray()
292+
SizeSorter::items($items)->column('bar.some.nested')->sort()->toArray()
293293
);
294294
}
295295

@@ -344,7 +344,7 @@ public function testLaravelModels(): void
344344
104 => 'ONE SIZE',
345345
131 => 'some',
346346
],
347-
Sorter::same($items)->sort()->pluck('value', 'id')->toArray()
347+
SizeSorter::items($items)->sort()->pluck('value', 'id')->toArray()
348348
);
349349
}
350350

@@ -397,7 +397,7 @@ public function testCollection(): void
397397
104 => 'ONE SIZE',
398398
131 => 'some',
399399
],
400-
Sorter::same($items)->sort()->toArray()
400+
SizeSorter::items($items)->sort()->toArray()
401401
);
402402
}
403403
}

0 commit comments

Comments
 (0)