Replace TextSinglePageSizeNumber and TextSinglePageSizeUnit with TextSinglePageSizeFormatted

This commit is contained in:
Mike Colagrosso 2024-05-29 15:35:26 -06:00 committed by Alex Cabal
parent 1589dda35c
commit b176a68d4d
2 changed files with 19 additions and 24 deletions

View file

@ -41,8 +41,7 @@ use function Safe\shell_exec;
* @property string $TitleWithCreditsHtml * @property string $TitleWithCreditsHtml
* @property string $TextUrl * @property string $TextUrl
* @property string $TextSinglePageUrl * @property string $TextSinglePageUrl
* @property string $TextSinglePageSizeNumber * @property string $TextSinglePageSizeFormatted
* @property string $TextSinglePageSizeUnit
* @property string $IndexableText * @property string $IndexableText
*/ */
class Ebook{ class Ebook{
@ -110,8 +109,7 @@ class Ebook{
protected ?string $_TitleWithCreditsHtml = null; protected ?string $_TitleWithCreditsHtml = null;
protected ?string $_TextUrl = null; protected ?string $_TextUrl = null;
protected ?string $_TextSinglePageUrl = null; protected ?string $_TextSinglePageUrl = null;
protected ?string $_TextSinglePageSizeNumber = null; protected ?string $_TextSinglePageSizeFormatted = null;
protected ?string $_TextSinglePageSizeUnit = null;
protected ?string $_IndexableText = null; protected ?string $_IndexableText = null;
// ******* // *******
@ -523,29 +521,26 @@ class Ebook{
return $this->_TextSinglePageUrl; return $this->_TextSinglePageUrl;
} }
protected function GetTextSinglePageSizeNumber(): string{ protected function GetTextSinglePageSizeFormatted(): string{
if($this->_TextSinglePageSizeNumber === null){ if($this->_TextSinglePageSizeFormatted === null){
$sizes = 'BKMGTP'; $bytes = $this->TextSinglePageByteCount;
$factor = intval(floor((strlen((string)$this->TextSinglePageByteCount) - 1) / 3)); $sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
try{
$this->_TextSinglePageSizeNumber = sprintf('%.1f', $this->TextSinglePageByteCount / pow(1024, $factor)); $index = 0;
while($bytes >= 1024 && $index < count($sizes) - 1){
$bytes /= 1024;
$index++;
} }
catch(\DivisionByZeroError){
$this->_TextSinglePageSizeNumber = '0'; if($index == 0){
// No decimal point for smaller than a KB.
$this->_TextSinglePageSizeFormatted = sprintf("%d %s", $bytes, $sizes[$index]);
}else{
$this->_TextSinglePageSizeFormatted = sprintf("%.1f %s", $bytes, $sizes[$index]);
} }
} }
return $this->_TextSinglePageSizeNumber; return $this->_TextSinglePageSizeFormatted;
}
protected function GetTextSinglePageSizeUnit(): string{
if($this->_TextSinglePageSizeUnit === null){
$sizes = 'BKMGTP';
$factor = intval(floor((strlen((string)$this->TextSinglePageByteCount) - 1) / 3));
$this->_TextSinglePageSizeUnit = $sizes[$factor] ?? '';
}
return $this->_TextSinglePageSizeUnit;
} }
protected function GetIndexableText(): string{ protected function GetIndexableText(): string{

View file

@ -289,7 +289,7 @@ catch(Exceptions\EbookNotFoundException){
<meta property="schema:description" content="XHTML"/> <meta property="schema:description" content="XHTML"/>
<meta property="schema:encodingFormat" content="application/xhtml+xml"/> <meta property="schema:encodingFormat" content="application/xhtml+xml"/>
<p<? if($ebook->TextSinglePageByteCount >= EBOOK_SINGLE_PAGE_SIZE_WARNING){ ?> class="has-size"<? } ?>> <p<? if($ebook->TextSinglePageByteCount >= EBOOK_SINGLE_PAGE_SIZE_WARNING){ ?> class="has-size"<? } ?>>
<a property="schema:contentUrl" href="<?= $ebook->TextSinglePageUrl ?>" class="page">Read on one page</a><? if($ebook->TextSinglePageByteCount >= EBOOK_SINGLE_PAGE_SIZE_WARNING){ ?><span><?= $ebook->TextSinglePageSizeNumber ?>MB</span><? } ?> <a property="schema:contentUrl" href="<?= $ebook->TextSinglePageUrl ?>" class="page">Read on one page</a><? if($ebook->TextSinglePageByteCount >= EBOOK_SINGLE_PAGE_SIZE_WARNING){ ?><span><?= $ebook->TextSinglePageSizeFormatted ?></span><? } ?>
</p> </p>
</li> </li>
<? } ?> <? } ?>