diff --git a/src/Sorter.php b/src/Sorter.php index ad3466f..a6ab0f3 100644 --- a/src/Sorter.php +++ b/src/Sorter.php @@ -4,6 +4,7 @@ namespace DragonCode\SizeSorter; +use ArrayAccess; use DragonCode\SizeSorter\Enum\Group; use DragonCode\SizeSorter\Services\Collection; use DragonCode\SizeSorter\Services\GroupsDetector; @@ -15,10 +16,10 @@ class Sorter { - public static function sort(IC $items, string $column = 'value', ?array $groupsOrder = null): IC + public static function sort(array|ArrayAccess|IC $items, string $column = 'value', ?array $groupsOrder = null): IC { return static::flatten( - static::handle($items, $column, Order::resolve($groupsOrder)) + static::handle(collect($items), $column, Order::resolve($groupsOrder)) ); } diff --git a/tests/Sorters/FullGroupsOrderTest.php b/tests/Sorters/FullGroupsOrderTest.php index 482baa6..62c543a 100644 --- a/tests/Sorters/FullGroupsOrderTest.php +++ b/tests/Sorters/FullGroupsOrderTest.php @@ -87,7 +87,7 @@ public function testArray(): void { $this->assertSame( $this->expected, - Sorter::sort(collect($this->values), groupsOrder: $this->groupsOrder)->toArray() + Sorter::sort($this->values, groupsOrder: $this->groupsOrder)->toArray() ); } diff --git a/tests/Sorters/PartialGroupsOrderTest.php b/tests/Sorters/PartialGroupsOrderTest.php index 0400cea..0da2ef3 100644 --- a/tests/Sorters/PartialGroupsOrderTest.php +++ b/tests/Sorters/PartialGroupsOrderTest.php @@ -84,7 +84,7 @@ public function testArray(): void { $this->assertSame( $this->expected, - Sorter::sort(collect($this->values), groupsOrder: $this->groupsOrder)->toArray() + Sorter::sort($this->values, groupsOrder: $this->groupsOrder)->toArray() ); } diff --git a/tests/Sorters/SorterTest.php b/tests/Sorters/SorterTest.php index 3cd14aa..e60616a 100644 --- a/tests/Sorters/SorterTest.php +++ b/tests/Sorters/SorterTest.php @@ -78,7 +78,7 @@ public function testArray(): void { $this->assertSame( $this->expected, - Sorter::sort(collect($this->values))->toArray() + Sorter::sort($this->values)->toArray() ); } @@ -121,6 +121,6 @@ public function testWithSaveKeys(): void 840 => 'XL', ]; - $this->assertSame($expected, Sorter::sort(collect($values))->toArray()); + $this->assertSame($expected, Sorter::sort($values)->toArray()); } } diff --git a/tests/Sorters/TypesTest.php b/tests/Sorters/TypesTest.php index cadce1e..7037510 100644 --- a/tests/Sorters/TypesTest.php +++ b/tests/Sorters/TypesTest.php @@ -16,7 +16,7 @@ class TypesTest extends TestCase { public function testString(): void { - $items = collect([ + $items = [ 104 => 'ONE SIZE', 105 => 'XXS', 106 => '2', @@ -35,7 +35,7 @@ public function testString(): void 150 => '3', 155 => '41х38х15 см', 156 => '39х38х15 см', - ]); + ]; $this->assertSame( [ @@ -69,7 +69,7 @@ public function testString(): void public function testStringable(): void { - $items = collect([ + $items = [ 104 => new Str('ONE SIZE'), 105 => new Str('XXS'), 106 => new Str('2'), @@ -88,7 +88,7 @@ public function testStringable(): void 150 => new Str('3'), 155 => new Str('41х38х15 см'), 156 => new Str('39х38х15 см'), - ]); + ]; $this->assertSame( [ @@ -122,13 +122,13 @@ public function testStringable(): void public function testInteger(): void { - $items = collect([ + $items = [ 106 => 2, 132 => 1, 133 => 30, 149 => 21, 150 => 3, - ]); + ]; $this->assertSame( [ @@ -145,13 +145,13 @@ public function testInteger(): void public function testFloat(): void { - $items = collect([ + $items = [ 106 => 2.2, 132 => 1.3, 133 => 30.5, 149 => 21.8, 150 => 3.0, - ]); + ]; $this->assertSame( [ @@ -168,7 +168,7 @@ public function testFloat(): void public function testEnumString(): void { - $items = collect([ + $items = [ 104 => StringValue::VALUE_ONE_SIZE, 105 => StringValue::VALUE_XXS, 106 => StringValue::VALUE_2, @@ -186,7 +186,7 @@ public function testEnumString(): void 150 => StringValue::VALUE_3, 155 => StringValue::VALUE_41_38_15, 156 => StringValue::VALUE_39_38_15, - ]); + ]; $this->assertSame( [ @@ -219,13 +219,13 @@ public function testEnumString(): void public function testEnumInteger(): void { - $items = collect([ + $items = [ 106 => IntegerValue::VALUE_2, 132 => IntegerValue::VALUE_1, 133 => IntegerValue::VALUE_30, 149 => IntegerValue::VALUE_21, 150 => IntegerValue::VALUE_3, - ]); + ]; $this->assertSame( [ @@ -242,7 +242,7 @@ public function testEnumInteger(): void public function testNestedArray(): void { - $items = collect([ + $items = [ 104 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => 'ONE SIZE']]], 105 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => 'XXS']]], 106 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => '2']]], @@ -261,7 +261,7 @@ public function testNestedArray(): void 150 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => '3']]], 155 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => '41х38х15 см']]], 156 => ['foo' => 'Foo', 'bar' => ['some' => ['nested' => '39х38х15 см']]], - ]); + ]; $this->assertSame( [ @@ -347,4 +347,57 @@ public function testLaravelModels(): void Sorter::sort($items)->pluck('value', 'id')->toArray() ); } + + public function testCollection(): void + { + $items = collect([ + 104 => 'ONE SIZE', + 105 => 'XXS', + 106 => '2', + 110 => 'M', + 113 => 'XL/2XL', + 116 => '80B', + 118 => '70B', + 130 => '44-46', + 131 => 'some', + 132 => '1', + 133 => '30', + 136 => '44/46', + 137 => 'XXS/XS', + 139 => '52-56', + 149 => '21', + 150 => '3', + 155 => '41х38х15 см', + 156 => '39х38х15 см', + ]); + + $this->assertSame( + [ + // 1 + 105 => 'XXS', + 137 => 'XXS/XS', + 110 => 'M', + 113 => 'XL/2XL', + // 2 + 132 => '1', + 106 => '2', + 150 => '3', + 149 => '21', + 133 => '30', + 130 => '44-46', + 136 => '44/46', + 139 => '52-56', + // 3 + 118 => '70B', + 116 => '80B', + // 4 + 156 => '39х38х15 см', + 155 => '41х38х15 см', + // 5 + 104 => 'ONE SIZE', + 131 => 'some', + ], + Sorter::sort($items)->toArray() + ); + } }