Skip to content

Commit

Permalink
Merge pull request #3 from TheDragonCode/1.x
Browse files Browse the repository at this point in the history
Refactored to statically methods (~7ms saved)
  • Loading branch information
andrey-helldar authored Feb 2, 2023
2 parents c7905a4 + 38d3126 commit 3807204
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 187 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/GroupMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface GroupMatcher
{
public function detect(string $value): bool;
public static function detect(string $value): bool;
}
2 changes: 1 addition & 1 deletion src/Contracts/Sorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 4 additions & 11 deletions src/Detectors/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Detectors/Group1.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class Group1 extends Base
{
protected array|string $pattern = '/^(x*[sml])$/';
protected static array|string $pattern = '/^(x*[sml])$/';
}
8 changes: 5 additions & 3 deletions src/Detectors/Group2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 5 additions & 3 deletions src/Detectors/Group3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 10 additions & 7 deletions src/Detectors/Group4.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Enum/Groups.php → src/Enum/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @method static int GROUP_4()
* @method static int GROUP_5()
*/
enum Groups: int
enum Group: int
{
use InvokableCases;
use Values;
Expand All @@ -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;

Expand Down
14 changes: 7 additions & 7 deletions src/Services/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 13 additions & 23 deletions src/Services/GroupsDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<class-string, Groups> */
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<class-string|BaseDetector, Group> */
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;
}
}
37 changes: 16 additions & 21 deletions src/Services/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
30 changes: 6 additions & 24 deletions src/Services/Sorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading

0 comments on commit 3807204

Please sign in to comment.