Skip to content

Commit

Permalink
Merge pull request #12 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Added the ability to pass any iterable object
  • Loading branch information
andrey-helldar authored Feb 21, 2023
2 parents d7cce81 + ee37d61 commit ea0ac84
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/Sorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DragonCode\SizeSorter;

use ArrayAccess;
use DragonCode\SizeSorter\Enum\Group;
use DragonCode\SizeSorter\Services\Collection;
use DragonCode\SizeSorter\Services\GroupsDetector;
Expand All @@ -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))
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Sorters/FullGroupsOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Sorters/PartialGroupsOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Sorters/SorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function testArray(): void
{
$this->assertSame(
$this->expected,
Sorter::sort(collect($this->values))->toArray()
Sorter::sort($this->values)->toArray()
);
}

Expand Down Expand Up @@ -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());
}
}
81 changes: 67 additions & 14 deletions tests/Sorters/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TypesTest extends TestCase
{
public function testString(): void
{
$items = collect([
$items = [
104 => 'ONE SIZE',
105 => 'XXS',
106 => '2',
Expand All @@ -35,7 +35,7 @@ public function testString(): void
150 => '3',
155 => '41х38х15 см',
156 => '39х38х15 см',
]);
];

$this->assertSame(
[
Expand Down Expand Up @@ -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'),
Expand All @@ -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(
[
Expand Down Expand Up @@ -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(
[
Expand All @@ -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(
[
Expand All @@ -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,
Expand All @@ -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(
[
Expand Down Expand Up @@ -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(
[
Expand All @@ -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']]],
Expand All @@ -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(
[
Expand Down Expand Up @@ -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()
);
}
}

0 comments on commit ea0ac84

Please sign in to comment.