devshed-io / arithmatic
PHP中的链式数学方法
1.5.1
2021-11-10 13:23 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
README
Arithmatic
为PHP提供的链式数学方法。旨在使数学操作更加流畅和易于阅读。
安装(通过Composer)
composer require devshed-io/arithmatic
使用方法
基本使用允许您链式调用方法以提高可读性
Arithmatic ::start(5) ->add(5) ->subtract(7); ->divide(2); ->multiply(7) ->output();
当方法会在方法第一个参数评估为真时执行给定的回调
Arithmatic ::start(10) ->when(true, fn (Arithmatic $calculation) => $calculation->subtract(5))->output();
当也提供了一个方法,当条件评估为假时运行
Arithmatic ::start(10) ->when( false, fn (Arithmatic $calculation) => $calculation->subtract(5), fn (Arithmatic $calculation) => $calculation->add(5), // This will run... ) ->output();
Arithmatic还提供了底层的运行API,因此您可以使用数组链式调用方法
Arithmatic ::make(10) ->run([ 'add' => 5, 'divide' => 2, 'round' ]) ->output(); // 3
注意:除非您调用 output
,否则arithmatic将提供一个自身的实例。可以使用 __toString()
或 (string)
方法将其转换为字符串。
可用方法
- percentageOf
- percentageChange
- divide
- subtract
- add
- multiply
- mean
- round
- clamp
- clone
测试(使用Docker Compose)
docker-compose run --rm php vendor/bin/phpunit --testdox