-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* PES-316: gram support * PES-316: gram support - CR * PES-316: gram support change log update * PES-316: gram support change log update Co-authored-by: Filip Jiskra <filip.jiskra@zasilkovna.cz> Co-authored-by: packeta <57401091+packeta@users.noreply.github.com>
- Loading branch information
1 parent
e32efbb
commit baee70e
Showing
8 changed files
with
75 additions
and
14 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
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,56 @@ | ||
<?php | ||
|
||
namespace Packetery\Weight; | ||
|
||
class Converter | ||
{ | ||
/** @var string */ | ||
const UNIT_KILOGRAMS = 'kg'; | ||
|
||
/** @var string */ | ||
const UNIT_GRAMS = 'g'; | ||
|
||
/** @var array[] */ | ||
private static $mapping = [ | ||
self::UNIT_KILOGRAMS => [ // to kilos | ||
self::UNIT_KILOGRAMS => 1, | ||
self::UNIT_GRAMS => 0.001, // from grams | ||
] | ||
]; | ||
|
||
/** | ||
* @param mixed $value | ||
* @return float|null | ||
*/ | ||
public static function getKilograms($value) | ||
{ | ||
return self::convert($value, \Configuration::get('PS_WEIGHT_UNIT'), self::UNIT_KILOGRAMS); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @param string $unit | ||
* @param string $targetUnit | ||
* @return float|int | ||
*/ | ||
private static function convert($value, $unit, $targetUnit) | ||
{ | ||
$unit = strtolower($unit); | ||
$value = (float)$value; | ||
|
||
if (!isset(self::$mapping[$targetUnit][$unit])) { | ||
return null; | ||
} | ||
|
||
return $value * self::$mapping[$targetUnit][$unit]; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public static function isKgConvertionSupported() | ||
{ | ||
$value = self::getKilograms(1.0); | ||
return $value !== null; | ||
} | ||
} |
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
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