diff --git a/src/Contracts/GroupMatcher.php b/src/Contracts/GroupMatcher.php index 37d3d4f..ff5899b 100644 --- a/src/Contracts/GroupMatcher.php +++ b/src/Contracts/GroupMatcher.php @@ -6,5 +6,5 @@ interface GroupMatcher { - public function detect(string $value): bool; + public static function detect(string $value): bool; } diff --git a/src/Contracts/Sorter.php b/src/Contracts/Sorter.php index 72be78a..ba21f19 100644 --- a/src/Contracts/Sorter.php +++ b/src/Contracts/Sorter.php @@ -6,5 +6,5 @@ interface Sorter { - public function callback(string $column, int $arrow = 1): callable; + public static function callback(string $column, int $arrow = 1): callable; } diff --git a/src/Detectors/Base.php b/src/Detectors/Base.php index a9dd489..a54c37d 100644 --- a/src/Detectors/Base.php +++ b/src/Detectors/Base.php @@ -5,25 +5,18 @@ namespace DragonCode\SizeSorter\Detectors; use DragonCode\SizeSorter\Contracts\GroupMatcher; -use DragonCode\SizeSorter\Services\Resolver; use DragonCode\SizeSorter\Services\Str; abstract class Base implements GroupMatcher { - protected array|string $pattern; + protected static array|string $pattern; - public function __construct( - protected Str $str = new Str(), - protected Resolver $resolver = new Resolver() - ) { - } - - public function detect(string $value): bool + public static function detect(string $value): bool { - return $this->str->match($this->prepare($value), $this->pattern); + return Str::match(static::prepare($value), static::$pattern); } - protected function prepare(string $value): string + protected static function prepare(string $value): string { return $value; } diff --git a/src/Detectors/Group1.php b/src/Detectors/Group1.php index 067de2e..33b1298 100644 --- a/src/Detectors/Group1.php +++ b/src/Detectors/Group1.php @@ -6,5 +6,5 @@ class Group1 extends Base { - protected array|string $pattern = '/^(x*[sml])$/'; + protected static array|string $pattern = '/^(x*[sml])$/'; } diff --git a/src/Detectors/Group2.php b/src/Detectors/Group2.php index 1c61f2e..37106b5 100644 --- a/src/Detectors/Group2.php +++ b/src/Detectors/Group2.php @@ -4,12 +4,14 @@ namespace DragonCode\SizeSorter\Detectors; +use DragonCode\SizeSorter\Services\Str; + class Group2 extends Base { - protected array|string $pattern = '/^(\d+-?\d*)$/'; + protected static array|string $pattern = '/^(\d+-?\d*)$/'; - protected function prepare(string $value): string + protected static function prepare(string $value): string { - return $this->str->slug($value); + return Str::slug($value); } } diff --git a/src/Detectors/Group3.php b/src/Detectors/Group3.php index e79335b..82e4221 100644 --- a/src/Detectors/Group3.php +++ b/src/Detectors/Group3.php @@ -4,12 +4,14 @@ namespace DragonCode\SizeSorter\Detectors; +use DragonCode\SizeSorter\Services\Str; + class Group3 extends Base { - protected array|string $pattern = '/^(\d+[a-f]{1,2})$/'; + protected static array|string $pattern = '/^(\d+[a-f]{1,2})$/'; - protected function prepare(string $value): string + protected static function prepare(string $value): string { - return $this->str->slug($value); + return Str::slug($value); } } diff --git a/src/Detectors/Group4.php b/src/Detectors/Group4.php index e5c422d..938c8fa 100644 --- a/src/Detectors/Group4.php +++ b/src/Detectors/Group4.php @@ -4,31 +4,34 @@ namespace DragonCode\SizeSorter\Detectors; +use DragonCode\SizeSorter\Services\Resolver; +use DragonCode\SizeSorter\Services\Str; + class Group4 extends Base { - protected array|string $pattern = [ + protected static array|string $pattern = [ '/^([\d\-hx\*]*0s0m)$/', '/^(\d+[a-f])$/', ]; - protected function prepare(string $value): string + protected static function prepare(string $value): string { - return $this->str->of($value) + return Str::of($value) ->squish() ->trim() ->replace('\\', '/') ->explode('/') - ->map(fn (string $value) => $this->compact($value)) + ->map(static fn (string $value) => static::compact($value)) ->implode('/') ->toString(); } - protected function compact(string $value): string + protected static function compact(string $value): string { - return $this->str->of($value) + return Str::of($value) ->slug() ->explode('-') - ->map(fn (string $value) => $this->resolver->size($value)) + ->map(static fn (string $value) => Resolver::size($value)) ->implode('-') ->toString(); } diff --git a/src/Enum/Groups.php b/src/Enum/Group.php similarity index 89% rename from src/Enum/Groups.php rename to src/Enum/Group.php index dbb2e90..e023f12 100644 --- a/src/Enum/Groups.php +++ b/src/Enum/Group.php @@ -14,7 +14,7 @@ * @method static int GROUP_4() * @method static int GROUP_5() */ -enum Groups: int +enum Group: int { use InvokableCases; use Values; @@ -30,7 +30,7 @@ enum Groups: int // Other values case GROUP_5 = 5; - public static function exists(Groups|string|int $group): bool + public static function exists(Group|string|int $group): bool { $value = $group->value ?? (int) $group; diff --git a/src/Services/Collection.php b/src/Services/Collection.php index 3a9daf7..a5eec27 100644 --- a/src/Services/Collection.php +++ b/src/Services/Collection.php @@ -8,32 +8,32 @@ class Collection { - public function make(array $items = []): IC + public static function make(array $items = []): IC { return new IC($items); } - public function flatten(IC $items): IC + public static function flatten(IC $items): IC { - $result = $this->make(); + $result = static::make(); - $items->each(function (mixed $value, mixed $key) use (&$result) { + $items->each(static function (mixed $value, mixed $key) use (&$result) { if (! $value instanceof IC) { $result->put($key, $value); return; } - $result = $this->merge($result, $this->flatten($value)); + $result = static::merge($result, static::flatten($value)); }); return $result; } - public function merge(IC $result, IC $second): IC + public static function merge(IC $result, IC $second): IC { $second->each( - fn (mixed $value, mixed $key) => $result->put($key, $value) + static fn (mixed $value, mixed $key) => $result->put($key, $value) ); return $result; diff --git a/src/Services/GroupsDetector.php b/src/Services/GroupsDetector.php index 766e74d..b256df6 100644 --- a/src/Services/GroupsDetector.php +++ b/src/Services/GroupsDetector.php @@ -4,43 +4,33 @@ namespace DragonCode\SizeSorter\Services; -use DragonCode\SizeSorter\Contracts\GroupMatcher; +use DragonCode\SizeSorter\Detectors\Base as BaseDetector; use DragonCode\SizeSorter\Detectors\Group1; use DragonCode\SizeSorter\Detectors\Group2; use DragonCode\SizeSorter\Detectors\Group3; use DragonCode\SizeSorter\Detectors\Group4; -use DragonCode\SizeSorter\Enum\Groups; +use DragonCode\SizeSorter\Enum\Group; class GroupsDetector { - /** @var array */ - protected array $detectors = [ - Group1::class => Groups::GROUP_1, - Group2::class => Groups::GROUP_2, - Group3::class => Groups::GROUP_3, - Group4::class => Groups::GROUP_4, + /** @var array */ + protected static array $detectors = [ + Group1::class => Group::GROUP_1, + Group2::class => Group::GROUP_2, + Group3::class => Group::GROUP_3, + Group4::class => Group::GROUP_4, ]; - protected Groups $default = Groups::GROUP_5; + protected static Group $default = Group::GROUP_5; - public function __construct( - protected Str $str = new Str() - ) { - } - - public function detect(string $value): int + public static function detect(string $value): int { - foreach ($this->detectors as $detector => $group) { - if ($this->resolve($detector)->detect($value)) { + foreach (static::$detectors as $detector => $group) { + if ($detector::detect($value)) { return $group->value; } } - return $this->default->value; - } - - protected function resolve(string $matcher): GroupMatcher - { - return new $matcher($this->str); + return static::$default->value; } } diff --git a/src/Services/Resolver.php b/src/Services/Resolver.php index 49949b2..dc6ec48 100644 --- a/src/Services/Resolver.php +++ b/src/Services/Resolver.php @@ -8,55 +8,50 @@ class Resolver { - public function __construct( - protected Str $str = new Str() - ) { - } - - public function key(mixed $value, string $column): mixed + public static function key(mixed $value, string $column): mixed { return $value->{$column} ?? $value[$column] ?? $value; } - public function value(mixed $value, string $column): Stringable + public static function value(mixed $value, string $column): Stringable { - return $this->prepare($value, $column) + return static::prepare($value, $column) ->replace(['\\', '-'], '/') ->before('/') ->lower(); } - public function number(mixed $value, string $column): array + public static function number(mixed $value, string $column): array { - return $this->prepare($value, $column) + return static::prepare($value, $column) ->replace(['\\', '-'], '/') ->explode('/') - ->map(fn (mixed $val) => (int) $val) + ->map(static fn (mixed $val) => (int) $val) ->toArray(); } - public function size(string $value): string + public static function size(string $value): string { - if ($this->str->match($value, '/(\d+x)/')) { - return $this->str->replace('x', '', $value); + if (Str::match($value, '/(\d+x)/')) { + return Str::replace('x', '', $value); } - if ($count = $this->str->count($value)) { - return $this->str->replace($value, $this->str->pad($count), $count); + if ($count = Str::count($value)) { + return Str::replace($value, Str::pad($count), $count); } - return $this->str->replace($value, ['s', 'm', 'l'], ['0s', '0m', '0l']); + return Str::replace($value, ['s', 'm', 'l'], ['0s', '0m', '0l']); } - public function callback(callable $callback, mixed ...$parameters): mixed + public static function callback(callable $callback, mixed ...$parameters): mixed { return $callback(...$parameters); } - protected function prepare(mixed $value, string $column): Stringable + protected static function prepare(mixed $value, string $column): Stringable { - return $this->str->of( - $this->key($value, $column) + return Str::of( + static::key($value, $column) ) ->squish() ->trim(); diff --git a/src/Services/Sorter.php b/src/Services/Sorter.php index 9709453..1ee3d1a 100644 --- a/src/Services/Sorter.php +++ b/src/Services/Sorter.php @@ -5,41 +5,23 @@ namespace DragonCode\SizeSorter\Services; use DragonCode\SizeSorter\Sorters\Arrow; -use DragonCode\SizeSorter\Sorters\Base; use DragonCode\SizeSorter\Sorters\Chars; use DragonCode\SizeSorter\Sorters\Numbers; class Sorter { - protected array $registry = []; - - public function __construct( - protected Resolver $resolver = new Resolver(), - protected Str $str = new Str() - ) { - } - - public function byArrow(string $column, int $arrow = 1): callable + public static function byArrow(string $column, int $arrow = 1): callable { - return $this->resolve(Arrow::class)->callback($column, $arrow); + return Arrow::callback($column, $arrow); } - public function byChars(string $column, int $arrow = 1): callable + public static function byChars(string $column, int $arrow = 1): callable { - return $this->resolve(Chars::class)->callback($column, $arrow); + return Chars::callback($column, $arrow); } - public function byNumbers(string $column, int $arrow = 1): callable + public static function byNumbers(string $column, int $arrow = 1): callable { - return $this->resolve(Numbers::class)->callback($column, $arrow); - } - - protected function resolve(string $class): Base - { - if (isset($this->registry[$class])) { - return $this->registry[$class]; - } - - return $this->registry[$class] = new $class($this->resolver, $this->str); + return Numbers::callback($column, $arrow); } } diff --git a/src/Services/Str.php b/src/Services/Str.php index 6bbfc42..daa9964 100644 --- a/src/Services/Str.php +++ b/src/Services/Str.php @@ -9,37 +9,37 @@ class Str { - public function of(?string $value): Stringable + public static function of(?string $value): Stringable { return DS::of($value); } - public function pad(int $length, string $pad = 'x'): string + public static function pad(int $length, string $pad = 'x'): string { return str_pad('', $length, $pad); } - public function replace(string $value, array|string $search, array|string|int $replace): string + public static function replace(string $value, array|string $search, array|string|int $replace): string { return DS::replace($value, $search, $replace); } - public function contains(string $value, string $needle): bool + public static function contains(string $value, string $needle): bool { return DS::contains($value, $needle); } - public function match(string $value, array|string $pattern): bool + public static function match(string $value, array|string $pattern): bool { return DS::matchContains($value, $pattern); } - public function slug(string $value): string + public static function slug(string $value): string { return DS::slug($value); } - public function count(string $value, string $needle = 'x'): int + public static function count(string $value, string $needle = 'x'): int { return DS::count($value, $needle); } diff --git a/src/Sorter.php b/src/Sorter.php index dcac9ae..53c6663 100644 --- a/src/Sorter.php +++ b/src/Sorter.php @@ -4,7 +4,7 @@ namespace DragonCode\SizeSorter; -use DragonCode\SizeSorter\Enum\Groups; +use DragonCode\SizeSorter\Enum\Group; use DragonCode\SizeSorter\Services\Collection; use DragonCode\SizeSorter\Services\GroupsDetector; use DragonCode\SizeSorter\Services\Resolver; @@ -14,103 +14,95 @@ class Sorter { - public function __construct( - protected GroupsDetector $groupsDetector = new GroupsDetector(), - protected Collection $collection = new Collection(), - protected Resolver $resolver = new Resolver(), - protected SorterService $sorter = new SorterService() - ) { - } - public static function sort(IlluminateCollection $items, string $column = 'value'): IlluminateCollection { - return (new static())->handle($items, $column); + return static::handle($items, $column); } - protected function handle(IlluminateCollection $items, string $column = 'value'): IlluminateCollection + protected static function handle(IlluminateCollection $items, string $column = 'value'): IlluminateCollection { - return $this->flatten( - $this->sorting($items, $column) + return static::flatten( + static::sorting($items, $column) ); } - protected function sorting(IlluminateCollection $items, string $column = 'value'): IlluminateCollection + public static function sorting(IlluminateCollection $items, string $column = 'value'): IlluminateCollection { return $items - ->groupBy(fn (mixed $size) => $this->detectGroup( - $this->resolveValue($size, $column)->toString() + ->groupBy(static fn (mixed $size) => static::detectGroup( + static::resolveValue($size, $column)->toString() ), true) ->sortKeys() - ->map(fn (IlluminateCollection $items, int $group) => $this->sortByGroup($items, $group, $column)); + ->map(static fn (IlluminateCollection $items, int $group) => static::sortByGroup($items, $group, $column)); } - protected function sortByGroup(IlluminateCollection $items, int $group, string $column): IlluminateCollection + public static function sortByGroup(IlluminateCollection $items, int $group, string $column): IlluminateCollection { return match ($group) { - Groups::GROUP_1() => $this->sortChars($items, $column), - Groups::GROUP_2() => $this->sortNumbers($items, $column), - default => $this->sortArrows($items, $column) + Group::GROUP_1() => static::sortChars($items, $column), + Group::GROUP_2() => static::sortNumbers($items, $column), + default => static::sortArrows($items, $column) }; } - protected function sortChars(IlluminateCollection $values, string $column): IlluminateCollection + public static function sortChars(IlluminateCollection $values, string $column): IlluminateCollection { return $values->groupBy( - fn (mixed $size) => $this->resolveValue($size, $column) + static fn (mixed $size) => static::resolveValue($size, $column) ->match('/(s|m|l)/') ->toString(), true ) ->sortKeysDesc() ->map( - fn (IlluminateCollection $values, string $group) => $group === 's' - ? $this->sortSmallSizes($values, $column) - : $this->sortArrows($values, $column) + static fn (IlluminateCollection $values, string $group) => $group === 's' + ? static::sortSmallSizes($values, $column) + : static::sortArrows($values, $column) ); } - protected function sortSmallSizes(IlluminateCollection $values, string $column): IlluminateCollection + public static function sortSmallSizes(IlluminateCollection $values, string $column): IlluminateCollection { return $values->sort( - $this->sorter->byArrow($column, -1) + SorterService::byArrow($column, -1) ) - ->groupBy(fn (mixed $size) => $this->resolveValue($size, $column)->toString(), true) - ->map(fn (IlluminateCollection $values) => $this->sortSpecialChars($values, $column)); + ->groupBy(static fn (mixed $size) => static::resolveValue($size, $column)->toString(), true) + ->map(static fn (IlluminateCollection $values) => static::sortSpecialChars($values, $column)); } - protected function sortSpecialChars(IlluminateCollection $values, string $column): IlluminateCollection + public static function sortSpecialChars(IlluminateCollection $values, string $column): IlluminateCollection { return $values->sort( - $this->sorter->byChars($column) + SorterService::byChars($column) ); } - protected function sortArrows(IlluminateCollection $values, string $column): IlluminateCollection + public static function sortArrows(IlluminateCollection $values, string $column): IlluminateCollection { return $values->sort( - $this->sorter->byArrow($column) + SorterService::byArrow($column) ); } - protected function sortNumbers(IlluminateCollection $items, string $column): IlluminateCollection + public static function sortNumbers(IlluminateCollection $items, string $column): IlluminateCollection { return $items->sort( - $this->sorter->byNumbers($column) + SorterService::byNumbers($column) ); } - protected function detectGroup(string $value): int + public static function detectGroup(string $value): int { - return $this->groupsDetector->detect($value); + return GroupsDetector::detect($value); } - protected function flatten(IlluminateCollection $items): IlluminateCollection + public static function flatten(IlluminateCollection $items): IlluminateCollection { - return $this->collection->flatten($items); + return Collection::flatten($items); } - protected function resolveValue(mixed $value, string $column): Stringable + public static function resolveValue(mixed $value, string $column): Stringable { - return $this->resolver->value($value, $column); + return Resolver::value($value, $column); } } diff --git a/src/Sorters/Arrow.php b/src/Sorters/Arrow.php index d66382f..2ec7002 100644 --- a/src/Sorters/Arrow.php +++ b/src/Sorters/Arrow.php @@ -6,11 +6,11 @@ class Arrow extends Base { - public function callback(string $column, int $arrow = 1): callable + public static function callback(string $column, int $arrow = 1): callable { - return function (mixed $a, mixed $b) use ($arrow, $column) { - $a = $this->key($a, $column); - $b = $this->key($b, $column); + return static function (mixed $a, mixed $b) use ($arrow, $column) { + $a = static::key($a, $column); + $b = static::key($b, $column); if ($a === $b) { return 0; diff --git a/src/Sorters/Base.php b/src/Sorters/Base.php index c84fb18..6450fc6 100644 --- a/src/Sorters/Base.php +++ b/src/Sorters/Base.php @@ -6,23 +6,11 @@ use DragonCode\SizeSorter\Contracts\Sorter; use DragonCode\SizeSorter\Services\Resolver; -use DragonCode\SizeSorter\Services\Str; abstract class Base implements Sorter { - public function __construct( - protected Resolver $resolver = new Resolver(), - protected Str $str = new Str() - ) { - } - - protected function key(mixed $value, string $column): mixed - { - return $this->resolver->key($value, $column); - } - - protected function sorter(string $sorter): Base + protected static function key(mixed $value, string $column): mixed { - return new $sorter($this->resolver, $this->str); + return Resolver::key($value, $column); } } diff --git a/src/Sorters/Chars.php b/src/Sorters/Chars.php index e349bf1..c15dcb1 100644 --- a/src/Sorters/Chars.php +++ b/src/Sorters/Chars.php @@ -4,27 +4,30 @@ namespace DragonCode\SizeSorter\Sorters; +use DragonCode\SizeSorter\Services\Resolver; +use DragonCode\SizeSorter\Services\Str; + class Chars extends Base { - public function callback(string $column, int $arrow = 1): callable + public static function callback(string $column, int $arrow = 1): callable { - return function (mixed $a, mixed $b) use ($column, $arrow) { - $a = $this->key($a, $column); - $b = $this->key($b, $column); + return static function (mixed $a, mixed $b) use ($column, $arrow) { + $a = static::key($a, $column); + $b = static::key($b, $column); - $arrow = $this->contains($a, '/') && $this->contains($b, '-') ? -1 : 1; + $arrow = static::contains($a, '/') && static::contains($b, '-') ? -1 : 1; - return $this->resolver->callback($this->resolveArrow($arrow, $column), $a, $b); + return Resolver::callback(static::resolveArrow($arrow, $column), $a, $b); }; } - protected function resolveArrow(int $arrow, string $column): callable + protected static function resolveArrow(int $arrow, string $column): callable { - return $this->sorter(Arrow::class)->callback($column, $arrow); + return Arrow::callback($column, $arrow); } - protected function contains(string $value, string $needle): bool + protected static function contains(string $value, string $needle): bool { - return $this->str->contains($value, $needle); + return Str::contains($value, $needle); } } diff --git a/src/Sorters/Numbers.php b/src/Sorters/Numbers.php index cf90c30..5639565 100644 --- a/src/Sorters/Numbers.php +++ b/src/Sorters/Numbers.php @@ -4,13 +4,15 @@ namespace DragonCode\SizeSorter\Sorters; +use DragonCode\SizeSorter\Services\Resolver; + class Numbers extends Base { - public function callback(string $column, int $arrow = 1): callable + public static function callback(string $column, int $arrow = 1): callable { - return function (mixed $a, mixed $b) use ($column, $arrow) { - $a = $this->number($a, $column); - $b = $this->number($b, $column); + return static function (mixed $a, mixed $b) use ($column, $arrow) { + $a = static::number($a, $column); + $b = static::number($b, $column); if ($a[0] === $b[0]) { if (isset($a[1], $b[1])) { @@ -26,8 +28,8 @@ public function callback(string $column, int $arrow = 1): callable }; } - protected function number(mixed $value, string $column): array + protected static function number(mixed $value, string $column): array { - return $this->resolver->number($value, $column); + return Resolver::number($value, $column); } }