2017 © Pedro Peláez
 

library super-number

An objectifier for primitive numeric data types.

image

degecko/super-number

An objectifier for primitive numeric data types.

  • Monday, March 26, 2018
  • by degecko
  • Repository
  • 1 Watchers
  • 3 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

SuperNumber

SuperNumber is a number objectifier. Its purpose is to keep simple math OOP friendly. If you're looking for a way to clean up your views, this will do the job., (*1)

To use it, you can use it via composer:, (*2)

composer require degecko/super-number

Then, simply initialize it on any number:, (*3)

$number = new DeGecko\SuperNumber(100);

If you need to initialize it multiple times, I recommend creating a helper, like the following:, (*4)

function number ($n) {
    return new DeGecko\SuperNumber($n);
}

Available Methods

Simple Math

  • add() / plus() to perform addition
  • subtract() / sub() / minus() to perform subtraction
  • multiply() / times() to perform multiplication
  • divide() / over() to perform division

All of the above support 2 parameters. The first is the value that you want to use with the current number, and the second parametere specifies if that value should be treated as a percentage., (*5)

E.g., (*6)

number(50)->add(10); // Returns 60
number(50)->add(10, true); // Returns 55, because 50 + 10% of 50 = 55

Of course, you can chain them together:, (*7)

number(10)->add(3)->divide(2); // Returns 6.5

Apart from those, there are other basic methods:, (*8)

  • increment() / decrement() which are aliases of add(1) and subtract(1). You can specify a different value by passing it as the first parameter.
  • power() / pow() to raise to a power
  • modulo() / mod() to perform modulo

E.g., (*9)

number(5)->increment(); // Returns 6
number(5)->decrement(2); // Returns 3
number(10)->increment(10, true); // Returns 11, because 10% of 10 is 1, and 10 + 1 = 11
number(2)->power(3); // Returns 8
number(10)->mod(5); // Returns 0

Formulas

  • percentageOf() / percentOf() computes the percentage of the current number from the given value.
  • percentageFrom() / percentFrom() computes what percentage of the given number represents the current number.

E.g., (*10)

number(40)->percentOf(200); // Returns 20, because 40 is 20% of 200.
number(40)->percentFrom(200); // Returns 80, because 40% of 200 is 80.

Mutators

  • mutate(callable $fn) will let you alter the currently stored number any way you want, by passing it a callable function. The first parameter of the function is the current $value. Whatever you return, will become the new number.

Warning! This does not validate your new value to be numeric., (*11)

E.g., (*12)

number(3.1)->mutate(function ($value) {
    return (int) $value;
}); // Returns 3

Output

The magic method __toString() will return (string) $this->number;, so there's no need to call another method to get the output of a current SuperNumber object. However, there are additional methods to get the output, if required., (*13)

  • get() which returns the current number, uncasted.
  • printf($pattern, ...$arguments) which allows you to use sprintf() on the current number. It automatically adds the second sprintf parameter to be the current value.
  • format($decimals = 0, $decPoint = '.', $thousandsSep = ',') which is an alias of number_format. It supports the same parameters as number_format.
number(10)->printf('%d'); // Returns 10
number(10)->printf('$%.2f'); // Returns $10.00

number(1000)->format(); // Returns 1,000
number(1000)->format(2); // Returns 1,000.00
number(1000)->format(2, '_'); // Returns 1,000_00
number(1000)->format(2, '_', '-'); // Returns 1-000_00

Castings

  • toInt() will cast the current value to an integer
  • toFloat() will cast the current value to a float

Checks

  • isOdd() checks if the integer version of the current number is odd.
  • isEven() checks if the integer version of the current number is even.
  • eq() / equal() / equals() checks if the current value is equal to the provided value.
  • lt() / lessThan() checks if the current value is less than the provided value.
  • lte() / lessThanOrEqual() checks if the current value is less than or equal to the provided value.
  • gt() / greaterThan() checks if the current value is greater than the provided value.
  • gte() / greaterThanOrEqual() checks if the current value is greater than or equal to the provided value.

And, also all of the PHP math functions are supported and can be chained together:, (*14)

  • abs()
  • acos()
  • acosh()
  • asin()
  • asinh()
  • atan()
  • atanh()
  • ceil()
  • cos()
  • cosh()
  • deg2rad()
  • exp()
  • expm1()
  • floor()
  • fmod()
  • log10()
  • log1p()
  • log()
  • min()
  • max()
  • rad2deg()
  • round()
  • sin()
  • sinh()
  • sqrt()
  • tan()
  • tanh()
  • base_convert()
  • bindec()
  • decbin()
  • dechex()
  • decoct()
  • hexdec()
  • octdec()
  • pow()

E.g., (*15)

number(-9)->abs()->pow(2); // Returns 81 

You can find those defined in the official PHP documentation., (*16)

Author

Cosmin Gheorghita (degecko.com), (*17)

The Versions

26/03 2018

dev-master

9999999-dev

An objectifier for primitive numeric data types.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Cosmin Gheorghita

number numeric objectifier

22/03 2018

1.1.1

1.1.1.0

An objectifier for primitive numeric data types.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Cosmin Gheorghita

number numeric objectifier

20/03 2018

1.1.0

1.1.0.0

An objectifier for primitive numeric data types.

  Sources   Download

MIT

The Requires

 

by Cosmin Gheorghita

number numeric objectifier

19/03 2018

1.0.0

1.0.0.0

An objectifier for primitive numeric data types.

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

by Cosmin Gheorghita