Download the PHP package madison-solutions/coerce without Composer

On this page you can find all versions of the php package madison-solutions/coerce. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package coerce

Coerce

Functions for coercing values to various data types

This library contains functions which takes an input value of any type, and attempt to 'coerce' it into an output value of a particular type, in a way that people would expect (following the principle of least astonishment). If the conversion is not possible, or if there is any ambiguity about how the conversion should be done, then the coercion will fail.

For example, the floating point number 2.0, can be coerced to the integer 2, because 2.0 is just another way of expressing the number 2. Whereas attempting to coerce the floating point number 2.1 to an integer will fail. In particular we don't try to guess whether some kind of rounding should be attempted, or which direction to round. The input 2.1 does not represent an integer and that's that.

There are 4 functions for each target data type, which differ in handling of null inputs and the way that failed coercions are handled.

Version 1

Coerce::toString($input, &$output): bool
Coerce::toInt($input, &$output): bool
Coerce::toFloat($input, &$output): bool
Coerce::toArrayKey($input, &$output): bool
Coerce::toBool($input, &$output): bool

In this version, the return value is a boolean indicating whether or not the coercion was successful (with the coerced value being stored in the $output parameter.

This version will be more useful when the input data comes from an untrusted source and therefore failed coercions are expected, and the code should always handle the failure gracefully.

The first parameter $input is passed by value, and contains the value (of any type) which will be coerced. The second parameter $output is passed by reference - if coercion is possible, the result of the coercion will be populated into $output. Depending on the target type, other additional parameters may be accepted - see notes on specific functions below.

The return value of the function is a boolean, true if the coercion was possible, false otherwise. If the function returns false then the value of $output should be considered invalid and ignored.

Version 2

Coerce::toStringOrFail($input): string
Coerce::toIntOrFail($input): int
Coerce::toFloatOrFail($input): float
Coerce::toArrayKeyOrFail($input): int|string
Coerce::toBoolOrFail($input): bool

In this version, the coerced value is the return value from the function, and if coercion failed then an InvalidArgumentException will be thrown.

This version will be more useful when the input data comes from a trusted source, and therefore the coercion should normally be expected to succeed, with failure being an exceptional event which may indicate a bug elsewhere in the code.

Version 3

Coerce::toStringOrNull($input, &$output): bool
Coerce::toIntOrNull($input, &$output): bool
Coerce::toFloatOrNull($input, &$output): bool
Coerce::toArrayKeyOrNull($input, &$output): bool
Coerce::toBoolOrNull($input, &$output): bool

This version is the same as version 1, except that null values are also accepted. So if the input is null or an empty string, the output will also be null, and the function will return true.

Version 4

Coerce::toStringOrNullOrFail($input): string|null
Coerce::toIntOrNullOrFail($input): int|null
Coerce::toFloatOrNullOrFail($input): float|null
Coerce::toArrayKeyOrNullOrFail($input): int|string|null
Coerce::toBoolOrNullOrFail($input): bool|null

This version is the same as version 3, except that null values are also accepted. So if the input is null or an empty string, the function will return null.

Examples

Form validation, where user input in $_POST cannot be trusted

Boolean value which has to be stored in a string database field

Functions

Coerce::toString($input, &$output, bool $reject_bool = false)

Coerce a value to a string.

Coerce::toInt($input, &$output, bool $reject_bool = false, bool $round_floats = false, bool $reject_negative = false, bool $reject_zero = false)

Coerce a value to an integer.

Coerce::toFloat($input, &$output, bool $reject_bool = false)

Coerce a value to a float.

Coerce::toArrayKey($input, &$output)

Coerce a value to type suitable for use as a key in a PHP array (string or int).

Coerce::toBool($input, &$output)

Coerce a value to a boolean.


All versions of coerce with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package madison-solutions/coerce contains the following files

Loading the files please wait ....