Download the PHP package squirrelphp/types without Composer

On this page you can find all versions of the php package squirrelphp/types. 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 types

Squirrel Types

Explicit handling of type coercions and enforcement in PHP in order to deal with unknown data in a more predictable and safe way.

Installation

composer require squirrelphp/types

Table of contents

Introduction

When getting values from a database or an API (or even just another PHP component you do not know) you have an expectation of what type of value you will get, but you might get something more or less different.

A database might give you the string "1" for a boolean, or the integer 1, and that would often be fine (many databases have no boolean type, so some coercion is often necessary). But getting the string "hello" or the integer -30 should not be fine for a boolean and most likely points to a mistake - maybe the expected type is wrong, or the wrong field in the database was retrieved.

This small component coerces and enforces types and is fully tested to behave in a predictable way. This should often be better than using other coercion methods like explicit casts (bool) / boolval(), as these will coerce any value, while this component will reject unreasonable values and throw a TypeError. It coerces less values than PHP does in coercive typing mode, as PHP accepts quite a few questionable values for legacy/BC reasons.

Coercion behavior overview

All argument flags mentioned below are set to false by default for a more conservative coercion behavior. Enable them only if it is necessary for your use case.

toBool

toInt

toFloat

toString

Test if value can be coerced

All these functions have the mixed $value as their first argument and return true or false:

Coerceable::toBool

Returns true if $value is one of the following:

For any other values it returns false.

Coerceable::toInt

Returns true if $value is one of the following:

For any other values it returns false.

Coerceable::toFloat

Returns true if $value is one of the following:

For any other values it returns false.

Coerceable::toString

Returns true if $value is one of the following:

For any other values it returns false.

Coerceable::stringToBool

Specifically tests a string if it can be coerced to a boolean. $value must be '0' or '1'.

Coerceable::intToBool

Specifically tests an integer if it can be coerced to a boolean. $value must be 0 or 1.

Coerceable::floatToBool

Specifically tests a float if it can be coerced to a boolean. $value must be 0 or 1.

Coerceable::floatToInt

Specifically tests a float if it can be coerced to an integer. $value must not have a fractional part.

Coerceable::stringToInt

Specifically tests a string if it can be coerced to an integer. $value must be a numeric string and not have a fractional part.

Coerceable::stringToFloat

Specifically tests a string if it can be coerced to a float. $value must be a numeric string.

Coerce a value

All these functions have the mixed $value as their first argument and return the type they are coercing (or throw a TypeError).

Coerce::toBool

Returns a boolean if the given value is coerceable, see Coerceable::toBool for valid values, or a TypeError if the value is not coerceable.

Coerce::toInt

Returns an integer if the given value is coerceable, see Coerceable::toInt for valid values, or a TypeError if the value is not coerceable.

Coerce::toFloat

Returns a float if the given value is coerceable, see Coerceable::toFloat for valid values, or a TypeError if the value is not coerceable.

Coerce::toString

Returns a string if the given value is coerceable, see Coerceable::toString for valid values, or a TypeError if the value is not coerceable.

Coerce::stringToBool

Returns a boolean if the given string is coerceable, see Coerceable::stringToBool for valid values, or a TypeError if the value is not coerceable.

Coerce::intToBool

Returns a boolean if the given int is coerceable, see Coerceable::intToBool for valid values, or a TypeError if the value is not coerceable.

Coerce::floatToBool

Returns a boolean if the given float is coerceable, see Coerceable::floatToBool for valid values, or a TypeError if the value is not coerceable.

Coerce::floatToInt

Returns an integer if the given float is coerceable, see Coerceable::floatToInt for valid values, or a TypeError if the value is not coerceable.

Coerce::stringToInt

Returns an integer if the given string is coerceable, see Coerceable::stringToInt for valid values, or a TypeError if the value is not coerceable.

Coerce::stringToFloat

Returns a float if the given string is coerceable, see Coerceable::stringToFloat for valid values, or a TypeError if the value is not coerceable.

Coerce::boolToString

Coerces a boolean to a string, returning either '0' or '1'.

Coerce::intToString

Coerces an integer to a string, returning a numeric string.

Coerce::floatToString

Coerces a float to a string, returning a numeric string.

Enforce a type for a value

All these functions have the mixed $value as their only argument and return the type they are enforcing, according to the same logic as strict mode in PHP.

Enforce::asBool

Returns $value as a boolean if it is a boolean. Throws a TypeError otherwise.

Enforce::asInt

Returns $value as an integer if it is an integer. Throws a TypeError otherwise.

Enforce::asFloat

Returns $value as a float if it is an integer or a float. Throws a TypeError otherwise.

Enforce::asString

Returns $value as a string if it is a string. Throws a TypeError otherwise.


All versions of types with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
squirrelphp/debug Version ^2.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 squirrelphp/types contains the following files

Loading the files please wait ....