Download the PHP package brekitomasson/laravel-support-helpers without Composer
On this page you can find all versions of the php package brekitomasson/laravel-support-helpers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brekitomasson/laravel-support-helpers
More information about brekitomasson/laravel-support-helpers
Files in brekitomasson/laravel-support-helpers
Package laravel-support-helpers
Short Description Helper methods in the spirit of Laravel's Arr and Str.
License MIT
Informations about the package laravel-support-helpers
laravel-support-helpers
Helper methods in the spirit of Laravel's native Arr
and Str
. Also offers a fluid num($input)
helper method that
allows you to manipulate numeric inputs with ease.
Installation
Usage
This library can be used in three ways, all offering more or less the same features and functionality. They are:
- Instantiating the
Number
class with an input and running methods on the variable. - Using the
num()
helper method. - Calling static methods on the
Num
class.
Note that the second option, the fluid helper method, is merely a shortcut to the first option, and the two are
functionally interchangeable. The input is parsed and cleaned - if given as a string, the input is reduced to only
the numeric values detected in the string. Decimal points must be stated with periods, as commas in the input
string are filtered out. The string '159,99'
will be parsed as the whole number 15999
.
The static methods are used internally by the library but can be called directly for simple one-off methods:
Available Methods
Note that most of these methods return an instance of
Number
and can be chained one after another. To get the current result, use the->toNumber()
method. The current value will be returned as afloat
orint
based on whether it is a whole number or not.
add()
Increases the current value by the given input.
decimal()
Changes the current value to only the decimal portion of the current value. If the current value is a whole number,
replaces the current value with 0
. See integer()
for the corresponding method.
factors()
Returns an array containing the factors of the current value (except 1 and the value itself). Requires the current value to be a whole number, as factors cannot be calculated from values with decimal numbers.
greaterThan()
Returns true
or false
depending on if the current value is greater than the given input. See lessThan()
for
the corresponding method.
inRange()
Returns true
or false
depending on if the current value is within a range defined by the two given inputs.
Accepts a boolean as a third argument (defaults to true
) to determine whether to allow exact matches with the min
and max values in the range.
integer()
Changes the current value to only the integer portion of the current value. If the current value is a whole number,
no change is made. See decimal()
for the corresponding method. Note that this does not round the current value
to the nearest whole number.
lessThan()
Returns true
or false
depending on if the current value is less than the given input. See greaterThan()
for the
corresponding method.
over()
Divides the current value by the given input.
percentOf()
Changes the current value to how many percent of the given input it is.
roundToPart()
Rounds the current value to the given input's "parts of one". If the given input is 4
, the current value is
rounded to the nearest 1/4
, or .25
. If the given input is 10
, the current value is rounded to the nearest
tenth. ->roundToPart(1)
is functionally equivalent to rounding to the nearest whole number.
sub()
Reduces the current value by the given input.
times()
Multiplies the current value by the given input.
withinRange()
Returns true
or false
depending on if the current value is within a given range of a given input. The first
argument is the base value and the second argument is the range away from the base value to test.
Accepts a boolean as a third argument (defaults to true
) to determine whether to allow exact matches with the min
and max values in the range.
TODO
The following is a list of things that are missing or incomplete, and which will be added to the library over the next couple of patches/versions. None of these changes are expected to break backwards compatibility.
PRs are always welcome! If you add a method, please update the documentation and the test suite accordingly.
- Aliases for some less obviously named methods.
dividedBy()
forover()
get()
fortoNumber()
gt()
forgreaterThan()
lt()
forlessThan()
minus()
forsub()
multipliedBy()
fortimes()
plus()
foradd()
format()
method to permit returning the value in a given format.isOdd()
,isEven()
methods.isWhole()
toInt()
andtoFloat()
to force cast the current value toint
orfloat
.round()
method to return the value to a given number of decimal places.- Should return the whole number if called without arguments.
- More mathematical functions.
- See how https://github.com/degecko/super-number/blob/master/src/SuperNumber.php uses
__call()
to reference some native PHP methods. Something similar could be done here.
- See how https://github.com/degecko/super-number/blob/master/src/SuperNumber.php uses
- "percent from" equivalent of the
percentOf()
method.