Skip to content

Commit

Permalink
Convert ebook SORT_ constants to an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
colagrosso authored and acabal committed Jan 30, 2024
1 parent 94dce10 commit 980ed8c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
- Converting some constants to enums, like `SORT_*` or `SOURCE_*`.
## PHP code style
- Indent with tabs.
Expand Down
4 changes: 0 additions & 4 deletions lib/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@
const DATABASE_DEFAULT_HOST = 'localhost';

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_WIDTH = 350;
Expand Down
7 changes: 7 additions & 0 deletions lib/EbookSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?
enum EbookSort: string{
case Newest = 'newest';
case AuthorAlpha = 'author-alpha';
case ReadingEase = 'reading-ease';
case Length = 'length';
}
12 changes: 6 additions & 6 deletions lib/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class Library{
/**
* @param string $query
* @param array<string> $tags
* @param string $sort
* @param EbookSort $sort
* @return array<Ebook>
*/
public static function FilterEbooks(string $query = null, array $tags = [], string $sort = null){
$ebooks = Library::GetEbooks();
$matches = $ebooks;

if($sort === null){
$sort = SORT_NEWEST;
$sort = EbookSort::Newest->value;
}

if(sizeof($tags) > 0 && !in_array('all', $tags)){ // 0 tags means "all ebooks"
Expand All @@ -51,13 +51,13 @@ public static function FilterEbooks(string $query = null, array $tags = [], stri
}

switch($sort){
case SORT_AUTHOR_ALPHA:
case EbookSort::AuthorAlpha->value:
usort($matches, function($a, $b){
return strcmp(mb_strtolower($a->Authors[0]->SortName), mb_strtolower($b->Authors[0]->SortName));
});
break;

case SORT_NEWEST:
case EbookSort::Newest->value:
usort($matches, function($a, $b){
if($a->Created < $b->Created){
return -1;
Expand All @@ -73,7 +73,7 @@ public static function FilterEbooks(string $query = null, array $tags = [], stri
$matches = array_reverse($matches);
break;

case SORT_READING_EASE:
case EbookSort::ReadingEase->value:
usort($matches, function($a, $b){
if($a->ReadingEase < $b->ReadingEase){
return -1;
Expand All @@ -89,7 +89,7 @@ public static function FilterEbooks(string $query = null, array $tags = [], stri
$matches = array_reverse($matches);
break;

case SORT_LENGTH:
case EbookSort::Length->value:
usort($matches, function($a, $b){
if($a->WordCount < $b->WordCount){
return -1;
Expand Down
8 changes: 4 additions & 4 deletions templates/SearchForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
<span>Sort</span>
<span>
<select name="sort">
<option value="<?= SORT_NEWEST ?>"<? if($sort == SORT_NEWEST){ ?> 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="<?= SORT_READING_EASE ?>"<? if($sort == SORT_READING_EASE){ ?> 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::Newest->value ?>"<? if($sort == EbookSort::Newest->value){ ?> selected="selected"<? } ?>>S.E. release date (new &#x2192; old)</option>
<option value="<?= EbookSort::AuthorAlpha->value ?>"<? if($sort == EbookSort::AuthorAlpha->value){ ?> selected="selected"<? } ?>>Author name (a &#x2192; z)</option>
<option value="<?= EbookSort::ReadingEase->value ?>"<? if($sort == EbookSort::ReadingEase->value){ ?> selected="selected"<? } ?>>Reading ease (easy &#x2192; hard)</option>
<option value="<?= EbookSort::Length->value ?>"<? if($sort == EbookSort::Length->value){ ?> selected="selected"<? } ?>>Length (short &#x2192; long)</option>
</select>
</span>
</label>
Expand Down

0 comments on commit 980ed8c

Please sign in to comment.