Skip to content

Commit

Permalink
Merge pull request #8 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Update `Resolver` class
  • Loading branch information
andrey-helldar authored Feb 20, 2023
2 parents 4ed17e8 + 2d172a3 commit 8ea6854
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 168 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
},
"require-dev": {
"fakerphp/faker": "^1.21",
"illuminate/support": "^8.75 || ^9.0 || ^10.0",
"phpunit/phpunit": "^9.6",
"symfony/var-dumper": "^5.3 || ^6.0"
},
Expand Down
44 changes: 24 additions & 20 deletions src/Services/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,19 @@
use BackedEnum;
use DragonCode\Support\Facades\Helpers\Arr;
use DragonCode\Support\Helpers\Ables\Stringable;
use Stringable as BaseStringable;

class Resolver
{
public static function key(mixed $value, string $column): mixed
{
if (is_object($value) && $value instanceof BackedEnum) {
return $value->value ?? $value->name;
}

if (is_object($value) && $value instanceof \Stringable) {
return method_exists($value, 'toString') ? $value->toString() : (string) $value;
}

if (is_array($value) || $value instanceof ArrayAccess) {
return Arr::get($value, $column, $value);
}

if (is_object($value)) {
return $value->{$column} ?? $value;
}

return $value;
return match (true) {
$value instanceof BackedEnum => self::fromEnum($value),
$value instanceof BaseStringable => self::fromStringable($value),
$value instanceof ArrayAccess,
is_array($value) => self::fromArray($value, $column),
default => $value->{$column} ?? $value
};
}

public static function value(mixed $value, string $column): Stringable
Expand Down Expand Up @@ -71,8 +62,21 @@ protected static function prepare(mixed $value, string $column): Stringable
{
return Str::of(
static::key($value, $column)
)
->squish()
->trim();
)->squish()->trim();
}

protected static function fromEnum(BackedEnum $item): mixed
{
return $item->value ?? $item->name;
}

protected static function fromStringable(BaseStringable $item): mixed
{
return method_exists($item, 'toString') ? $item->toString() : (string) $item;
}

protected static function fromArray(mixed $item, string $column): mixed
{
return Arr::get($item, $column, $item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Fixtures;
namespace Tests\Fixtures\Enums;

enum IntegerValue: int
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Tests\Fixtures;
namespace Tests\Fixtures\Enums;

enum StringValue: string
{
Expand Down
20 changes: 20 additions & 0 deletions tests/Fixtures/Helpers/Str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Helpers;

use Stringable;

class Str implements Stringable
{
public function __construct(
protected mixed $value
) {
}

public function __toString(): string
{
return (string) $this->value;
}
}
177 changes: 32 additions & 145 deletions tests/Sorters/TypesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace Tests\Sorters;

use DragonCode\SizeSorter\Sorter;
use DragonCode\Support\Facades\Helpers\Str as DragonStr;
use Illuminate\Support\Str as IlluminateStr;
use Tests\Fixtures\StringValue;
use Tests\Fixtures\Enums\IntegerValue;
use Tests\Fixtures\Enums\StringValue;
use Tests\Fixtures\Helpers\Str;
use Tests\TestCase;

class TypesTest extends TestCase
Expand Down Expand Up @@ -65,80 +65,27 @@ public function testString(): void
);
}

public function testIlluminateStringable(): void
public function testStringable(): void
{
$items = collect([
104 => IlluminateStr::of('ONE SIZE'),
105 => IlluminateStr::of('XXS'),
106 => IlluminateStr::of('2'),
110 => IlluminateStr::of('M'),
113 => IlluminateStr::of('XL/2XL'),
116 => IlluminateStr::of('80B'),
118 => IlluminateStr::of('70B'),
130 => IlluminateStr::of('44-46'),
131 => IlluminateStr::of('some'),
132 => IlluminateStr::of('1'),
133 => IlluminateStr::of('30'),
136 => IlluminateStr::of('44/46'),
137 => IlluminateStr::of('XXS/XS'),
139 => IlluminateStr::of('52-56'),
149 => IlluminateStr::of('21'),
150 => IlluminateStr::of('3'),
155 => IlluminateStr::of('41х38х15 см'),
156 => IlluminateStr::of('39х38х15 см'),
]);

$this->assertSame(
[
// 1
105 => $items[105],
137 => $items[137],
110 => $items[110],
113 => $items[113],
// 2
132 => $items[132],
106 => $items[106],
150 => $items[150],
149 => $items[149],
133 => $items[133],
130 => $items[130],
136 => $items[136],
139 => $items[139],
// 3
118 => $items[118],
116 => $items[116],
// 4
156 => $items[156],
155 => $items[155],
// 5
104 => $items[104],
131 => $items[131],
],
Sorter::sort($items)->toArray()
);
}

public function testDragonStringable(): void
{
$items = collect([
104 => DragonStr::of('ONE SIZE'),
105 => DragonStr::of('XXS'),
106 => DragonStr::of('2'),
110 => DragonStr::of('M'),
113 => DragonStr::of('XL/2XL'),
116 => DragonStr::of('80B'),
118 => DragonStr::of('70B'),
130 => DragonStr::of('44-46'),
131 => DragonStr::of('some'),
132 => DragonStr::of('1'),
133 => DragonStr::of('30'),
136 => DragonStr::of('44/46'),
137 => DragonStr::of('XXS/XS'),
139 => DragonStr::of('52-56'),
149 => DragonStr::of('21'),
150 => DragonStr::of('3'),
155 => DragonStr::of('41х38х15 см'),
156 => DragonStr::of('39х38х15 см'),
104 => new Str('ONE SIZE'),
105 => new Str('XXS'),
106 => new Str('2'),
110 => new Str('M'),
113 => new Str('XL/2XL'),
116 => new Str('80B'),
118 => new Str('70B'),
130 => new Str('44-46'),
131 => new Str('some'),
132 => new Str('1'),
133 => new Str('30'),
136 => new Str('44/46'),
137 => new Str('XXS/XS'),
139 => new Str('52-56'),
149 => new Str('21'),
150 => new Str('3'),
155 => new Str('41х38х15 см'),
156 => new Str('39х38х15 см'),
]);

$this->assertSame(
Expand Down Expand Up @@ -174,51 +121,21 @@ public function testDragonStringable(): void
public function testInteger(): 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()
);
Expand All @@ -227,51 +144,21 @@ public function testInteger(): void
public function testFloat(): void
{
$items = collect([
104 => 'ONE SIZE',
105 => 'XXS',
106 => 2.2,
110 => 'M',
113 => 'XL/2XL',
116 => '80B',
118 => '70B',
130 => '44-46',
131 => 'some',
132 => 1.3,
133 => 30.5,
136 => '44/46',
137 => 'XXS/XS',
139 => '52-56',
149 => 21.8,
150 => 3.0,
155 => '41х38х15 см',
156 => '39х38х15 см',
]);

$this->assertSame(
[
// 1
105 => 'XXS',
137 => 'XXS/XS',
110 => 'M',
113 => 'XL/2XL',
// 2
132 => 1.3,
106 => 2.2,
150 => 3.0,
149 => 21.8,
133 => 30.5,
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()
);
Expand Down Expand Up @@ -331,21 +218,21 @@ public function testEnumString(): void
public function testEnumInteger(): void
{
$items = collect([
106 => StringValue::VALUE_2,
132 => StringValue::VALUE_1,
133 => StringValue::VALUE_30,
149 => StringValue::VALUE_21,
150 => StringValue::VALUE_3,
106 => IntegerValue::VALUE_2,
132 => IntegerValue::VALUE_1,
133 => IntegerValue::VALUE_30,
149 => IntegerValue::VALUE_21,
150 => IntegerValue::VALUE_3,
]);

$this->assertSame(
[
// 2
132 => StringValue::VALUE_1,
106 => StringValue::VALUE_2,
150 => StringValue::VALUE_3,
149 => StringValue::VALUE_21,
133 => StringValue::VALUE_30,
132 => IntegerValue::VALUE_1,
106 => IntegerValue::VALUE_2,
150 => IntegerValue::VALUE_3,
149 => IntegerValue::VALUE_21,
133 => IntegerValue::VALUE_30,
],
Sorter::sort($items)->toArray()
);
Expand Down

0 comments on commit 8ea6854

Please sign in to comment.