-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
- Loading branch information
Showing
5 changed files
with
187 additions
and
52 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,74 @@ | ||
<?php | ||
|
||
namespace Katsana\Metric; | ||
|
||
abstract class Metric | ||
{ | ||
/** | ||
* Value. | ||
* | ||
* @var float|int | ||
*/ | ||
protected $value; | ||
|
||
/** | ||
* Value format. | ||
* | ||
* @var string | ||
*/ | ||
protected $format; | ||
|
||
/** | ||
* List of supported formats. | ||
* | ||
* @var array | ||
*/ | ||
protected $supportedFormats = []; | ||
|
||
/** | ||
* Format as string. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return \number_format($this->humanize($this->format), 0, '.', ','); | ||
} | ||
|
||
/** | ||
* Convert to new format. | ||
* | ||
* @param string $format | ||
* @return static | ||
*/ | ||
abstract public function to(string $format); | ||
|
||
/** | ||
* Convert to humanize. | ||
* | ||
* @param string $format | ||
* @return float|int | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
abstract public function humanize(string $format); | ||
|
||
/** | ||
* Convert value to. | ||
* | ||
* @param float|int $value | ||
* @param string $format | ||
* @param string $type | ||
* @return float|int | ||
*/ | ||
protected function convertTo($value, string $format, string $type = 'from') | ||
{ | ||
if (! \array_key_exists($format, $this->supportedFormats)) { | ||
throw new InvalidArgumentException("Unvalid use unsupported {$format} format."); | ||
} | ||
|
||
$conversion = $this->supportedFormats[$format]; | ||
|
||
return ($value * $conversion[$type]); | ||
} | ||
} |
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
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