Skip to content

Commit

Permalink
cc
Browse files Browse the repository at this point in the history
  • Loading branch information
arusinowski committed Mar 1, 2024
1 parent 0ff1968 commit 70d4bf7
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 86 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.bat]
end_of_line = crlf

[*.yml]
indent_size = 2

[*.xml]
indent_size = 2

[Makefile]
indent_style = tab

[*.neon]
indent_style = tab
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/.idea/
/.phpunit.cache
/.ddev/
/tools/
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"moneyphp/money": "^3.3"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.3"
"phpunit/phpunit": "^8.5 || ^9.3",
"cakephp/cakephp-codesniffer": "^4.5",
"ext-pdo": "*"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -40,5 +42,10 @@
"psalm-baseline": "tools/psalm --set-baseline=psalm-baseline.xml",
"stan-setup": "phive install",
"test": "phpunit"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false
}
}
}
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<ruleset name="CakeDC/Money">
<config name="installed_paths" value="../../cakephp/cakephp-codesniffer" />

<rule ref="CakePHP" />
</ruleset>
22 changes: 8 additions & 14 deletions src/Database/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@
namespace CakeDC\Money\Database\Type;

use Cake\Core\Configure;

use Cake\Database\DriverInterface;
use Cake\Database\Type\BaseType;
use Cake\Database\TypeInterface;
use Cake\Error\Debugger;
use CakeDC\Money\Utility\MoneyUtil;
use CakeDC\Money\Money;
use http\Exception\RuntimeException;
use Money\Money as MoneyPHP;
use CakeDC\Money\Utility\MoneyUtil;
use PDO;

/**
Expand All @@ -29,15 +25,14 @@
*/
class MoneyType extends BaseType implements TypeInterface
{

/**
* Casts given value from a database type to a PHP equivalent.
*
* @param mixed $value Value to be converted to PHP equivalent
* @param \Cake\Database\DriverInterface $driver Object from which database preferences and configuration will be extracted
* @return ?Money Given value casted from a database to a PHP equivalent.
* @return ?\CakeDC\Money\Money Given value casted from a database to a PHP equivalent.
*/
public function toPHP($value, DriverInterface $driver) : ?Money
public function toPHP($value, DriverInterface $driver): ?Money
{
if ($value === null) {
return null;
Expand All @@ -55,9 +50,9 @@ public function toPHP($value, DriverInterface $driver) : ?Money
* that make sense for the rest of the ORM/Database layers.
*
* @param mixed $value The value to convert.
* @return ?Money Converted value.
* @return ?\CakeDC\Money\Money Converted value.
*/
public function marshal($value) : ?Money
public function marshal($value): ?Money
{
if ($value === null) {
return null;
Expand All @@ -70,11 +65,11 @@ public function marshal($value) : ?Money
/**
* Casts given value from a PHP type to one acceptable by a database.
*
* @param ?Money $value Value to be converted to a database equivalent.
* @param ?\CakeDC\Money\Money $value Value to be converted to a database equivalent.
* @param \Cake\Database\DriverInterface $driver Object from which database preferences and configuration will be extracted.
* @return ?string Given PHP type casted to one acceptable by a database.
*/
public function toDatabase($value, DriverInterface $driver) : ?string
public function toDatabase($value, DriverInterface $driver): ?string
{

if ($value === null) {
Expand All @@ -94,13 +89,12 @@ public function toDatabase($value, DriverInterface $driver) : ?string
* @param \Cake\Database\DriverInterface $driver Object from which database preferences and configuration will be extracted.
* @return int Given value casted to its Statement equivalent.
*/
public function toStatement($value, DriverInterface $driver) : int
public function toStatement($value, DriverInterface $driver): int
{
if ($value === null) {
return PDO::PARAM_NULL;
}

return PDO::PARAM_INT;
}

}
24 changes: 12 additions & 12 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @method \CakeDC\Money\Money mod(\CakeDC\Money\Money $divisor) See \Money\Money::mod()
* @method \CakeDC\Money\Money[] allocate(array $ratios) See \Money\Money::allocate()
* @method \CakeDC\Money\Money[] allocateTo($n) See \Money\Money::allocateTo()
* @method string ratioOf(Money $money) See \Money\Money::ratioOf()
* @method string ratioOf(\CakeDC\Money\Money $money) See \Money\Money::ratioOf()
* @method \CakeDC\Money\Money absolute() See \Money\Money::absolute()
* @method \CakeDC\Money\Money negative() See \Money\Money::negative()
* @method bool isZero() See \Money\Money::isZero()
Expand All @@ -52,29 +52,29 @@
*/
class Money
{
const ROUND_HALF_UP = PHP_ROUND_HALF_UP;
public const ROUND_HALF_UP = PHP_ROUND_HALF_UP;

const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN;
public const ROUND_HALF_DOWN = PHP_ROUND_HALF_DOWN;

const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN;
public const ROUND_HALF_EVEN = PHP_ROUND_HALF_EVEN;

const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD;
public const ROUND_HALF_ODD = PHP_ROUND_HALF_ODD;

const ROUND_UP = 5;
public const ROUND_UP = 5;

const ROUND_DOWN = 6;
public const ROUND_DOWN = 6;

const ROUND_HALF_POSITIVE_INFINITY = 7;
public const ROUND_HALF_POSITIVE_INFINITY = 7;

const ROUND_HALF_NEGATIVE_INFINITY = 8;
public const ROUND_HALF_NEGATIVE_INFINITY = 8;

/**
* @var MoneyPHP
* @var \Money\Money
*/
protected $_money;

/**
* @return MoneyPHP
* @return \Money\Money
*/
public function getMoney(): MoneyPHP
{
Expand Down Expand Up @@ -130,7 +130,7 @@ public function __toString(): string
*/
protected static function processArguments($arguments = [])
{
for ($i=0; $i < count($arguments); $i++) {
for ($i = 0; $i < count($arguments); $i++) {
if ($arguments[$i] instanceof Money) {
$arguments[$i] = $arguments[$i]->getMoney();
}
Expand Down
56 changes: 28 additions & 28 deletions src/Utility/MoneyUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
namespace CakeDC\Money\Utility;

use Cake\Core\Configure;
use CakeDC\Money\Money;
use Money\Currencies\BitcoinCurrencies;
use Money\Currencies\ISOCurrencies;
use Money\Currency;
use Money\Formatter\BitcoinMoneyFormatter;
use Money\Formatter\IntlMoneyFormatter;
use CakeDC\Money\Money;
use Money\MoneyFormatter;

/**
Expand All @@ -37,10 +37,10 @@ class MoneyUtil
* Returns a new object of type Money
*
* @param \CakeDC\Money\Money|int|float|string $value
* @param boolean $fromDb
* @return Money
* @param bool $fromDb
* @return \CakeDC\Money\Money
*/
public static function money($value, bool $fromDb = false) : ?Money
public static function money($value, bool $fromDb = false): ?Money
{
if (!is_numeric($value) && empty($value)) {
return null;
Expand All @@ -63,28 +63,28 @@ public static function money($value, bool $fromDb = false) : ?Money

$value = ltrim($parts[0] . $parts[1], '0');
}

$currency = Configure::read('Money.currency', 'USD');

return Money::{$currency}(!empty($value) ? str_replace(',', '', (string)$value) : 0);
}

/**
* @param Money $money
* @param \CakeDC\Money\Money $money
* @return float
*/
public static function float(Money $money) : float
public static function float(Money $money): float
{
return ((float)$money->getAmount()) / 100;
return (float)$money->getAmount() / 100;
}

/**
* @param Money $value
* @param \CakeDC\Money\Money $value
* @return string
*/
public static function format(Money $value) : string
public static function format(Money $value): string
{
/** @var Currency $currency */
/** @var \Money\Currency $currency */
$currency = $value->getCurrency();

return static::_loadMoneyFormatter($currency)->format($value->getMoney());
Expand All @@ -93,10 +93,10 @@ public static function format(Money $value) : string
/**
* Loads proper money formatter or returns if it is already loaded
*
* @param Currency $currency
* @return MoneyFormatter
* @param \Money\Currency $currency
* @return \Money\MoneyFormatter
*/
protected static function _loadMoneyFormatter(Currency $currency) : MoneyFormatter
protected static function _loadMoneyFormatter(Currency $currency): MoneyFormatter
{
if (isset(static::$_moneyFormatters[$currency->getCode()])) {
return static::$_moneyFormatters[$currency->getCode()];
Expand All @@ -112,73 +112,73 @@ protected static function _loadMoneyFormatter(Currency $currency) : MoneyFormatt
throw new \RuntimeException(sprintf('Cannot format currency \'%s\'. Only ISO currencies and Bitcoin are allowed.', $currency));
}
static::$_moneyFormatters[$currency->getCode()] = $moneyFormatter;

return static::$_moneyFormatters[$currency->getCode()];
}

/**
* Returns money object with value 0.00, false otherwise.
*
* @return Money
* @return \CakeDC\Money\Money
*/
public static function zero() : Money
public static function zero(): Money
{
/** @var Money */
/** @var \CakeDC\Money\Money */
return self::money(0);
}

/**
* Returns true if amount value is > 0.00
*
* @param Money $amount
* @param \CakeDC\Money\Money $amount
* @return bool
*/
public static function greaterThanZero(Money $amount) : bool
public static function greaterThanZero(Money $amount): bool
{
return $amount->greaterThan(self::zero());
}

/**
* Returns true if amount value is >= 0.00, false otherwise.
*
* @param Money $amount
* @param \CakeDC\Money\Money $amount
* @return bool
*/
public static function greaterThanOrEqualZero(Money $amount) : bool
public static function greaterThanOrEqualZero(Money $amount): bool
{
return $amount->greaterThanOrEqual(self::zero());
}

/**
* Returns true if amount value is < 0.00, false otherwise.
*
* @param Money $amount
* @param \CakeDC\Money\Money $amount
* @return bool
*/
public static function lessThanZero(Money $amount) : bool
public static function lessThanZero(Money $amount): bool
{
return $amount->lessThan(self::zero());
}

/**
* Returns true if amount value is <= 0.00, false otherwise.
*
* @param Money $amount
* @param \CakeDC\Money\Money $amount
* @return bool
*/
public static function lessThanOrEqualZero(Money $amount) : bool
public static function lessThanOrEqualZero(Money $amount): bool
{
return $amount->lessThanOrEqual(self::zero());
}

/**
* Returns true if amount value is = 0.00, false otherwise.
*
* @param Money $amount
* @param \CakeDC\Money\Money $amount
* @return bool
*/
public static function equalZero(Money $amount) : bool
public static function equalZero(Money $amount): bool
{
return $amount->equals(self::zero());
}

}
8 changes: 5 additions & 3 deletions src/View/Helper/MoneyHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
*/
namespace CakeDC\Money\View\Helper;


use Cake\View\Helper;
use CakeDC\Money\Utility\MoneyUtil;
use CakeDC\Money\Money;
use CakeDC\Money\Utility\MoneyUtil;

/**
* Class MoneyHelper
*
* @package CakeDC\Money\View\Helper
* @property \Cake\View\Helper\HtmlHelper $Html
* @property \Cake\View\Helper\NumberHelper $Number
Expand All @@ -41,6 +41,7 @@ public function initialize(array $config): void

/**
* Format number or money as currency.
*
* @param \CakeDC\Money\Money|float|string $value
* @return string
*/
Expand All @@ -52,7 +53,8 @@ public function currency($value): string
} else {
$output = $this->Number->currency($value);
}
if ((is_numeric($value) && $value < 0) ||
if (
(is_numeric($value) && $value < 0) ||
($value instanceof Money && MoneyUtil::lessThanZero($value))
) {
$class = 'negative-balance';
Expand Down
Loading

0 comments on commit 70d4bf7

Please sign in to comment.