Download the PHP package samsara/newton without Composer
On this page you can find all versions of the php package samsara/newton. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package newton
Newton
This project is unit tested against 5.6.X and 7.X, and merges are not accepted unless the tests pass on both.
Installation
To install, simply require the package using composer:
composer require samsara/newton "1.*"
Or include it in your composer.json
file:
The project namespace is Samsara\Newton\*
. You can view the project on Packagist.
Usage
There are many ways to use this package. The preferred way involves instantiating the UnitComposition class and using it as a factory.
In order to allow new unit classes which extend Quantity to work with the UnitComposition class, it was necessary to force instantiation of it. This is explained in further detail in the Extending section.
This also means that if you directly instantiate a unit, you must inject a UnitComposition instance.
You can also add unit of different types.
The MathProvider has static methods which allow you to perform math operations using the BC Math extension. This is used internally in the project as we might very easily exceed the PHP_INT_MAX limit during unit conversions. It also provides several random functions, including a gaussianRandom() method.
The PhysicsProvider has static methods which implement some common physics equations using the correct unit classes.
An interesting, non-physics use for something like this library is to determine how many times a given server can execute a loop per second. For instance:
Extending
Adding new units is relatively easy. You must first make your unit class, and this class must extend Samsara\Newton\Core\Quantity
. This class must define a set of units in the $units
property (where it defines the index for $rates
), and then define the relative conversion rates between them.
All of the conversions must be in terms of the native unit, which is defined in the property $native
.
Example
Then, in the calling context, you must prepare the UnitComposition class with the definitions of what types of units this custom unit contains. This allows the UnitComposition class to automatically use your custom class in multiply and divide operations when it is appropriate to do so.
Only the instance of UnitComposition prepared in the way outlined above, with a call to addUnit(), will understand how to automatically return an instance of MyUnit(). Because of this, it is suggested that you treat the UnitComposition class as a service, and use a single instance of it within your application.
Contributing
Please ensure that pull requests meet the following guidelines:
- New files created in the pull request must have a corresponding unit test file, or must be covered within an existing test file.
- Your merge may not drop the project's test coverage below 85%.
- Your merge may not drop the project's test coverage by MORE than 5%.
- Your merge must pass Travis-CI build tests for BOTH PHP 5.6.X and PHP 7.X.
For more information, please see the section on Contributing