Download the PHP package ctw/ctw-cast without Composer

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

Package "ctw/ctw-cast"

Latest Stable Version GitHub Actions

Type-safe, exception-free casting utility for PHP 8.5+ applications.

Introduction

Why This Library Exists

Modern PHP development with strict types (declare(strict_types=1)) demands precise type handling. However, many data sources in PHP return mixed or loosely-typed values:

PHP's native type casting ((int), (string), etc.) is permissive and can silently produce unexpected results:

This library provides predictable, exception-free type conversions. When a value cannot be cast, a safe default is returned instead of throwing an exception, making it ideal for use in hot paths, middleware, and defensive code where unexpected input must never interrupt execution.

Default Return Values

When a value cannot be cast to the target type, the following defaults are returned:

Method Default Return
toArray []
toBool false
toFloat 0.0
toInt 0
toJson '{}'
toString ''

Problems This Library Solves

  1. Silent data corruption: Native casts convert invalid values in unpredictable ways; this library uses clear, documented rules
  2. Inconsistent boolean handling: PHP treats "false", "no", "off" as true; this library treats them as false
  3. Missing validation: No built-in way to reject non-numeric strings for number conversion
  4. Overflow handling: Native (int) silently wraps on overflow; this library returns 0
  5. Type ambiguity: Superglobals and legacy code return mixed, breaking strict typing

Accessing Data from Superglobals

When working with $_GET, $_POST, $_SERVER, $_ENV, and other superglobals, values are always mixed. This library ensures type-safe access:

Working with Legacy Libraries

Many older PHP libraries return untyped data. This library bridges the gap between legacy code and modern strict-typed applications:

Where to Use This Library

Design Goals

  1. Never throw: Every method returns a safe default when a value cannot be cast
  2. Explicit behavior: Every conversion rule is documented and predictable
  3. No silent corruption: Ambiguous values produce the documented default, not a guess
  4. PHPStan/Psalm friendly: Return types are precise, enabling static analysis
  5. Zero dependencies: No external packages required

Requirements

Installation

Install by adding the package as a Composer requirement:

Usage Examples


Cast::toString(mixed $value): string

Converts values to string representation with explicit, predictable rules. Returns '' for values that cannot be cast.

Input Type Input Value Output
string "hello" "hello"
int 42 "42"
int -17 "-17"
int 0 "0"
float 3.14 "3.14"
float -2.5 "-2.5"
float 1.0 "1"
float INF "INF"
float NAN "NAN"
bool true "1"
bool false "0"
null null ""
object with __toString() __toString() result
object without __toString() ""
array [1, 2, 3] ""
resource fopen(...) ""

Cast::toInt(mixed $value): int

Converts values to integers with rounding for floats. Returns 0 for values that cannot be cast.

Input Type Input Value Output
int 42 42
int -17 -17
bool true 1
bool false 0
null null 0
float 3.14 3 (rounded)
float 3.5 4 (rounded)
float -2.7 -3 (rounded)
string "42" 42
string " 42 " 42 (trimmed)
string "3.14" 3 (rounded)
string "1e3" 1000
float INF 0
float NAN 0
float 1e20 (overflow) 0
string "" 0
string "hello" 0
string "42abc" 0
string out of int range 0
array [1, 2, 3] 0
object stdClass 0
resource fopen(...) 0

Cast::toFloat(mixed $value): float

Converts values to floating-point numbers. Returns 0.0 for values that cannot be cast.

Input Type Input Value Output
float 3.14 3.14
float -2.5 -2.5
float INF INF
float NAN NAN
int 42 42.0
int -17 -17.0
int 0 0.0
bool true 1.0
bool false 0.0
null null 0.0
string "3.14" 3.14
string " 3.14 " 3.14 (trimmed)
string "42" 42.0
string "1e3" 1000.0
string "-2.5" -2.5
string "" 0.0
string "hello" 0.0
string "42abc" 0.0
array [1, 2, 3] 0.0
object stdClass 0.0
resource fopen(...) 0.0

Cast::toBool(mixed $value): bool

Strict boolean conversion with explicit allowed values. Returns false for values that cannot be interpreted unambiguously.

Input Type Input Value Output
bool true true
bool false false
int 1 true
int 0 false
float 1.0 true
float 0.0 false
null null false
string "true" true
string "1" true
string "yes" true
string "on" true
string "y" true
string "t" true
string "false" false
string "0" false
string "no" false
string "off" false
string "n" false
string "f" false
string "" false
string " TRUE " true (trimmed, case-insensitive)
int 2 false
int -1 false
float 3.14 false
float -1.0 false
string "hello" false
string "2" false
array [1, 2, 3] false
object stdClass false
resource fopen(...) false

Cast::toArray(mixed $value): array

Intelligent array conversion with multiple strategies for different types. Returns [] for values that cannot be cast.

Input Type Input Value Output
array [1, 2, 3] [1, 2, 3]
null null []
string "" []
string " " [] (trimmed)
string '{"a":1}' ["a" => 1] (JSON parsed)
string '[1,2,3]' [1, 2, 3] (JSON parsed)
string '{invalid}' ['{invalid}'] (wrapped)
string "hello" ["hello"] (wrapped)
int 42 [42]
float 3.14 [3.14]
bool true [true]
object Traversable iterator_to_array() result
object with toArray() toArray() result
object stdClass{a:1} ["a" => 1] (via get_object_vars)
resource fopen(...) []

Cast::toJson(mixed $value, int $flags = ..., int $depth = 512): string

Type-safe JSON encoding. Returns '{}' when encoding fails (INF/NAN, invalid UTF-8, depth exceeded, resources, etc.).

Input Type Input Value Output
null null "null"
string "hello" "\"hello\""
string "with/slash" "\"with/slash\""
int 42 "42"
int -17 "-17"
bool true "true"
bool false "false"
float 3.14 "3.14"
array [1, 2, 3] "[1,2,3]"
array ["a" => 1] "{\"a\":1}"
object JsonSerializable via jsonSerialize()
object with toArray() via toArray()
object stdClass{a:1} "{\"a\":1}"
float INF "{}"
float NAN "{}"
(any) depth < 1 "{}"
object toArray() not returning array "{}"
resource fopen(...) "{}"

Default flags: JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE


Error Handling

This library never throws exceptions. When a value cannot be cast to the target type, a documented default value is returned instead:

Because no exceptions are thrown, there is no need for try/catch blocks around cast calls.


All versions of ctw-cast with dependencies

PHP Build Version
Package Version
Requires php Version ^8.5
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 ctw/ctw-cast contains the following files

Loading the files please wait ...