diff --git a/Datatypes/Enum.php b/Datatypes/Enum.php index fc0824230..1df47ae2c 100644 --- a/Datatypes/Enum.php +++ b/Datatypes/Enum.php @@ -45,9 +45,9 @@ abstract class Enum */ public static function isValidValue($value) : bool { - $values = array_values(self::getConstants()); + $constants = self::getConstants(); - return in_array($value, $values, true); + return in_array($value, $constants, true); } /** @@ -76,8 +76,9 @@ abstract class Enum public static function getRandom() { $constants = self::getConstants(); + $keys = array_keys($constants); - return $constants[mt_rand(0, count($constants))]; + return $constants[$keys[mt_rand(0, count($constants) - 1)]]; } /** diff --git a/Event/EventManager.php b/Event/EventManager.php index bf94c3adf..a2219b410 100644 --- a/Event/EventManager.php +++ b/Event/EventManager.php @@ -71,10 +71,17 @@ class EventManager implements Mediator /** * {@inheritdoc} */ - public function detach(string $group) + public function detach(string $group) : bool { - unset($this->callbacks[$group]); - unset($this->groups[$group]); + if(isset($this->callbacks[$group])) { + unset($this->callbacks[$group]); + } + + if(isset($this->groups[$group])) { + unset($this->groups[$group]); + } + + return true; } /** diff --git a/Localization/Money.php b/Localization/Money.php index b433c95dc..08a6f0cad 100644 --- a/Localization/Money.php +++ b/Localization/Money.php @@ -15,8 +15,6 @@ */ namespace phpOMS\Localization; -use phpOMS\Math\Number\OperationInterface; - /** * Money class. * @@ -28,7 +26,7 @@ use phpOMS\Math\Number\OperationInterface; * @link http://orange-management.com * @since 1.0.0 */ -class Money implements \Serializable, OperationInterface +class Money implements \Serializable { /** @@ -37,15 +35,7 @@ class Money implements \Serializable, OperationInterface * @var int * @since 1.0.0 */ - const MAX_DECIMALS = 5; - - /** - * Currency symbol. - * - * @var string - * @since 1.0.0 - */ - private $currency = ISO4217CharEnum::C_USD; + const MAX_DECIMALS = 4; /** * Thousands separator. @@ -74,16 +64,16 @@ class Money implements \Serializable, OperationInterface /** * Constructor. * - * @param string $currency Currency symbol + * @param string|int|float $value Value * @param string $thousands Thousands separator * @param string $decimal Decimal separator * * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(string $currency = ISO4217CharEnum::C_USD, string $thousands = ',', string $decimal = '.') + public function __construct($value = 0, string $thousands = ',', string $decimal = '.') { - $this->currency = $currency; + $this->value = is_int($value) ? $value : self::toInt((string) $value); $this->thousands = $thousands; $this->decimal = $decimal; } @@ -93,43 +83,44 @@ class Money implements \Serializable, OperationInterface * * @param string $value Money value * - * @return void + * @return Money * * @since 1.0.0 * @author Dennis Eichhorn */ - public function setString(string $value) + public function setString(string $value) : Money { - $this->value = self::toInt($value, $this->decimal, $this->thousands); + $this->value = self::toInt($value, $this->thousands, $this->decimal); + + return $this; } /** * Money to int. * * @param string $value Money value - * @param string $decimal Decimal character * @param string $thousands Thousands character + * @param string $decimal Decimal character * * @return int * * @since 1.0.0 * @author Dennis Eichhorn */ - public static function toInt(string $value, string $decimal = ',', string $thousands = '.') : int + public static function toInt(string $value, string $thousands = ',', string $decimal = '.') : int { - $split = explode($value, $decimal); - - $left = $split[0]; - $left = str_replace($thousands, '', $left); - + $split = explode($decimal, $value); + $left = $split[0]; + $left = str_replace($thousands, '', $left); $right = ''; + if (count($split) > 1) { $right = $split[1]; } - $right = substr($right, 0, -self::MAX_DECIMALS); + $right = substr($right, 0, self::MAX_DECIMALS); - return (int) $left * 10 * self::MAX_DECIMALS + (int) $right; + return (int) (((int) $left) * 10 ** self::MAX_DECIMALS + (int) str_pad($right, self::MAX_DECIMALS, '0')); } /** @@ -144,16 +135,12 @@ class Money implements \Serializable, OperationInterface */ public function getAmount(int $decimals = 2) : string { - if ($decimals > ($dec = constant('\phpOMS\Localization\ISO4217DecimalEnum::C_' . strtoupper($this->currency)))) { - $decimals = $dec; - } - $value = (string) round($this->value, -self::MAX_DECIMALS + $decimals); $left = substr($value, 0, -self::MAX_DECIMALS); $right = substr($value, -self::MAX_DECIMALS); - return ($decimals > 0) ? number_format($left, 0, $this->thousands, $this->decimal) . $this->decimal . $right : (string) $left; + return ($decimals > 0) ? number_format($left, 0, $this->decimal, $this->thousands) . $this->decimal . substr($right, 0, $decimals) : (string) $left; } /** @@ -169,7 +156,7 @@ class Money implements \Serializable, OperationInterface public function add($value) : Money { if (is_string($value) || is_float($value)) { - $this->value += self::toInt((string) $value, $this->decimal, $this->thousands); + $this->value += self::toInt((string) $value, $this->thousands, $this->decimal); } elseif (is_int($value)) { $this->value += $value; } elseif ($value instanceof Money) { @@ -205,7 +192,7 @@ class Money implements \Serializable, OperationInterface public function sub($value) : Money { if (is_string($value) || is_float($value)) { - $this->value -= self::toInt((string) $value, $this->decimal, $this->thousands); + $this->value -= self::toInt((string) $value, $this->thousands, $this->decimal); } elseif (is_int($value)) { $this->value -= $value; } elseif ($value instanceof Money) { @@ -228,7 +215,7 @@ class Money implements \Serializable, OperationInterface public function mult($value) : Money { if (is_float($value) || is_int($value)) { - $this->value *= $value; + $this->value = (int) ($this->value * $value); } return $this; @@ -247,7 +234,7 @@ class Money implements \Serializable, OperationInterface public function div($value) : Money { if (is_float($value) || is_int($value)) { - $this->value = self::toInt((string) ($this->value / $value), $this->decimal, $this->thousands); + $this->value = (int) ($this->value / $value); } return $this; @@ -320,13 +307,15 @@ class Money implements \Serializable, OperationInterface * * @param int $value Value * - * @return void + * @return Money * * @since 1.0.0 * @author Dennis Eichhorn */ - public function setInt(int $value) + public function setInt(int $value) : Money { $this->value = $value; + + return $this; } } diff --git a/Pattern/Mediator.php b/Pattern/Mediator.php index e011598b0..ab1b65352 100644 --- a/Pattern/Mediator.php +++ b/Pattern/Mediator.php @@ -57,18 +57,6 @@ interface Mediator extends \Countable */ public function detach(string $group) : bool; - /** - * Has outstanding. - * - * @param string $group Group - * - * @return bool - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - private function hasOutstanding(string $group) : bool; - /** * Add group. * diff --git a/Validation/BitcoinValidator.php b/Validation/BitcoinValidator.php index 92101a5bd..f7d9319ab 100644 --- a/Validation/BitcoinValidator.php +++ b/Validation/BitcoinValidator.php @@ -53,7 +53,6 @@ class BitcoinValidator extends ValidatorAbstract return true; } catch (\Exception $e) { self::$msg = $e->getMessage(); - } finally { return false; } } diff --git a/Validation/Validator.php b/Validation/Validator.php index 4067e31d3..ff49c0242 100644 --- a/Validation/Validator.php +++ b/Validation/Validator.php @@ -113,7 +113,7 @@ final class Validator extends ValidatorAbstract */ public static function contains(string $var, $substr) { - return StringUtils::contains($var, $substr); + return is_string($substr) ? strpos($var, $substr) : StringUtils::contains($var, $substr); } /** diff --git a/Views/View.php b/Views/View.php index 9890673ac..daaa6ad86 100644 --- a/Views/View.php +++ b/Views/View.php @@ -365,11 +365,15 @@ class View implements \Serializable { $viewArray = []; - $viewArray[] = $this->render(); + if($this->template === '') { + $viewArray[] = $this->render(); + } foreach ($this->views as $key => $view) { $viewArray[$key] = $view->toArray(); } + + return $viewArray; } /**