-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from TheDragonCode/1.x
Added the ability to change the sort order of logical groups
- Loading branch information
Showing
5 changed files
with
539 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DragonCode\SizeSorter\Services; | ||
|
||
use DragonCode\SizeSorter\Enum\Group; | ||
|
||
class Order | ||
{ | ||
public static function resolve(?array $groups): array | ||
{ | ||
return ! empty($groups) | ||
? static::fill($groups) | ||
: static::groups(); | ||
} | ||
|
||
protected static function fill(array $groups): array | ||
{ | ||
return static::fillDefault( | ||
static::fillCustom($groups) | ||
); | ||
} | ||
|
||
protected static function fillCustom(array $groups): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($groups as $group) { | ||
$value = static::resolveValue($group); | ||
|
||
if (static::existGroup($value)) { | ||
$result[] = $value; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
protected static function fillDefault(array $groups): array | ||
{ | ||
foreach (static::groups() as $group) { | ||
if (! in_array($group, $groups)) { | ||
$groups[] = $group; | ||
} | ||
} | ||
|
||
return $groups; | ||
} | ||
|
||
protected static function existGroup(int $value): bool | ||
{ | ||
return Group::exists($value); | ||
} | ||
|
||
/** | ||
* @return array<Group> | ||
*/ | ||
protected static function groups(): array | ||
{ | ||
return Group::values(); | ||
} | ||
|
||
protected static function resolveValue(Group|int $group): int | ||
{ | ||
return $group->value ?? $group; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.