Helpers

Codacy Badge StyleCI License Total Downloads Latest Stable Version

Helper classes dependency for Laravel Enso.

This package can work independently of the Enso ecosystem.

For live examples and demos, you may visit laravel-enso.com

Installation

Comes pre-installed in Enso.

To install outside of Enso: composer require laravel-enso/helpers

Usage

The following classes, exceptions and traits are available.

Classes

  • An Decimals class which is a wrapper for PHP's bc* methods such as bcadd with support for a customizable, default precision
  • A JsonParser class that takes a JSON file as its constructor's argument, and can parse and transform the file to:
    • object
    • array
    • JSON string
  • An Obj class, with a constructor for building an object from an array, an object, a Laravel model that can even have loaded relationships and more. It provides a suite of helper functions, such as:
    • all(),
    • __toString(),
    • toJson(),
    • toArray(),
    • get($key),
    • set($key, $value),
    • has($key),
    • keys(),
    • values()

Decimals - LaravelEnso\Helpers\app\Classes\Decimals

All the class' methods are static.

Methods:

  • scale($precision), sets the default precision. If not specified, the precision will be 2
  • add($first, $second, $precision = null), calls bcadd with the given/default precision
  • sub($first, $second, $precision = null), calls bcsub with the given/default precision
  • mul($first, $second, $precision = null), calls bcmul with the given/default precision
  • div($first, $second, $precision = null), calls bcdiv with the given/default precision
  • sqrt($first, $second, $precision = null), calls bcsqrt with the given/default precision
  • pow($first, $second, $precision = null), calls bcpow with the given/default precision
  • mod($first, $second, $precision = null), calls bcmod with the given/default precision
  • powmod($first, $second, $precision = null), calls bcpowmod with the given/default precision
  • lt($first, $second, $precision = null), uses bccomp with the given/default precision, returns the boolean result of a less than comparison
  • lte($first, $second, $precision = null), uses bccomp with the given/default precision, returns the boolean result of a less or equals than comparison
  • eq($first, $second, $precision = null), uses bccomp with the given/default precision, returns the boolean result of an equals comparison
  • notEq($first, $second, $precision = null), uses bccomp with the given/default precision, returns the boolean result of not equals comparison
  • gt($first, $second, $precision = null), uses bccomp with the given/default precision, returns the boolean result of a greater than comparison
  • gte($first, $second, $precision = null), uses bccomp with the given/default precision, returns the boolean result of a greater than or equals comparison
  • ceil($first, $second, $precision = null), uses bcceil with the given/default precision, returns the boolean result of a less than comparison
  • floor($first, $second, $precision = null), uses floor with the given/default precision, returns the boolean result of a less than comparison

JsonParser - LaravelEnso\Helpers\app\Classes\JsonParser

The constructor takes a file name. This must be a text file with valid json content. Note: When trying to read it, a JsonParseException exception will be thrown if the file contents is not valid.

Methods:

  • object(), returns an object representation of the file
  • array(), returns an array representation of the file
  • json, returns an json representation of the file

Obj - LaravelEnso\Helpers\app\Classes\Obj

The constructor optionally takes an associate array or an object (even a Laravel model). This parameter is used to set up the object. For arrays, it uses the array keys as properties and the array values as the property values.

Methods:

  • all, __toString(), returns the string representation of the object
  • toJson, gives back the json representation of the object. Note that the String representation also gives back the same
  • toArray, same as all, gives back the object's attributes and values as an associative array
  • get(key), returns the value of the object's 'key' property. Return 'null' if the key does not exist
  • set(key, value), sets the value of the object's 'key' property
  • has(key), returns true if the 'key' property exists on the object
  • filled(key), returns true if the 'key' property exists on the object and its value is not null
  • forget(key), unsets the 'key' property
  • keys, gives back as array the names of the object's properties
  • values, gives back as array the values of the object's properties
  • isEmpty, returns true if the object does not have any properties
  • isNotEmpty, returns true if the object has any property
  • count, returns the number of the object's properties

Exceptions

  • A generic exception: EnsoException is available also with a Facade. This exception is extended by all the other Enso specific exceptions and it is not reported by the Laravel's Exception Handler.

  • A FileMissingException, a child of EnsoException

  • A JsonParseException, a child of EnsoException

  • A MorphableConfigException, a child of EnsoException

These exceptions are used throughout various Enso packages.

Traits

ActiveState

Adds the following methods for models that have a boolean is_active property:

  • whereActive() scope
  • whereDisabled() scope
  • isActive() helper
  • isDisabled() helper

Contributions

are welcome. Pull requests are great, but issues are good too.

License

This package is released under the MIT license.