Convert ebook SORT_ constants to an enum

This commit is contained in:
Mike Colagrosso 2024-01-29 23:34:48 -07:00 committed by Alex Cabal
parent 94dce1009a
commit 980ed8cea9
5 changed files with 17 additions and 16 deletions

View file

@ -134,8 +134,6 @@ Before submitting design contributions, please discuss them with the Standard Eb
- Finding a self-hosted replacement for GitHub, like possibly [Gitea](https://gitea.io/) or [GitLab](https://about.gitlab.com/), and figuring out how to reproducably install and update it on the SE server. - Finding a self-hosted replacement for GitHub, like possibly [Gitea](https://gitea.io/) or [GitLab](https://about.gitlab.com/), and figuring out how to reproducably install and update it on the SE server.
- Converting some constants to enums, like `SORT_*` or `SOURCE_*`.
## PHP code style ## PHP code style
- Indent with tabs. - Indent with tabs.

View file

@ -32,10 +32,6 @@ const DATABASE_DEFAULT_DATABASE = 'se';
const DATABASE_DEFAULT_HOST = 'localhost'; const DATABASE_DEFAULT_HOST = 'localhost';
const EBOOKS_PER_PAGE = 12; const EBOOKS_PER_PAGE = 12;
const SORT_NEWEST = 'newest';
const SORT_AUTHOR_ALPHA = 'author-alpha';
const SORT_READING_EASE = 'reading-ease';
const SORT_LENGTH = 'length';
const ARTWORK_THUMBNAIL_HEIGHT = 350; const ARTWORK_THUMBNAIL_HEIGHT = 350;
const ARTWORK_THUMBNAIL_WIDTH = 350; const ARTWORK_THUMBNAIL_WIDTH = 350;

7
lib/EbookSort.php Normal file
View file

@ -0,0 +1,7 @@
<?
enum EbookSort: string{
case Newest = 'newest';
case AuthorAlpha = 'author-alpha';
case ReadingEase = 'reading-ease';
case Length = 'length';
}

View file

@ -16,7 +16,7 @@ class Library{
/** /**
* @param string $query * @param string $query
* @param array<string> $tags * @param array<string> $tags
* @param string $sort * @param EbookSort $sort
* @return array<Ebook> * @return array<Ebook>
*/ */
public static function FilterEbooks(string $query = null, array $tags = [], string $sort = null){ public static function FilterEbooks(string $query = null, array $tags = [], string $sort = null){
@ -24,7 +24,7 @@ class Library{
$matches = $ebooks; $matches = $ebooks;
if($sort === null){ if($sort === null){
$sort = SORT_NEWEST; $sort = EbookSort::Newest->value;
} }
if(sizeof($tags) > 0 && !in_array('all', $tags)){ // 0 tags means "all ebooks" if(sizeof($tags) > 0 && !in_array('all', $tags)){ // 0 tags means "all ebooks"
@ -51,13 +51,13 @@ class Library{
} }
switch($sort){ switch($sort){
case SORT_AUTHOR_ALPHA: case EbookSort::AuthorAlpha->value:
usort($matches, function($a, $b){ usort($matches, function($a, $b){
return strcmp(mb_strtolower($a->Authors[0]->SortName), mb_strtolower($b->Authors[0]->SortName)); return strcmp(mb_strtolower($a->Authors[0]->SortName), mb_strtolower($b->Authors[0]->SortName));
}); });
break; break;
case SORT_NEWEST: case EbookSort::Newest->value:
usort($matches, function($a, $b){ usort($matches, function($a, $b){
if($a->Created < $b->Created){ if($a->Created < $b->Created){
return -1; return -1;
@ -73,7 +73,7 @@ class Library{
$matches = array_reverse($matches); $matches = array_reverse($matches);
break; break;
case SORT_READING_EASE: case EbookSort::ReadingEase->value:
usort($matches, function($a, $b){ usort($matches, function($a, $b){
if($a->ReadingEase < $b->ReadingEase){ if($a->ReadingEase < $b->ReadingEase){
return -1; return -1;
@ -89,7 +89,7 @@ class Library{
$matches = array_reverse($matches); $matches = array_reverse($matches);
break; break;
case SORT_LENGTH: case EbookSort::Length->value:
usort($matches, function($a, $b){ usort($matches, function($a, $b){
if($a->WordCount < $b->WordCount){ if($a->WordCount < $b->WordCount){
return -1; return -1;

View file

@ -17,10 +17,10 @@ $allSelected = sizeof($tags) == 0 || in_array('all', $tags);
<span>Sort</span> <span>Sort</span>
<span> <span>
<select name="sort"> <select name="sort">
<option value="<?= SORT_NEWEST ?>"<? if($sort == SORT_NEWEST){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option> <option value="<?= EbookSort::Newest->value ?>"<? if($sort == EbookSort::Newest->value){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option>
<option value="<?= SORT_AUTHOR_ALPHA ?>"<? if($sort == SORT_AUTHOR_ALPHA){ ?> selected="selected"<? } ?>>Author name (a &#x2192; z)</option> <option value="<?= EbookSort::AuthorAlpha->value ?>"<? if($sort == EbookSort::AuthorAlpha->value){ ?> selected="selected"<? } ?>>Author name (a &#x2192; z)</option>
<option value="<?= SORT_READING_EASE ?>"<? if($sort == SORT_READING_EASE){ ?> selected="selected"<? } ?>>Reading ease (easy &#x2192; hard)</option> <option value="<?= EbookSort::ReadingEase->value ?>"<? if($sort == EbookSort::ReadingEase->value){ ?> selected="selected"<? } ?>>Reading ease (easy &#x2192; hard)</option>
<option value="<?= SORT_LENGTH ?>"<? if($sort == SORT_LENGTH){ ?> selected="selected"<? } ?>>Length (short &#x2192; long)</option> <option value="<?= EbookSort::Length->value ?>"<? if($sort == EbookSort::Length->value){ ?> selected="selected"<? } ?>>Length (short &#x2192; long)</option>
</select> </select>
</span> </span>
</label> </label>