Add ENUM for ASC/DESC sort order
This commit is contained in:
2
l18n
2
l18n
Submodule l18n updated: 42ef9d90c7...7683733723
146
src/enums/MDDbQueryAscDesc.php
Normal file
146
src/enums/MDDbQueryAscDesc.php
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
<?PHP
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a sort order option in DB queries (ASC, DESC).
|
||||||
|
*/
|
||||||
|
enum MDDbQueryAscDesc implements MDValueEnumInterface, JsonSerializable {
|
||||||
|
|
||||||
|
case ASC;
|
||||||
|
case DESC;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a value of this type based on a string.
|
||||||
|
*
|
||||||
|
* @param string $input Input to get a value from.
|
||||||
|
*
|
||||||
|
* @return MDDbQueryAscDesc
|
||||||
|
*/
|
||||||
|
public static function fromString(string $input):MDDbQueryAscDesc {
|
||||||
|
|
||||||
|
return match($input) {
|
||||||
|
'asc' => self::ASC,
|
||||||
|
'ASC' => self::ASC,
|
||||||
|
'desc' => self::DESC,
|
||||||
|
'DESC' => self::DESC,
|
||||||
|
default => throw new MDpageParameterNotFromListException("Unknown sort order"),
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a value of this type based on an integer.
|
||||||
|
*
|
||||||
|
* @param integer $input Input to get a value from.
|
||||||
|
*
|
||||||
|
* @return MDDbQueryAscDesc
|
||||||
|
*/
|
||||||
|
public static function fromInt(int $input):MDDbQueryAscDesc {
|
||||||
|
|
||||||
|
return match($input) {
|
||||||
|
1 => self::ASC,
|
||||||
|
2 => self::DESC,
|
||||||
|
default => throw new MDpageParameterNotFromListException("Unknown sort order"),
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lists all available names.
|
||||||
|
*
|
||||||
|
* @return array<string>
|
||||||
|
*/
|
||||||
|
public static function caseNames():array {
|
||||||
|
|
||||||
|
$output = [];
|
||||||
|
|
||||||
|
$cases = self::cases();
|
||||||
|
foreach ($cases as $case) {
|
||||||
|
$output[] = $case->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $output;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an integer representation of the collective (for storage in DB).
|
||||||
|
*
|
||||||
|
* @return integer
|
||||||
|
*/
|
||||||
|
public function toInt():int {
|
||||||
|
|
||||||
|
return match($this) {
|
||||||
|
self::ASC => 1,
|
||||||
|
self::DESC => 2,
|
||||||
|
# default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the SQL representation of the entity.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toDbName():string {
|
||||||
|
|
||||||
|
return match ($this) {
|
||||||
|
self::ASC => 'ASC',
|
||||||
|
self::DESC => 'DESC',
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an unsorted list of the entries in a translated version.
|
||||||
|
*
|
||||||
|
* @param MDTlLoader $tlLoader Translation loader.
|
||||||
|
*
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
public static function getUnsortedList(MDTlLoader $tlLoader):array {
|
||||||
|
return MDValueSet::getTlUnsortedList($tlLoader, self::caseNames(), "query_asc_desc", "query_asc_desc");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a sorted list of the entries in a translated version.
|
||||||
|
*
|
||||||
|
* @param MDTlLoader $tlLoader Translation loader.
|
||||||
|
*
|
||||||
|
* @return array<string, string>
|
||||||
|
*/
|
||||||
|
public static function getSortedList(MDTlLoader $tlLoader):array {
|
||||||
|
return MDValueSet::getTlSortedList($tlLoader, self::caseNames(), "query_asc_desc", "query_asc_desc");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of the current value in translation.
|
||||||
|
*
|
||||||
|
* @param MDTlLoader $tlLoader Translation loader.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getTledName(MDTlLoader $tlLoader):string {
|
||||||
|
|
||||||
|
return $tlLoader->tl("query_asc_desc", "query_asc_desc", $this->name);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the option to serialize as a string during json_encode().
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function jsonSerialize():string {
|
||||||
|
|
||||||
|
return $this->name;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user