From 2d172a3c54d46c2858138ee2bcae43484e181050 Mon Sep 17 00:00:00 2001 From: Andrey Helldar Date: Mon, 20 Feb 2023 22:30:54 +0300 Subject: [PATCH] Update `Resolver` class --- composer.json | 1 - src/Services/Resolver.php | 44 ++--- tests/Fixtures/{ => Enums}/IntegerValue.php | 2 +- tests/Fixtures/{ => Enums}/StringValue.php | 2 +- tests/Fixtures/Helpers/Str.php | 20 +++ tests/Sorters/TypesTest.php | 177 ++++---------------- 6 files changed, 78 insertions(+), 168 deletions(-) rename tests/Fixtures/{ => Enums}/IntegerValue.php (84%) rename tests/Fixtures/{ => Enums}/StringValue.php (94%) create mode 100644 tests/Fixtures/Helpers/Str.php diff --git a/composer.json b/composer.json index 236671f..72dc41d 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,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" }, diff --git a/src/Services/Resolver.php b/src/Services/Resolver.php index 81f5c13..ad76e3f 100644 --- a/src/Services/Resolver.php +++ b/src/Services/Resolver.php @@ -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 @@ -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); } } diff --git a/tests/Fixtures/IntegerValue.php b/tests/Fixtures/Enums/IntegerValue.php similarity index 84% rename from tests/Fixtures/IntegerValue.php rename to tests/Fixtures/Enums/IntegerValue.php index a153544..e07256c 100644 --- a/tests/Fixtures/IntegerValue.php +++ b/tests/Fixtures/Enums/IntegerValue.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Fixtures; +namespace Tests\Fixtures\Enums; enum IntegerValue: int { diff --git a/tests/Fixtures/StringValue.php b/tests/Fixtures/Enums/StringValue.php similarity index 94% rename from tests/Fixtures/StringValue.php rename to tests/Fixtures/Enums/StringValue.php index 08ac27a..a62c2ba 100644 --- a/tests/Fixtures/StringValue.php +++ b/tests/Fixtures/Enums/StringValue.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Tests\Fixtures; +namespace Tests\Fixtures\Enums; enum StringValue: string { diff --git a/tests/Fixtures/Helpers/Str.php b/tests/Fixtures/Helpers/Str.php new file mode 100644 index 0000000..b03f146 --- /dev/null +++ b/tests/Fixtures/Helpers/Str.php @@ -0,0 +1,20 @@ +value; + } +} diff --git a/tests/Sorters/TypesTest.php b/tests/Sorters/TypesTest.php index 990a446..647bb3e 100644 --- a/tests/Sorters/TypesTest.php +++ b/tests/Sorters/TypesTest.php @@ -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 @@ -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( @@ -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() ); @@ -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() ); @@ -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() );